s+ow: do first sleep 30 in script itself if SPOW_CRON=y (#50)

* deps: add shfmt and shellcheck

* s+ow: also add time took to run information
This commit is contained in:
Mark Joshwel 2023-10-30 22:21:07 +08:00 committed by GitHub
parent dd5d95f21f
commit 30d23d61cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 95 additions and 18 deletions

View file

@ -2,10 +2,14 @@
"packages": [ "packages": [
"python311@latest", "python311@latest",
"python311Packages.ipykernel@latest", "python311Packages.ipykernel@latest",
"poetry@latest" "poetry@latest",
"shfmt@latest",
"shellcheck@latest"
], ],
"shell": { "shell": {
"init_hook": ["poetry install"] "init_hook": [
"poetry install"
]
}, },
"nixpkgs": { "nixpkgs": {
"commit": "f80ac848e3d6f0c12c52758c0f25c10c97ca3b62" "commit": "f80ac848e3d6f0c12c52758c0f25c10c97ca3b62"

View file

@ -62,6 +62,46 @@
"store_path": "/nix/store/3kqcf56q2q46hj0g5n4rxzzy1c9vdiqa-python3.11-ipykernel-6.21.2" "store_path": "/nix/store/3kqcf56q2q46hj0g5n4rxzzy1c9vdiqa-python3.11-ipykernel-6.21.2"
} }
} }
},
"shellcheck@latest": {
"last_modified": "2023-10-25T20:49:13Z",
"resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#shellcheck",
"source": "devbox-search",
"version": "0.9.0",
"systems": {
"aarch64-darwin": {
"store_path": "/nix/store/w3i70rjj7nhz211hp172g5i6f8f7kp1j-shellcheck-0.9.0-bin"
},
"aarch64-linux": {
"store_path": "/nix/store/4fapanq6k3x6pb990mbfcnj0cwzsizr7-shellcheck-0.9.0-bin"
},
"x86_64-darwin": {
"store_path": "/nix/store/gkv79r6iiys1k9d523slnz4k7vf99xcl-shellcheck-0.9.0-bin"
},
"x86_64-linux": {
"store_path": "/nix/store/sfc6nh6a2q1a71dz39rm4y5c7az1s3di-shellcheck-0.9.0-bin"
}
}
},
"shfmt@latest": {
"last_modified": "2023-10-25T20:49:13Z",
"resolved": "github:NixOS/nixpkgs/75a52265bda7fd25e06e3a67dee3f0354e73243c#shfmt",
"source": "devbox-search",
"version": "3.7.0",
"systems": {
"aarch64-darwin": {
"store_path": "/nix/store/3nfimx4z0nbxdkkshqpxzrh73llm57vk-shfmt-3.7.0"
},
"aarch64-linux": {
"store_path": "/nix/store/2qk1vvq18nfvjjf1rz4kdxbjd6rayw6a-shfmt-3.7.0"
},
"x86_64-darwin": {
"store_path": "/nix/store/1rigank4nlkq41dzqvp1di4hd628wp02-shfmt-3.7.0"
},
"x86_64-linux": {
"store_path": "/nix/store/lv02gfaqb60xl8a3x55g6s2s0yqrrpp5-shfmt-3.7.0"
}
}
} }
} }
} }

57
s+ow
View file

