Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/runbooks/nos-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ The updater LaunchAgent runs every 600 s. It pulls the clone and runs `install.s
The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file.
The updater cannot reload its own plist, so one hand run of `install.sh` (section 4, step 3) is needed to turn the alert on.
Until then the updater logs `NTFY_TOPIC is unset` and sends nothing.
- On a quiet night the supervisor restarts each slot about hourly (`NOS_IDLE_MAX_S`). That is normal.
- A pf rule change needs `install-pf.sh` again with sudo. The updater never touches root files.

## 6. Hold, release and full stop
Expand Down Expand Up @@ -222,6 +223,9 @@ Key lines to look for:

- `was still registered after its VM exited; deleted`: normal after a job. GitHub keeps a JIT runner listed for a while,
so the supervisor deletes it. A VM exit is not a failed start: a runner that dies before its job is not detected yet.
- `slot N: runner ID idle for S s (limit L s); restarting`: the runner waited longer than `NOS_IDLE_MAX_S` (default 3600, `0` disables)
for a job, so the supervisor deleted it and stops the VM. A fresh VM starts in its place. One slot restarts per pass.
A `delete failed (HTTP 422)` line instead means the runner had just taken a job; the VM keeps running.
- `container run failed`: a failed start. Three in a row fail over.
- `NOS_RUNNER set to ubuntu-latest`: the supervisor failed over. The reason follows.
- `stopping: switching NOS_RUNNER`: the supervisor got SIGTERM.
Expand Down
3 changes: 3 additions & 0 deletions scripts/nos-runner/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ _nos_conf_dir="$(CDPATH='' cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
: "${NOS_INTERVAL:=30}"
: "${NOS_CONTAINER_TIMEOUT:=60}"

# Seconds a runner may wait idle for a job before its slot is restarted; 0 disables. A JIT runner idle too long goes stale (dev-env#297).
: "${NOS_IDLE_MAX_S:=3600}"

# Worst-case seconds from SIGTERM to exit. Task 5 sets the plist ExitTimeOut above this.
: "${NOS_SHUTDOWN_MAX_S:=1030}"

Expand Down
24 changes: 24 additions & 0 deletions scripts/nos-runner/supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,29 @@ delete_runner() {
log "runner $1: delete failed (HTTP ${API_CODE}); will retry"
return 1
}
# Restart one idle-too-long slot per pass. Delete before stop: a runner that took a job meanwhile gets HTTP 422 and keeps its VM.
# Remaining race: a job assigned before GitHub reports busy=true is lost (accepted 2026-11-18).
restart_stale_slots() {
local n id name started age
[[ "${NOS_IDLE_MAX_S}" -gt 0 ]] || return 0
for ((n = 1; n <= NOS_SLOTS; n += 1)); do
slot_load "${n}"
name="${SL_NAME}"
id="${SL_ID}"
started="${SL_STARTED}"
[[ -n "${name}" && -n "${id}" && "${started}" =~ ^[0-9]+$ ]] || continue
age=$(($(now) - started))
((age >= NOS_IDLE_MAX_S)) || continue
runner_idle "${id}" || continue
delete_runner "${id}" || continue
: >"${NOS_STATE_DIR}/dropped-${n}"
log "slot ${n}: runner ${id} idle for ${age} s (limit ${NOS_IDLE_MAX_S} s); restarting"
if stop_slot "${n}"; then refresh_ls; fi
return 0
done
return 0
}

# deregister_pending: delete runners whose VM is gone (name= empty, id= set). A failed DELETE keeps id= for retry.

# After a VM exit, the slot's fail count resets: 2xx and 404 both follow a finished job (see the comment inside).
Expand Down Expand Up @@ -526,6 +549,7 @@ cmd_once() {
return 1
fi
deregister_pending
restart_stale_slots
running || return 1
if ! slots_ok; then
log "self-check failed: a slot failed 3 starts in a row"
Expand Down
109 changes: 108 additions & 1 deletion scripts/nos-runner/tests/test-supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ case "\${method} \${url}" in
if [[ -n "\${STUB_TOKEN_FAIL:-}" ]]; then reply '{"message":"boom"}' 500; else reply '{"token":"ghs_stubtok"}' 201; fi
;;
"GET "*/actions/runners/*)
if [[ -n "\${STUB_RUNNER_GET_FAIL:-}" ]]; then reply '{"message":"boom"}' 500; exit 0; fi
id="\${url##*/}"
if grep -qx "\${id}" "\${S}/busy" 2>/dev/null; then b=true; else b=false; fi
reply "{\"id\":\${id},\"busy\":\${b}}" 200
Expand All @@ -161,7 +162,9 @@ case "\${method} \${url}" in
;;
"POST "*/actions/variables) setvar; reply '' 201 ;;
"DELETE "*/actions/runners/*)
if [[ -n "\${STUB_DELETE_FAIL:-}" ]]; then
if [[ -n "\${STUB_DELETE_BUSY:-}" ]]; then
reply '{"message":"Runner \\"x\\" is currently running a job and cannot be deleted."}' 422
elif [[ -n "\${STUB_DELETE_FAIL:-}" ]]; then
reply '{"message":"boom"}' 500
elif grep -qx "\${url##*/}" "\${S}/gone" 2>/dev/null; then
reply '{"message":"Not Found"}' 404
Expand Down Expand Up @@ -441,6 +444,110 @@ check "sigterm_stops_idle_keeps_busy: runner 101 deleted" grep -qE '^DELETE .*/a
check_not "sigterm_stops_idle_keeps_busy: runner 102 kept" grep -qE '^DELETE .*/actions/runners/102 ' "${S}/requests.log"
check "sigterm_stops_idle_keeps_busy: slot 2 state kept" grep -qE '^name=mimolette-2-' "${S}/state/slot-2"

# age_slot N: make slot N's VM look 2 hours old.
age_slot() {
local old
old="$(( $(date +%s || true) - 7200 ))"
sed "s/^started=.*/started=${old}/" "${S}/state/slot-$1" >"${S}/state/slot-$1.new" && mv "${S}/state/slot-$1.new" "${S}/state/slot-$1"
}

echo "-- idle_restart_replaces_old_idle_slot"
reset
sup once
age_slot 1
NOS_IDLE_MAX_S=3600 sup once
del_at="$(grep -n '^curl DELETE .*/runners/101' "${S}/order.log" | head -n 1 | cut -d: -f1)"
stop_at="$(grep -n '^container stop mimolette-1-' "${S}/order.log" | head -n 1 | cut -d: -f1)"
check "idle_restart_replaces_old_idle_slot: delete before stop" test -n "${del_at}" -a -n "${stop_at}" -a "${del_at:-0}" -lt "${stop_at:-0}"
check "idle_restart_replaces_old_idle_slot: logged" grep -q 'slot 1: runner 101 idle for' "${S}/sup.err"
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_replaces_old_idle_slot: one new VM" 3 runs

echo "-- idle_restart_skips_young_slot"
reset
sup once
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_skips_young_slot: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log"
check_not "idle_restart_skips_young_slot: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv"

echo "-- idle_restart_skips_busy"
reset
sup once
age_slot 1
echo 101 >"${S}/busy"
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_skips_busy: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log"
check_not "idle_restart_skips_busy: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv"

echo "-- idle_restart_skips_unknown"
reset
sup once
age_slot 1
STUB_RUNNER_GET_FAIL=1 NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_skips_unknown: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log"
check_not "idle_restart_skips_unknown: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv"

echo "-- idle_restart_delete_fails_keeps_vm"
reset
sup once
age_slot 1
STUB_DELETE_FAIL=1 NOS_IDLE_MAX_S=3600 sup once
check_not "idle_restart_delete_fails_keeps_vm: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv"
check "idle_restart_delete_fails_keeps_vm: slot keeps VM" grep -qE '^name=mimolette-1-' "${S}/state/slot-1"

echo "-- idle_restart_delete_422_keeps_vm"
reset
sup once
age_slot 1
STUB_DELETE_BUSY=1 NOS_IDLE_MAX_S=3600 sup once
check_not "idle_restart_delete_422_keeps_vm: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv"
check_not "idle_restart_delete_422_keeps_vm: no dropped marker" test -e "${S}/state/dropped-1"
check "idle_restart_delete_422_keeps_vm: slot keeps VM" grep -qE '^name=mimolette-1-' "${S}/state/slot-1"
check "idle_restart_delete_422_keeps_vm: slot keeps id" grep -qx 'id=101' "${S}/state/slot-1"
check "idle_restart_delete_422_keeps_vm: logs HTTP 422" grep -q 'HTTP 422' "${S}/sup.err"

echo "-- idle_restart_stop_fails_marks_dropped"
reset
sup once
age_slot 1
STUB_STOP_FAIL=1 NOS_IDLE_MAX_S=3600 sup once
check "idle_restart_stop_fails_marks_dropped: marker" test -e "${S}/state/dropped-1"
check_eq "idle_restart_stop_fails_marks_dropped: nothing started" 2 runs

echo "-- idle_restart_not_a_fail"
reset
sup once
age_slot 1
NOS_IDLE_MAX_S=3600 sup once
check "idle_restart_not_a_fail: fails=0" grep -qx 'fails=0' "${S}/state/slot-1"

echo "-- idle_restart_disabled_at_zero"
reset
sup once
age_slot 1
NOS_IDLE_MAX_S=0 sup once
check_eq "idle_restart_disabled_at_zero: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log"

echo "-- idle_restart_refreshes_vm_list"
reset
sup once
age_slot 1
# Slot 2 is empty but a stray mimolette VM still shows in the pass's first ls: a stale list blocks the start (2 listed).
printf 'name=\nid=\nfails=0\nstarted=0\n' >"${S}/state/slot-2"
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_refreshes_vm_list: empty slot 2 starts in the same pass" 1 count '^mimolette-2-' "${S}/running"

echo "-- idle_restart_one_slot_per_pass"
reset
sup once
age_slot 1
age_slot 2
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_one_slot_per_pass: one delete this pass" 1 count '^DELETE .*/actions/runners/10[12] ' "${S}/requests.log"
check_eq "idle_restart_one_slot_per_pass: other slot untouched this pass" 0 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log"
NOS_IDLE_MAX_S=3600 sup once
check_eq "idle_restart_one_slot_per_pass: other slot restarted next pass" 1 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log"

echo "-- sigterm_mid_pass_starts_no_vm"
reset
bg_run STUB_LS_SLEEP=2
Expand Down
Loading