From 709e7cda12ff8de02457be33b21175a87d355e25 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Wed, 18 Nov 2026 16:17:20 -0800 Subject: [PATCH] fix(swm-macos-runner): end an idle runner when an update is pending The updater installs only when the slot says job=0. A JIT runner that waits for a job also holds job=1, so on a quiet repo a merged fix sat in update-pending and nothing alerted. A human had to start a job to free the slot. While run_job waits on the guest, bounded now calls bounded_check every SWM_UPDATE_IDLE_CHECK_S (300 s). With update-pending present, the check deletes the runner registration first. Only when the DELETE returns 2xx does it end the VM. The pass then counts as neutral, and the next pass's update_wait lets update.sh install. A refused DELETE (422 while the runner runs a job, or an API failure) keeps the job wait as before. So does a 404, because GitHub also answers 404 when access is missing; a runner that really left ends its guest by itself. Both outcomes are logged. SWM_UPDATE_IDLE_CHECK_S=0 turns the check off. A pass whose update_wait gave up does not end runners, so an update that keeps failing to install cannot stop every runner from taking a job. Backstop: update.sh writes the first-seen epoch into update-pending and no longer rewrites it on each waiting pass. When the marker is older than SWM_UPDATE_PENDING_ALERT_S (21600 s), it sends one "an update has been pending" alert. The alert marker goes away with update-pending. install.sh also passes that one knob into the updater plist. GitHub evidence for "a busy runner cannot be deleted": - REST docs, "Delete a self-hosted runner from an organization" (docs.github.com/en/rest/actions/self-hosted-runners): "Forces the removal of a self-hosted runner from an organization." Responses: 204, and 422 "Validation failed, or the endpoint has been spammed." The docs say nothing about busy runners. - GitHub's actions-runner-controller, controllers/actions.summerwind.net/runner_graceful_stop.go (master), has this comment: "we learned that RemoveRunner already has an ability to prevent stopping a busy runner" and "Trying to remove a busy runner can result in errors like the following: ... 422 Bad request - Runner "example-runnerset-0" is still running a job". It also sets runnerBusy = errRes.Response.StatusCode == 422. So the guard depends on observed API behavior that ARC relies on. It is not a documented contract. Advances #252 --- docs/runbooks/swm-macos-runner.md | 13 ++- scripts/swm-macos-runner/config.sh | 2 + scripts/swm-macos-runner/install.sh | 7 +- scripts/swm-macos-runner/lib.sh | 10 ++- scripts/swm-macos-runner/supervisor.sh | 31 ++++++- .../swm-macos-runner/tests/test-install.sh | 6 ++ .../swm-macos-runner/tests/test-supervisor.sh | 82 ++++++++++++++++++- scripts/swm-macos-runner/tests/test-update.sh | 64 +++++++++++++++ scripts/swm-macos-runner/update.sh | 39 ++++++++- 9 files changed, 245 insertions(+), 9 deletions(-) diff --git a/docs/runbooks/swm-macos-runner.md b/docs/runbooks/swm-macos-runner.md index 47d8a1b..5ffb833 100644 --- a/docs/runbooks/swm-macos-runner.md +++ b/docs/runbooks/swm-macos-runner.md @@ -87,6 +87,16 @@ The updater job runs `update.sh` at load and every 600 s. It always exits 0. - The supervisor looks for `update-pending` at the start of each pass, before it clones a VM. While the file exists it waits (at most `SWM_UPDATE_WAIT_S`, 900 s) for the updater to install. The updater removes the file after a good install, or when there is no new version. So an update installs within one cycle: at most `SWM_JOB_MAX_S` + 600 s after the merge, and sooner when a job ends. +- **An idle runner gives way to an update.** While the supervisor waits on a runner, it looks for `update-pending` every `SWM_UPDATE_IDLE_CHECK_S` (300 s). + When it finds the file, it deletes the runner registration through the API. GitHub refuses (HTTP 422) to delete a runner that runs a job. + On a 2xx it logs `update pending and runner N idle ...`, ends the VM, and the next pass waits for the install. + On a refusal it logs `update pending but runner N not deleted (HTTP 422 ...); waiting for the job` and tries again later. + A 404 also keeps the wait: GitHub answers 404 for a missing permission too, so it does not prove the runner is idle. + `SWM_UPDATE_IDLE_CHECK_S=0` turns the check off. + It does not end runners in a pass whose `update_wait` gave up, so an update that keeps failing to install does not end every runner. +- **A long-pending update alerts.** `update-pending` holds the epoch when it was first written. When it is older than `SWM_UPDATE_PENDING_ALERT_S` (21600 s, 6 h), + the updater sends one `macOS runner: an update has been pending for ...` alert and writes `update-pending-alerted`. That marker goes when `update-pending` goes. + `SWM_UPDATE_PENDING_ALERT_S` is the one env-file knob that `install.sh` also writes into the updater plist. - If `install.sh` fails, the version stays unmarked and the next run tries again. - The updater never reloads itself. When a change touches its own plist, run `install.sh` by hand. - **Failed updates alert.** A pass fails when the git fetch or fast-forward fails, the revision cannot be read, or `install.sh` fails. @@ -95,7 +105,7 @@ The updater job runs `update.sh` at load and every 600 s. It always exits 0. At 6 (one hour) it sends one `macOS runner: the updater has failed 6 passes in a row. Last failure: ...` alert and writes `update-alerted`; later failures stay quiet. The first good pass after that sends `macOS runner: the updater works again.` and clears both files. `SWM_UPDATE_ALERT_AFTER` overrides the threshold, but `install.sh` does not carry it into the plist: a hand edit of the plist lasts only until the next `install.sh`. A failed ntfy delivery is retried on the next failed or good pass. - The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file (the topic only, not the supervisor knobs). + The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file (the topic and `SWM_UPDATE_PENDING_ALERT_S`, not the supervisor knobs). The updater cannot reload its own plist, so one hand run of `install.sh` is needed to turn the alert on. Until then the updater logs `NTFY_TOPIC is unset` and sends nothing. - The log is `~/Library/Logs/swm-macos-runner-update.log`. @@ -183,6 +193,7 @@ The log is `~/Library/Logs/swm-macos-runner.log`. Key lines: - `runner ... handed its JIT config`: the runner may be idle or busy; it is waiting for a job or running one (`job=1`). - `update pending; waiting ...` / `update installed or withdrawn` / `update still pending after ...`: the supervisor held a pass for the updater. +- `update pending and runner N idle ...` / `runner ... ended idle for the pending update`: an idle runner made way for an update. - `runner ... finished its job`: a job ended. - `pass failed (, try N): ...`: a failed pass and why. - `fails in a row; next pass in N s`: the supervisor is backing off. diff --git a/scripts/swm-macos-runner/config.sh b/scripts/swm-macos-runner/config.sh index ecfa31a..7f4f8ca 100755 --- a/scripts/swm-macos-runner/config.sh +++ b/scripts/swm-macos-runner/config.sh @@ -38,6 +38,8 @@ export TART_HOME # Pass waits for a pending update: : "${SWM_UPDATE_WAIT_S:=900}" : "${SWM_UPDATE_POLL_S:=5}" +# While a runner waits for a job with an update pending: how often to try to end it (DELETE refused while busy). +: "${SWM_UPDATE_IDLE_CHECK_S:=300}" # GitHub: org, runner App (swm-runner), and the pf guard's ok files. : "${SWM_ORG:=smartwatermelon}" diff --git a/scripts/swm-macos-runner/install.sh b/scripts/swm-macos-runner/install.sh index 549b59a..1ff3821 100755 --- a/scripts/swm-macos-runner/install.sh +++ b/scripts/swm-macos-runner/install.sh @@ -55,7 +55,7 @@ else echo "install: warning: no NTFY_TOPIC in ${ENV_FILE}; alerts will not be sent" >&2 fi -# The updater plist gets the topic only (its alerts); the supervisor knobs below are not for it. +# The updater plist gets the topic (its alerts) and only SWM_UPDATE_PENDING_ALERT_S of the knobs below. upd_env="${ntfy_env}" # SWM__S= env-file lines go into the plist; other SWM_ lines are refused. @@ -68,7 +68,10 @@ if [[ -f "${ENV_FILE}" ]]; then [[ "${line}" =~ ^(SWM_[A-Z_]+_S)=([0-9]+)$ ]] || die "${ENV_FILE}: '${line}' is not SWM__S=" [[ "${knob_keys}" != *" ${BASH_REMATCH[1]} "* ]] || die "${ENV_FILE}: ${BASH_REMATCH[1]} is set twice" knob_keys="${knob_keys}${BASH_REMATCH[1]} " - ntfy_env="${ntfy_env:+${ntfy_env}$'\n'}$(printf ' %s\n %s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}")" + knob_xml="$(printf ' %s\n %s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}")" + ntfy_env="${ntfy_env:+${ntfy_env}$'\n'}${knob_xml}" + # The one knob update.sh reads: its pending-update alert age. + if [[ "${BASH_REMATCH[1]}" == SWM_UPDATE_PENDING_ALERT_S ]]; then upd_env="${upd_env:+${upd_env}$'\n'}${knob_xml}"; fi done <"${ENV_FILE}" fi diff --git a/scripts/swm-macos-runner/lib.sh b/scripts/swm-macos-runner/lib.sh index a3c9859..4fd487a 100755 --- a/scripts/swm-macos-runner/lib.sh +++ b/scripts/swm-macos-runner/lib.sh @@ -36,13 +36,21 @@ notify_ok() { [[ "${NOTIFY_OK:-0}" -eq 1 ]]; } # bounded SECS CMD...: kill CMD after SECS (status 124). # stdin is SWM_IN, stdout SWM_OUT (default /dev/null). BOUNDED_PID names CMD while it runs. +# With BOUNDED_CHECK_S=N, the caller's bounded_check runs every N s; when it returns 0, CMD is killed (status 125). bounded() { - local secs="$1" pid ticks=0 rc=0 + local secs="$1" pid ticks=0 rc=0 every=0 shift + if [[ "${BOUNDED_CHECK_S:-}" =~ ^[1-9][0-9]*$ ]]; then every=$((BOUNDED_CHECK_S * 10)); fi "$@" <"${SWM_IN:-/dev/null}" >"${SWM_OUT:-/dev/null}" & pid=$! BOUNDED_PID="${pid}" while kill -0 "${pid}" 2>/dev/null; do + if [[ "${every}" -gt 0 && "${ticks}" -gt 0 && $((ticks % every)) -eq 0 ]] && bounded_check; then + kill -9 "${pid}" 2>/dev/null + wait "${pid}" 2>/dev/null + BOUNDED_PID="" + return 125 + fi if [[ "${ticks}" -ge $((secs * 10)) ]]; then kill -9 "${pid}" 2>/dev/null wait "${pid}" 2>/dev/null diff --git a/scripts/swm-macos-runner/supervisor.sh b/scripts/swm-macos-runner/supervisor.sh index f995b44..081784f 100755 --- a/scripts/swm-macos-runner/supervisor.sh +++ b/scripts/swm-macos-runner/supervisor.sh @@ -54,6 +54,10 @@ STOP=0 ABORT_OK=0 SLEEP_PID="" API_CODE=000 +# 1 once this pass's update_wait gave up: the update is stuck, so do not end idle runners for it. +UPDATE_GAVE_UP=0 +# 1 when bounded_check ended an idle runner for a pending update. +IDLE_ENDED=0 running() { [[ "${STOP}" -eq 0 ]]; } now() { date +%s; } @@ -181,6 +185,7 @@ update_wait() { t="$(now)" if [[ "${t}" -ge "${deadline}" ]]; then log "update still pending after ${SWM_UPDATE_WAIT_S} s; continuing" + UPDATE_GAVE_UP=1 return 0 fi sleep "${SWM_UPDATE_POLL_S}" & @@ -273,6 +278,22 @@ start_vm() { set_fail pf "IPv6 ok file ${SWM_PF_OK_FILE_V6} not renewed within ${SWM_V6_LIMIT_S} s of the VM start" || return 1 } +# bounded_check: with an update pending, DELETE the runner (422 if busy). 0 = 2xx: end the VM. 404 can mean no access. +bounded_check() { + [[ -e "${UPDATE_PENDING}" && "${UPDATE_GAVE_UP}" -eq 0 && -n "${SL_ID}" ]] || return 1 + running || return 1 + API_CODE=000 + if delete_runner "${SL_ID}" && [[ "${API_CODE}" != 404 ]]; then + log "update pending and runner ${SL_ID} idle (DELETE: HTTP ${API_CODE}); ending VM ${VM} so the update installs" + SL_ID="" + slot_save + IDLE_ENDED=1 + return 0 + fi + log "update pending but runner ${SL_ID} not deleted (HTTP ${API_CODE}; 422 = it runs a job); waiting for the job" + return 1 +} + # run_job: mint last, so a VM that never boots leaves no registration behind. The JIT reaches tart on fd 3 (a pipe). run_job() { local body resp jit id rc=0 @@ -298,12 +319,19 @@ run_job() { exec 3< <(printf '%s' "${jit}") jit="" ABORT_OK=1 - running && SWM_IN=/dev/fd/3 bounded "${SWM_JOB_MAX_S}" "${TART_BIN}" exec -i "${VM}" /bin/bash "${GUEST_JOB}" || rc=$? + IDLE_ENDED=0 + running && BOUNDED_CHECK_S="${SWM_UPDATE_IDLE_CHECK_S}" SWM_IN=/dev/fd/3 \ + bounded "${SWM_JOB_MAX_S}" "${TART_BIN}" exec -i "${VM}" /bin/bash "${GUEST_JOB}" || rc=$? ABORT_OK=0 exec 3<&- SL_JOB=0 slot_save if [[ "${STOP}" -eq 1 ]]; then return 1; fi + # Ended idle for a pending update: neutral, like the SWM_JOB_MAX_S case below. + if [[ "${IDLE_ENDED}" -eq 1 ]]; then + log "runner ${VM} ended idle for the pending update; not counted" + return 0 + fi # An idle JIT runner reaches SWM_JOB_MAX_S every 6 h: neutral, so it never counts as a fail or a recovery. if [[ "${rc}" -eq 124 ]]; then log "runner ${VM} reached SWM_JOB_MAX_S (${SWM_JOB_MAX_S} s); not counted" @@ -366,6 +394,7 @@ cmd_once() { REASON="" HUNG=0 VM="" + UPDATE_GAVE_UP=0 slot_load if sweep && running && update_wait && self_checks && start_vm && run_job; then ok=0; fi teardown diff --git a/scripts/swm-macos-runner/tests/test-install.sh b/scripts/swm-macos-runner/tests/test-install.sh index 27a2005..d9afd69 100755 --- a/scripts/swm-macos-runner/tests/test-install.sh +++ b/scripts/swm-macos-runner/tests/test-install.sh @@ -212,6 +212,12 @@ check_not "knobs: no literal newline escape in the plist" grep -qF '\n' "${H}/${ check "knobs: topic still in the plist" grep -q 'drill-topic_1.x' "${H}/${P}" check "updater plist gets the topic" grep -q 'drill-topic_1.x' "${H}/${U}" check_not "knobs: not in the updater plist" grep -q 'SWM_CLONE_LIMIT_S' "${H}/${U}" +ENV_BODY=$'NTFY_TOPIC=drill-topic_1.x\nSWM_CLONE_LIMIT_S=1\nSWM_UPDATE_PENDING_ALERT_S=60' run_install +check "knobs: pending-alert knob installs" test "${RC}" -eq 0 +check "knobs: SWM_UPDATE_PENDING_ALERT_S in the updater plist" \ + awk -v k=SWM_UPDATE_PENDING_ALERT_S -v v='60' "${PLIST_AWK}" "${H}/${U}" +check "knobs: updater plist keeps the topic" grep -q 'drill-topic_1.x' "${H}/${U}" +check_not "knobs: other knobs still not in the updater plist" grep -q 'SWM_CLONE_LIMIT_S' "${H}/${U}" ENV_BODY=$'SWM_CLONE_LIMIT_S=1' run_install check "knobs: knob alone (no topic) still installs" test "${RC}" -eq 0 check "knobs: knob alone lands in the plist" grep -q 'SWM_CLONE_LIMIT_S' "${H}/${P}" diff --git a/scripts/swm-macos-runner/tests/test-supervisor.sh b/scripts/swm-macos-runner/tests/test-supervisor.sh index e47a084..9afb5c3 100755 --- a/scripts/swm-macos-runner/tests/test-supervisor.sh +++ b/scripts/swm-macos-runner/tests/test-supervisor.sh @@ -84,7 +84,8 @@ case "${cmd}" in cat >>"${S}/exec-stdin" echo >>"${S}/exec-stdin" cat "${S}/state/slot" >"${S}/slot-during-job" - : >"${S}/in-job" + # One line, so wait_for (which counts lines) sees it at once. + echo in >"${S}/in-job" [[ -z "${STUB_JOB_HANG:-}" ]] || hang [[ -z "${STUB_JOB_FAIL:-}" ]] || exit 1 else @@ -142,7 +143,10 @@ case "${method} ${url}" in "POST "*/actions/runners/generate-jitconfig) reply '{"runner":{"id":4242,"name":"x"},"encoded_jit_config":"JITSECRET-4242"}' 201 ;; "DELETE "*/actions/runners/*) - if [[ -n "${STUB_DELETE_FAIL:-}" ]]; then reply '{"message":"boom"}' 500; else reply '' 204; fi ;; + if [[ -n "${STUB_DELETE_FAIL:-}" ]]; then reply '{"message":"boom"}' 500 + elif [[ -n "${STUB_DELETE_BUSY:-}" ]]; then reply '{"message":"Bad request - Runner \"x\" is still running a job\""}' 422 + elif [[ -n "${STUB_DELETE_GONE:-}" ]]; then reply '{"message":"Not Found"}' 404 + else reply '' 204; fi ;; *) reply '{"message":"unexpected"}' 500 ;; esac STUB @@ -454,6 +458,80 @@ reset sup once check_not "no marker: no wait logged" grep -q 'update pending' "${S}/sup.err" +echo "-- update pending while the runner idles -> runner DELETEd, VM ended, next pass waits" +dels() { count '^DELETE .*/actions/runners/4242 ' "${S}/requests.log"; } +reset +SECONDS=0 +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 sup once & +pid=$! +wait_for "${S}/in-job" +: >"${PEND}" +wait "${pid}" +rc=$? +took="${SECONDS}" +vm="$(vm_name)" +check "idle end: exit 0" test "${rc}" -eq 0 +check "idle end: ended long before SWM_JOB_MAX_S" test "${took}" -lt 10 +check_is "idle end: runner DELETEd before the VM stop" "tart exec-i|curl DELETE 4242|tart stop|tart delete|" \ + "$(sed -n '/exec-i/,$p' "${S}/order.log" | tr '\n' '|')" +check "idle end: VM deleted" grep -qx "delete ${vm}" "${S}/tart.argv" +check "idle end: logged" grep -q 'update pending and runner 4242 idle (DELETE: HTTP 204)' "${S}/sup.err" +check "idle end: not counted logged" grep -q 'ended idle for the pending update' "${S}/sup.err" +check_not "idle end: did not reach SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_is "idle end: slot cleared, not a failure" "//0/0" "$(slot name)/$(slot id)/$(slot job)/$(slot fails)" +check_is "idle end: no alert" 0 "$(count . "${S}/ntfy.log")" +/bin/rm -f "${S}/order.log" +SWM_UPDATE_POLL_S=0.1 SWM_UPDATE_WAIT_S=30 sup once & +pid=$! +wait_for "${S}/order.log" +sleep 0.6 +check_is "idle end: next pass waits for the update before a clone" "tart list|" "$(order)" +/bin/rm -f "${PEND}" +wait "${pid}" +check "idle end: next pass runs once the update is in" grep -q 'update installed or withdrawn' "${S}/sup.err" + +echo "-- update pending but GitHub refuses the DELETE (busy) -> job keeps running" +reset +STUB_JOB_HANG=1 STUB_DELETE_BUSY=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_JOB_MAX_S=4 sup once & +pid=$! +wait_for "${S}/in-job" +: >"${PEND}" +wait "${pid}" +check "busy: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "busy: not ended idle" grep -q 'ended idle for the pending update' "${S}/sup.err" +check "busy: DELETE tried more than once during the job" test "$(dels)" -ge 3 +check "busy: refusal logged" grep -q 'runner 4242 not deleted (HTTP 422' "${S}/sup.err" +check_is "busy: VM stopped only after the job exec ended" "tart stop" \ + "$(sed -n '/exec-i/,$p' "${S}/order.log" | grep -v 'curl DELETE' | sed -n 2p)" +check_is "busy: id= kept for the next sweep" 4242 "$(slot id)" + +echo "-- update pending and DELETE answers 404 -> not proof of idle, job wait goes on" +reset +STUB_JOB_HANG=1 STUB_DELETE_GONE=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_JOB_MAX_S=3 sup once & +pid=$! +wait_for "${S}/in-job" +: >"${PEND}" +wait "${pid}" +check "404: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "404: not ended idle" grep -q 'ended idle for the pending update' "${S}/sup.err" +check "404: logged as not deleted" grep -q 'runner 4242 not deleted (HTTP 404' "${S}/sup.err" +check_is "404: id= cleared by teardown" "" "$(slot id)" + +echo "-- no update pending -> no DELETE during the job" +reset +STUB_JOB_HANG=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_JOB_MAX_S=3 sup once +check "no pending: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_is "no pending: only the teardown DELETE" 1 "$(dels)" + +echo "-- update_wait gave up this pass -> the stuck update ends no runner" +reset +mkdir -p "${S}/state" +: >"${PEND}" +STUB_JOB_HANG=1 SWM_UPDATE_POLL_S=0.1 SWM_UPDATE_WAIT_S=1 SWM_UPDATE_IDLE_CHECK_S=1 SWM_JOB_MAX_S=3 sup once +check "gave up: job ran to SWM_JOB_MAX_S" grep -q 'reached SWM_JOB_MAX_S' "${S}/sup.err" +check_not "gave up: not ended idle" grep -q 'ended idle for the pending update' "${S}/sup.err" +check_is "gave up: only the teardown DELETE" 1 "$(dels)" + echo "-- exec not ready in time -> VM deleted, no JIT request" reset # 2 s, not 1: the deadline has 1 s granularity, so 1 s can allow a single try. diff --git a/scripts/swm-macos-runner/tests/test-update.sh b/scripts/swm-macos-runner/tests/test-update.sh index 1d3f012..86941e7 100755 --- a/scripts/swm-macos-runner/tests/test-update.sh +++ b/scripts/swm-macos-runner/tests/test-update.sh @@ -16,6 +16,10 @@ check_not() { shift if "$@"; then _fail "${name}"; else _pass "${name}"; fi } +# check_is NAME WANT GOT: pass when GOT equals WANT. +check_is() { + if [[ "$3" == "$2" ]]; then _pass "$1"; else _fail "$1 (got '$3', want '$2')"; fi +} HERE="$(CDPATH='' cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" UPDATE="${HERE}/../update.sh" @@ -304,6 +308,66 @@ check "warning logged" grep -q 'NTFY_TOPIC is unset' <<<"${OUT}" check "exit 0" test "${RC}" -eq 0 check_not "no marker without a delivered alert" test -e "${S}/update-alerted" +echo "== pending-update backstop" +busy() { printf 'name=mimolette-macos-1\nid=7\nfails=0\nalert=\njob=1\n' >"${S}/slot"; } +pend_alerts() { if [[ -e "${L}/curl" ]]; then grep -c 'an update has been pending' "${L}/curl"; else echo 0; fi; } +run_update +newrev +busy +again NTFY_TOPIC=topic-abc +first="$(cat "${S}/update-pending")" +check "marker holds the epoch it was written" grep -qE '^[0-9]{10,}$' "${S}/update-pending" +again NTFY_TOPIC=topic-abc +check_is "a second waiting pass keeps the first epoch" "${first}" "$(cat "${S}/update-pending")" +check_is "young marker: no alert" 0 "$(pend_alerts)" +printf '%s\n' $(($(date +%s) - 21000)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc +check_is "just under 6 h: no alert" 0 "$(pend_alerts)" +printf '%s\n' $(($(date +%s) - 21700)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc +check_is "over 6 h: one alert" 1 "$(pend_alerts)" +check "alert text" grep -q 'macOS runner: an update has been pending for 6 h' "${L}/curl" +check "alert marker written" test -e "${S}/update-pending-alerted" +again NTFY_TOPIC=topic-abc +again NTFY_TOPIC=topic-abc +check_is "later waiting passes stay quiet" 1 "$(pend_alerts)" +printf 'name=\nid=\nfails=0\nalert=\njob=0\n' >"${S}/slot" +again NTFY_TOPIC=topic-abc +check_not "install clears the pending marker" test -e "${S}/update-pending" +check_not "install clears the alert marker" test -e "${S}/update-pending-alerted" +printf '%s\n' "scripts/swm-macos-runner REV3" >"${L}/revs" +busy +again NTFY_TOPIC=topic-abc +printf '%s\n' $(($(date +%s) - 21700)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc +check_is "a new pending period alerts again" 2 "$(pend_alerts)" +run_update +newrev +printf '%s\n' $(($(date +%s) - 21700)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc STUB_INSTALL_FAIL=1 +check_is "failed install with an old marker: alerts" 1 "$(pend_alerts)" +run_update +newrev +busy +printf '%s\n' $(($(date +%s) - 100)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc SWM_UPDATE_PENDING_ALERT_S=60 +check_is "limit from the environment" 1 "$(pend_alerts)" +run_update +newrev +busy +printf '%s\n' $(($(date +%s) - 21700)) >"${S}/update-pending" +again NTFY_TOPIC=topic-abc STUB_CURL_FAIL=1 +check_not "failed delivery: no alert marker" test -e "${S}/update-pending-alerted" +again NTFY_TOPIC=topic-abc +check "failed delivery: the next pass sends it" test -e "${S}/update-pending-alerted" +run_update +newrev +busy +printf 'x\n' >"${S}/update-pending" +again NTFY_TOPIC=topic-abc +check "an old empty-style marker gets an epoch" grep -qE '^[0-9]{10,}$' "${S}/update-pending" +check_is "and does not alert at once" 0 "$(pend_alerts)" + echo "== failed delivery is retried" run_update fails 6 NTFY_TOPIC=topic-abc STUB_CURL_FAIL=1 diff --git a/scripts/swm-macos-runner/update.sh b/scripts/swm-macos-runner/update.sh index 17448ed..e02dedf 100755 --- a/scripts/swm-macos-runner/update.sh +++ b/scripts/swm-macos-runner/update.sh @@ -19,6 +19,9 @@ PENDING="${STATE_DIR}/update-pending" FAILS="${STATE_DIR}/update-fails" ALERTED="${STATE_DIR}/update-alerted" ALERT_AFTER="${SWM_UPDATE_ALERT_AFTER:-6}" +# PENDING holds the epoch it was first written; one alert once it is older than this, marked by PENDING_ALERTED. +PENDING_ALERT_S="${SWM_UPDATE_PENDING_ALERT_S:-21600}" +PENDING_ALERTED="${STATE_DIR}/update-pending-alerted" log() { local ts @@ -73,6 +76,37 @@ pass_ok() { fi } +# pending_since: the epoch in PENDING; writes the current one when the file is missing or holds no number. +pending_since() { + local s + s="$(cat "${PENDING}" 2>/dev/null)" || s="" + if [[ ! "${s}" =~ ^[0-9]+$ ]]; then + s="$(date +%s)" + (umask 077 && mkdir -p "${STATE_DIR}" && printf '%s\n' "${s}" >"${PENDING}") || log "cannot write ${PENDING}" + fi + printf '%s' "${s}" +} + +# pending_backstop: after every pass. One alert when PENDING is older than the limit; its marker goes with PENDING. +pending_backstop() { + local since t age limit="${PENDING_ALERT_S}" + if [[ ! -e "${PENDING}" ]]; then + if [[ -e "${PENDING_ALERTED}" ]]; then "${RM_BIN}" -f "${PENDING_ALERTED}"; fi + return 0 + fi + if [[ ! "${limit}" =~ ^[0-9]+$ ]] || [[ "${limit}" -lt 1 ]]; then + log "SWM_UPDATE_PENDING_ALERT_S is not a positive number; using 21600" + limit=21600 + fi + since="$(pending_since)" + t="$(date +%s)" + age=$((t - since)) + if [[ "${age}" -ge "${limit}" && ! -e "${PENDING_ALERTED}" ]]; then + notify "macOS runner: an update has been pending for $((age / 3600)) h $((age % 3600 / 60)) min and has not installed. See the supervisor and updater logs." + if [[ "${NOTIFY_OK}" -eq 1 ]]; then (umask 077 && : >"${PENDING_ALERTED}") || log "cannot write ${PENDING_ALERTED}"; fi + fi +} + main() { # Not `git pull`: the NOS updater runs in the same second, and two pulls # collide on FETCH_HEAD. Any failure retries. @@ -106,7 +140,8 @@ main() { if [[ "${job}" != 0 ]]; then # Waiting is neither a failure nor a success: the failure count stays as it is. log "new version ${rev} but a job is running; waiting" - (umask 077 && mkdir -p "${STATE_DIR}" && : >"${PENDING}") || log "cannot write ${PENDING}" + # Written once, so its epoch tells the backstop how long the wait is. + pending_since >/dev/null return 0 fi @@ -123,4 +158,4 @@ main() { } # One line, so bash has parsed it all before install.sh can replace this file. -main; exit 0 +main; pending_backstop; exit 0