@ -211,15 +211,13 @@ notify() {
} }
notify_end() { notify_end() {
# $1 is s+ow status (0, 1, 2) # $1 is done tuple
# $2 is termux-location run number # $2 is sharetext
# $3 is sent type (0, 1, 2)
# $4 is sharetext
termux-notification \ termux-notification \
--priority "min" \ --priority "min" \
--id "s+ow" \ --id "s+ow" \
--title "surplus on wheels" \ --title "surplus on wheels" \
--content "$(printf 'Run has finished. (%d, %d, %d)\n\n%s' "$1" "$2" "$3" "$4")" --content "$(printf 'Run has finished. %s\n\n%s' "$1" "$2")"
} }
# program functions # program functions
@ -233,6 +231,21 @@ run() {
printf "[run! stdout (%s)]\n" "$(date)" >>"$SPOW_SESH_OUT" printf "[run! stdout (%s)]\n" "$(date)" >>"$SPOW_SESH_OUT"
printf "[run! stderr (%s)]\n" "$(date)" >>"$SPOW_SESH_ERR" printf "[run! stderr (%s)]\n" "$(date)" >>"$SPOW_SESH_ERR"
time_run_start="$(date +%s)"
# if cron: wait until its the new hour
if [ -n "$SPOW_CRON" ]; then
notify "Waiting for the 30th second to pass..."
printf "waiting for the 30th second to pass...\n"
while [ "$(date +'%S')" -lt 30 ]; do
printf " $(date)\n"
sleep 1
done
printf "proceeding\n"
fi
time_locate_start="$(date +%s)"
# termux-location # termux-location
location="" location=""
for locate_run in 1 2 3; do # run three times in case :p for locate_run in 1 2 3; do # run three times in case :p
@ -252,6 +265,11 @@ run() {
fi fi
done done
time_locate_end="$(date +%s)"
time_locate=$((time_locate_end - time_locate_start))
time_surplus_start="$(date +%s)"
# surplus # surplus
printf "running surplus... " printf "running surplus... "
notify "Running surplus -td $location" notify "Running surplus -td $location"
@ -270,16 +288,22 @@ run() {
printf "skipped\n" printf "skipped\n"
fi fi
time_surplus_end="$(date +%s)"
time_surplus=$((time_surplus_end - time_surplus_start))
# if cron: wait until its the new hour # if cron: wait until its the new hour
if [ -n "$SPOW_CRON" ]; then if [ -n "$SPOW_CRON" ]; then
notify "Waiting for the new hour..."
printf "waiting until the new hour...\n" printf "waiting until the new hour...\n"
while [ "$(date +'%M')" -eq 59 ]; do while [ "$(date +'%M')" -eq 59 ]; do
printf " $(date)\n" printf " $(date)\n"
sleep 1 sleep 1
done done
printf "done\n" printf "proceeding\n"
fi fi
time_sendmsg_start="$(date +%s)"
# mdtest/send message # mdtest/send message
printf "sending message(s)... " printf "sending message(s)... "
notify "Sending message(s)" notify "Sending message(s)"
@ -308,18 +332,24 @@ run() {
printf "using fallback... \n" printf "using fallback... \n"
fi fi
# send done info except
printf "(%d, %d, %d) [lc:%ds sp:%ds]\n" "$status" "$locate_run" "$sent_type" "$time_locate" "$time_surplus" >>"$SPOW_SESH_ERR"
send "$JID_NOMINAL_TARGET" "$sharetext" send "$JID_NOMINAL_TARGET" "$sharetext"
send "$JID_ERRORED_TARGET" "$(cat "$SPOW_SESH_ERR")" send "$JID_ERRORED_TARGET" "$(cat "$SPOW_SESH_ERR")"
fi fi
done_msg="$(printf "done (%d, %d, %d)\n" "$status" "$locate_run" "$sent_type")" time_sendmsg_end="$(date +%s)"
echo "$done_msg" time_sendmsg=$((time_sendmsg_end - time_sendmsg_start))
echo "$done_msg" >>"$SPOW_SESH_ERR"
time_run_end="$(date +%s)"
time_run=$((time_run_end - time_run_start))
done_info="$(printf "(%d, %d, %d) [lc:%ds sp:%ds sm:%ds - %ds]\n" "$status" "$locate_run" "$sent_type" "$time_locate" "$time_surplus" "$time_sendmsg" "$time_run")"
echo "done $done_info" | tee -a "$SPOW_SESH_ERR"
# cleanup # cleanup
printf "%s\n\n" "$(cat "$SPOW_SESH_OUT")" >>"$SPOW_WEEK_OUT" notify_end "$done_info" "$sharetext"
printf "%s\n\n" "$(cat "$SPOW_SESH_ERR")" >>"$SPOW_WEEK_ERR"
notify_end "$status" "$locate_run" "$sent_type" "$sharetext"
} }
# script entry # script entry
@ -344,3 +374,6 @@ choices
$0 $0
run surplus on wheels normally" run surplus on wheels normally"
fi fi
printf "%s\n\n" "$(cat "$SPOW_SESH_OUT")" >>"$SPOW_WEEK_OUT"
printf "%s\n\n" "$(cat "$SPOW_SESH_ERR")" >>"$SPOW_WEEK_ERR"

View file

@ -1,4 +1,4 @@
#!/bin/sh #!/bin/sh
sv-enable crond sv-enable crond
printf "59 * * * *\t(sleep 30; JID_NOMINAL_TARGET=\"\" JID_ERRORED_TARGET=\"\" LOCATION_PRIORITISE_NETWORK=n SPOW_CRON=y ~/.local/bin/s+ow)\n" | crontab - printf "59 * * * *\tJID_NOMINAL_TARGET=\"\" JID_ERRORED_TARGET=\"\" LOCATION_PRIORITISE_NETWORK=n SPOW_CRON=y ~/.local/bin/s+ow\n" | crontab -
crontab -e crontab -e