fix: gitea controller
runner nix smoke / nix label and flake smoke (push) Failing after 1m39s
runner ubuntu smoke / ubuntu-latest label smoke (push) Successful in 1s

This commit is contained in:
2026-11-02 20:06:04 +00:00
parent 0143a1a760
commit 9b5d2ed657
7 changed files with 388 additions and 60 deletions
+6 -3
View File
@@ -581,9 +581,12 @@ immediate teardown; failed jobs retain pre-destroy diagnostics. A healthy,
bootstrapped terminal VM may stay running until the next hourly boundary
measured from original VM creation, capped by its original hard TTL.
Retention first enters a durable retirement state. The controller disables the
exact repository runner registration, deletes it, and confirms its absence
before clearing repository, label, and runner ownership into generic idle. A
Each controller-managed registration is ephemeral: Gitea permits it to accept
exactly one task and revokes reuse credentials after assignment. Retention first
enters a durable retirement state. The controller disables the exact repository
runner registration, waits if a pre-upgrade non-ephemeral registration is still
assigned to an in-progress job, then deletes it and confirms its absence before
clearing repository, label, and runner ownership into generic idle. A
queued job from any allowed repository can atomically claim generic idle only
when persisted server type, location, architecture, and image match one of the
job label's candidates. The VM then remains unschedulable while Hetzner rebuilds
+18 -6
View File
@@ -522,7 +522,7 @@ gcr_sweep_stale_runners() {
runners="$(gcr_gitea_list_runners "$repo")" || continue
# Here-doc instead of pipe: dash runs pipe tails in a subshell, which
# would strand gcr_event/audit writes from the caller's perspective.
while read -r rid rname; do
while read -r rid _rbusy _rdisabled rname; do
[ -n "${rid:-}" ] || continue
case "$rname" in
gcr-*) ;;
@@ -534,11 +534,23 @@ gcr_sweep_stale_runners() {
jid="${rest%-*}"
if ! gcr_record_exists_for_runner "$repo" "$rname"; then
gcr_log warn --ns=sweep "stale repo=$repo registration id=$rid name=$rname -> retire"
if gcr_gitea_deregister_runner_exact "$repo" "$rname"; then
gcr_event "stale-runner-deleted" "${jid:-unknown}" "{\"repo\":\"$repo\",\"runner_id\":$rid,\"name\":\"$rname\"}"
else
gcr_log error --ns=sweep "failed deleting repo=$repo runner id=$rid"
fi
stale_status=0
gcr_gitea_deregister_runner_exact "$repo" "$rname" \
|| stale_status="$?"
case "$stale_status" in
0)
gcr_event "stale-runner-deleted" "${jid:-unknown}" \
"{\"repo\":\"$repo\",\"runner_id\":$rid,\"name\":\"$rname\"}"
;;
2|3)
gcr_log info --ns=sweep \
"stale runner retirement pending repo=$repo runner id=$rid"
;;
*)
gcr_log error --ns=sweep \
"failed deleting repo=$repo runner id=$rid"
;;
esac
fi
done <<EOF
$runners
+107 -35
View File
@@ -37,7 +37,7 @@ gcr_gitea_admin_token() {
tr -d '\n' < "$GITEA_ADMIN_TOKEN_FILE"
}
# gcr_gitea_list_runners REPO — prints "id name" lines for repo runners.
# gcr_gitea_list_runners REPO — prints "id busy disabled name" lines.
gcr_gitea_list_runners() {
repo="$1"
gcr_gitea_admin_token >/dev/null || return 1
@@ -50,10 +50,14 @@ gcr_gitea_list_runners() {
|| return 1
printf '%s' "$runner_page" | jq -e \
'.runners | select(type == "array")
| all(.[]; (.id | type) == "number" and (.name | type) == "string")' \
| all(.[]; (.id | type) == "number"
and (.busy | type) == "boolean"
and (.disabled | type) == "boolean"
and (.name | type) == "string")' \
>/dev/null || return 1
printf '%s' "$runner_page" \
| jq -r '.runners[] | "\(.id) \(.name)"' || return 1
| jq -r '.runners[] | "\(.id) \(.busy) \(.disabled) \(.name)"' \
|| return 1
runner_count="$(printf '%s' "$runner_page" | jq -er '.runners | length')" \
|| return 1
[ "$runner_count" -lt 50 ] && return 0
@@ -61,6 +65,37 @@ gcr_gitea_list_runners() {
done
}
# Find one exact runner after a fresh list. Globals avoid command-substitution
# subshell state loss. Exit 0 = found, 1 = absent, 2 = ambiguous/list failure.
gcr_gitea_find_runner_exact() {
find_runner_repo="$1"; find_runner_name="$2"
GCR_RUNNER_ID=""
GCR_RUNNER_DISABLED=""
find_runner_list="$(gcr_gitea_list_runners "$find_runner_repo")" || return 2
find_runner_count=0
while read -r find_runner_id find_runner_busy find_runner_disabled \
find_runner_candidate_name; do
[ "${find_runner_candidate_name:-}" = "$find_runner_name" ] || continue
case "$find_runner_id" in ''|*[!0-9]*) return 2 ;; esac
case "$find_runner_busy" in true|false) ;; *) return 2 ;; esac
case "$find_runner_disabled" in true|false) ;; *) return 2 ;; esac
find_runner_count=$((find_runner_count + 1))
GCR_RUNNER_ID="$find_runner_id"
GCR_RUNNER_DISABLED="$find_runner_disabled"
done <<EOF
$find_runner_list
EOF
case "$find_runner_count" in
0) return 1 ;;
1) return 0 ;;
*)
gcr_log error --ns=gitea \
"duplicate exact runner registrations repo=$find_runner_repo name=$find_runner_name"
return 2
;;
esac
}
# gcr_gitea_list_owner_repos OWNER — prints fully-qualified repository names.
# Allowed owner globs may refer to either organizations or users. Probe the
# organization route first, then use the user route on a confirmed 404.
@@ -115,6 +150,38 @@ gcr_gitea_job_state() {
| jq -r '.status + ":" + (.conclusion // "")'
}
# Exit 0 when an in-progress repository job is assigned to RUNNER_ID, 1 when
# complete pagination proves none, and 2 on API or schema uncertainty.
gcr_gitea_runner_has_active_job() {
active_repo="$1"; active_runner_id="$2"
case "$active_runner_id" in ''|*[!0-9]*) return 2 ;; esac
active_owner="${active_repo%%/*}"
active_name="${active_repo#*/}"
active_page=1
while :; do
active_jobs="$(gcr_gitea_curl -fsS \
"$GCR_GITEA_URL/api/v1/repos/$active_owner/$active_name/actions/jobs?status=in_progress&page=$active_page&limit=50")" \
|| return 2
printf '%s' "$active_jobs" | jq -e \
'.jobs | select(type == "array")
| all(.[]; (.id | type) == "number"
and (.status | type) == "string"
and ((.runner_id | type) == "number" or
(.runner_id | type) == "null"))' \
>/dev/null || return 2
if printf '%s' "$active_jobs" | jq -e --arg runner "$active_runner_id" \
'.jobs | any(.[];
.status == "in_progress" and (.runner_id | tostring) == $runner)' \
>/dev/null; then
return 0
fi
active_count="$(printf '%s' "$active_jobs" | jq -er '.jobs | length')" \
|| return 2
[ "$active_count" -lt 50 ] && return 1
active_page=$((active_page + 1))
done
}
gcr_gitea_delete_runner() {
repo="$1"; id="$2"
gcr_gitea_admin_token >/dev/null || return 1
@@ -129,46 +196,51 @@ gcr_gitea_delete_runner() {
# name disappeared. Duplicate exact names fail closed for durable retry.
gcr_gitea_deregister_runner_exact() {
dereg_repo="$1"; dereg_name="$2"
# Stale registrations have no allocation record for a durable barrier.
# Their sweep already retries disable on a later pass; VM retirement
# explicitly supplies an empty third argument to request a new barrier.
dereg_delete_not_before="${3-0}"
[ -n "$dereg_repo" ] && [ -n "$dereg_name" ] || return 1
case "$dereg_delete_not_before" in *[!0-9]*) return 1 ;; esac
dereg_runners="$(gcr_gitea_list_runners "$dereg_repo")" || return 1
dereg_count=0
dereg_id=""
while read -r dereg_candidate_id dereg_candidate_name; do
[ "${dereg_candidate_name:-}" = "$dereg_name" ] || continue
case "$dereg_candidate_id" in ''|*[!0-9]*) return 1 ;; esac
dereg_count=$((dereg_count + 1))
dereg_id="$dereg_candidate_id"
done <<EOF
$dereg_runners
EOF
case "$dereg_count" in
0) return 0 ;;
1) ;;
*)
gcr_log error --ns=gitea \
"duplicate exact runner registrations repo=$dereg_repo name=$dereg_name"
return 1
;;
dereg_lookup=0
gcr_gitea_find_runner_exact "$dereg_repo" "$dereg_name" \
|| dereg_lookup="$?"
case "$dereg_lookup" in
0) dereg_id="$GCR_RUNNER_ID" ;;
1) return 0 ;;
*) return 1 ;;
esac
if ! gcr_gitea_set_runner_disabled "$dereg_repo" "$dereg_id" true; then
gcr_log warn --ns=gitea \
"failed disabling runner repo=$dereg_repo name=$dereg_name"
return 1
if [ "$GCR_RUNNER_DISABLED" = false ]; then
if ! gcr_gitea_set_runner_disabled "$dereg_repo" "$dereg_id" true; then
gcr_log warn --ns=gitea \
"failed disabling runner repo=$dereg_repo name=$dereg_name"
return 1
fi
# Caller starts the deletion barrier only after this PATCH succeeds.
return 3
fi
# Legacy records without a barrier must establish one after disabled state
# has been confirmed, never delete immediately.
[ -n "$dereg_delete_not_before" ] || return 3
[ "$(gcr_now_epoch)" -ge "$dereg_delete_not_before" ] || return 2
dereg_active_status=0
gcr_gitea_runner_has_active_job "$dereg_repo" "$dereg_id" \
|| dereg_active_status="$?"
case "$dereg_active_status" in
0) return 2 ;;
1) ;;
*) return 1 ;;
esac
if ! gcr_gitea_delete_runner "$dereg_repo" "$dereg_id"; then
gcr_log warn --ns=gitea \
"runner DELETE uncertain repo=$dereg_repo name=$dereg_name; confirming"
fi
dereg_runners="$(gcr_gitea_list_runners "$dereg_repo")" || return 1
while read -r dereg_candidate_id dereg_candidate_name; do
[ "${dereg_candidate_name:-}" != "$dereg_name" ] || return 1
done <<EOF
$dereg_runners
EOF
return 0
dereg_lookup=0
gcr_gitea_find_runner_exact "$dereg_repo" "$dereg_name" \
|| dereg_lookup="$?"
[ "$dereg_lookup" -eq 1 ]
}
gcr_gitea_set_runner_disabled() {
@@ -186,7 +258,7 @@ gcr_gitea_set_runner_disabled() {
gcr_gitea_runner_disabled() {
repo="$1"; runner_name="$2"; disabled="$3"
runners="$(gcr_gitea_list_runners "$repo")" || return 1
while read -r id name; do
while read -r id _busy _disabled name; do
[ "$name" = "$runner_name" ] || continue
gcr_gitea_set_runner_disabled "$repo" "$id" "$disabled"
return "$?"
+45 -8
View File
@@ -766,8 +766,7 @@ gcr_vm_retire_pending() {
esac
retire_now="$(gcr_now_epoch)"
retire_hard_expires="$((retire_created + retire_ttl * 60))"
if [ "$retire_now" -ge "$retire_expires" ] \
|| [ "$retire_now" -ge "$retire_hard_expires" ]; then
if [ "$retire_now" -ge "$retire_hard_expires" ]; then
gcr_vm_cleanup_start "$retire_job" "$retire_attempt" "$retire_rec" \
retirement-expired false
return "$?"
@@ -775,16 +774,54 @@ gcr_vm_retire_pending() {
retire_repo="$(gcr_record_field "$retire_rec" retirement_repo)"
retire_runner="$(gcr_record_field "$retire_rec" retirement_runner_name)"
if ! gcr_gitea_deregister_runner_exact "$retire_repo" "$retire_runner"; then
gcr_log warn --ns=gitea \
"runner retirement pending job=$retire_job repo=$retire_repo name=$retire_runner"
return 2
retire_delete_not_before="$(gcr_record_field \
"$retire_rec" retirement_delete_not_before)"
case "$retire_delete_not_before" in *[!0-9]*) return 1 ;; esac
retire_status=0
gcr_gitea_deregister_runner_exact "$retire_repo" "$retire_runner" \
"$retire_delete_not_before" \
|| retire_status="$?"
case "$retire_status" in
0) ;;
2)
gcr_log info --ns=gitea \
"busy runner retirement pending job=$retire_job repo=$retire_repo name=$retire_runner"
return 2
;;
3)
retire_delay="${GCR_RECONCILE_INTERVAL_SEC:-60}"
case "$retire_delay" in ''|0|*[!0-9]*) return 1 ;; esac
retire_barrier_now="$(gcr_now_epoch)"
case "$retire_barrier_now" in ''|*[!0-9]*) return 1 ;; esac
retire_delete_not_before="$((retire_barrier_now + retire_delay))"
retire_rec="$(printf '%s' "$retire_rec" | jq -c \
--arg not_before "$retire_delete_not_before" \
'.retirement_delete_not_before = ($not_before | tonumber)')" \
|| return 1
gcr_record_put "$retire_job" "$retire_attempt" "$retire_rec" \
|| return 1
gcr_log info --ns=gitea \
"disabled runner retirement pending job=$retire_job repo=$retire_repo name=$retire_runner"
return 2
;;
*)
gcr_log warn --ns=gitea \
"runner retirement pending job=$retire_job repo=$retire_repo name=$retire_runner"
return 2
;;
esac
if [ "$retire_now" -ge "$retire_expires" ]; then
gcr_vm_cleanup_start "$retire_job" "$retire_attempt" "$retire_rec" \
retirement-expired false
return "$?"
fi
retire_idle="$(printf '%s' "$retire_rec" | jq -c \
'.status = "idle_vm"
| .repo = "" | .label = "" | .runner_name = ""
| .bootstrapped = false
| del(.retirement_repo, .retirement_runner_name)')" || return 1
| del(.retirement_repo, .retirement_runner_name,
.retirement_delete_not_before)')" || return 1
gcr_record_put "$retire_job" "$retire_attempt" "$retire_idle" || return 1
gcr_event "vm-idle" "$retire_job" \
"{\"vm_id\":$retire_vm_id,\"expires_at\":$retire_expires}"
@@ -965,7 +1002,7 @@ umask 077
printf '%s' '$reg_token_b64' | base64 -d > "\$token_file"
trap 'rm -f "\$token_file"' EXIT HUP INT TERM
if [ ! -s /var/lib/gitea-runner/.runner ]; then
/usr/local/bin/gitea-runner register --no-interactive \
/usr/local/bin/gitea-runner register --no-interactive --ephemeral \
--instance '$GCR_GITEA_URL' --token "\$(cat "\$token_file")" \
--name '$runner_name' --labels '$label:host' \
--config /etc/gitea-runner/config.yaml
+1
View File
@@ -167,6 +167,7 @@ gcr_record_retiring_json() {
| .bootstrapped = false
| .retirement_repo = $repo
| .retirement_runner_name = $runner
| del(.retirement_delete_not_before)
| .idle_since = ($now | tonumber)
| .idle_expires_at = ($expires | tonumber)'
}
@@ -34,12 +34,14 @@ curl() {
hostile-token) printf '%s\n' '{"token":"odd $ token"}' ;;
paginated)
case "$url" in
*page=1*) jq -cn '{runners:[range(1;51) | {id:.,name:("runner-" + tostring)}]}' ;;
*page=2*) printf '{"runners":[{"id":51,"name":"runner-51"}]}\n' ;;
*page=1*) jq -cn '{runners:[range(1;51) | {id:.,busy:false,disabled:false,name:("runner-" + tostring)}]}' ;;
*page=2*) printf '{"runners":[{"id":51,"busy":false,"disabled":false,"name":"runner-51"}]}\n' ;;
*) return 1 ;;
esac
;;
malformed-page) printf '{"runners":[{"id":"bad","name":"runner"}]}\n' ;;
malformed-page) printf '{"runners":[{"id":"bad","busy":false,"disabled":false,"name":"runner"}]}\n' ;;
active-job) printf '{"jobs":[{"id":465,"status":"in_progress","runner_id":17}]}\n' ;;
inactive-job) printf '{"jobs":[]}\n' ;;
*) return 1 ;;
esac
}
@@ -53,8 +55,8 @@ rm -f "$hardening_calls"
curl_mode=paginated
runners="$(gcr_gitea_list_runners hinterland/hearth)"
test "$(printf '%s\n' "$runners" | wc -l | tr -d ' ')" = 51
printf '%s\n' "$runners" | grep -q '^1 runner-1$'
printf '%s\n' "$runners" | grep -q '^51 runner-51$'
printf '%s\n' "$runners" | grep -q '^1 false false runner-1$'
printf '%s\n' "$runners" | grep -q '^51 false false runner-51$'
grep -q 'page=1&limit=50$' "$hardening_calls"
grep -q 'page=2&limit=50$' "$hardening_calls"
curl_mode=malformed-page
@@ -62,9 +64,16 @@ if gcr_gitea_list_runners hinterland/hearth >/dev/null 2>&1; then
printf 'malformed runner list must fail closed\n' >&2
exit 1
fi
curl_mode=active-job
gcr_gitea_runner_has_active_job hinterland/hearth 17
if gcr_gitea_runner_has_active_job hinterland/hearth 18; then
printf 'active job must match exact runner id\n' >&2
exit 1
fi
curl_mode=inactive-job
retire_calls="$GCR_STATE_DIR/retire-calls"
gcr_gitea_list_runners() { printf '17 gcr-77-1\n'; }
gcr_gitea_list_runners() { printf '17 false false gcr-77-1\n'; }
gcr_gitea_set_runner_disabled() {
printf 'disable %s %s %s\n' "$1" "$2" "$3" >> "$retire_calls"
return 1
@@ -116,9 +125,11 @@ gcr_gitea_runner_disabled() {
# Terminal registration is retired exactly before VM becomes generic idle.
runner_present=1
runner_disabled=false
gcr_gitea_list_runners() {
printf 'list-runners repo=%s\n' "$1" >> "$calls"
[ "$runner_present" -eq 0 ] || printf '17 gcr-301-1\n'
[ "$runner_present" -eq 0 ] \
|| printf '17 false %s gcr-301-1\n' "$runner_disabled"
}
gcr_gitea_delete_runner() {
printf 'delete-runner repo=%s id=%s\n' "$1" "$2" >> "$calls"
@@ -127,17 +138,22 @@ gcr_gitea_delete_runner() {
gcr_gitea_set_runner_disabled() {
printf 'disable-runner repo=%s id=%s value=%s\n' \
"$1" "$2" "$3" >> "$calls"
runner_disabled="$3"
}
original='{"job_id":"301","run_attempt":"1","repo":"hinterland/hearth","label":"gross-nix-x86-perf","created_at":1000,"ttl_min":480,"vm_id":71,"vm_name":"gcr-301-1","runner_name":"gcr-301-1","server_type":"cx53","budget_rate":"0.032","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
gcr_record_put 301 1 "$original"
gcr_deallocate 301 1 completed:success
test "$(gcr_record_field "$(gcr_record_get 301 1)" status)" = retiring_vm
NOW=2860
gcr_sweep_retiring_vms
NOW=2800
idle="$(gcr_record_get 301 1)"
jq -e 'select(.status == "idle_vm" and .idle_expires_at == 4600 and
.repo == "" and .label == "" and .runner_name == "" and
.bootstrapped == false and .created_at == 1000 and .ttl_min == 480 and
.server_type == "cx53" and .location == "nbg1" and .arch == "amd64" and
.image_id == "424242")' "$(gcr_record_path 301 1)" >/dev/null
test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 2
test "$(grep -c '^list-runners repo=hinterland/hearth$' "$calls")" = 3
grep -q '^disable-runner repo=hinterland/hearth id=17 value=true$' "$calls"
grep -q '^delete-runner repo=hinterland/hearth id=17$' "$calls"
test "$(grep -E '^(disable-runner|delete-runner)' "$calls")" = 'disable-runner repo=hinterland/hearth id=17 value=true
@@ -0,0 +1,187 @@
#!/bin/dash
set -eu
. "$LOG_SH"
. "$STATE_SH"
. "$DECIDE_SH"
. "$HCLOUD_SH"
. "$GITEA_SH"
. "$CONTROLLER_SH"
. "$WEBHOOK_SH"
gcr_state_init
export GCR_ALLOWED_REPOS='hectic-lab/util.nix'
export GCR_CONCURRENCY_CAP=1
export GCR_PER_REPO_CAP=1
export GCR_NIX_IMAGE_ID=424242
export GCR_NIX_VERSION=2.35.0
export GCR_NIX_TARBALL_SHA256=sha256
export GCR_ARM_NIX_TARBALL_SHA256=sha256
export GCR_ACT_RUNNER_VERSION=1.0.6
export GCR_ACT_RUNNER_SHA256=sha256
export GCR_GITEA_URL=https://gitea.example.test
NOW=2800
gcr_now_epoch() { printf '%s' "$NOW"; }
calls="$GCR_STATE_DIR/calls"
runner_present=true
runner_busy=true
runner_disabled=false
disable_delay=120
gcr_gitea_list_runners() {
[ "$runner_present" = false ] \
|| printf '127 %s %s gcr-464-1\n' "$runner_busy" "$runner_disabled"
}
gcr_gitea_set_runner_disabled() {
printf 'disable repo=%s id=%s value=%s\n' "$1" "$2" "$3" >> "$calls"
NOW=$((NOW + disable_delay))
runner_disabled="$3"
}
gcr_gitea_delete_runner() {
printf 'delete repo=%s id=%s\n' "$1" "$2" >> "$calls"
runner_present=false
}
gcr_gitea_job_state() {
case "$2" in
465) printf '%s' "${JOB_465_STATE:-in_progress:}" ;;
*) return 1 ;;
esac
}
gcr_gitea_runner_has_active_job() {
[ "${JOB_465_STATE:-in_progress:}" = completed:success ] && return 1
return 0
}
gcr_vm_create() {
printf 'unexpected-create\n' >> "$calls"
return 1
}
gcr_vm_destroy() {
printf 'unexpected-destroy vm=%s\n' "$1" >> "$calls"
}
# Pre-upgrade reusable runner has already accepted job 465 when job 464's
# terminal event starts retirement. Busy recheck must preserve runner and VM.
job_464='{"job_id":"464","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":174028836,"vm_name":"gcr-464-1","runner_name":"gcr-464-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":true,"status":"vm_active"}'
job_465='{"job_id":"465","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":2000,"ttl_min":null,"vm_id":"","vm_name":"","status":"deferred"}'
gcr_record_put 464 1 "$job_464"
gcr_record_put 465 1 "$job_465"
gcr_deallocate 464 1 completed:success
test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm
test "$(gcr_record_field "$(gcr_record_get 464 1)" retirement_delete_not_before)" = 2980
test "$(gcr_record_field "$(gcr_record_get 465 1)" status)" = deferred
test "$(grep -c '^disable ' "$calls")" = 1
test "$(grep -Ec '^(delete|unexpected-destroy|unexpected-create)' "$calls" || true)" = 0
gcr_retry_deferred
gcr_sweep_retiring_vms
test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm
test "$(gcr_record_field "$(gcr_record_get 465 1)" status)" = deferred
test "$(grep -Ec '^(delete|unexpected-destroy|unexpected-create)' "$calls" || true)" = 0
# Once second task is no longer active, retirement can complete. Polling then
# observes job 465 terminal and drops its deferred allocation without rebuild.
runner_busy=false
gcr_sweep_retiring_vms
test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm
if grep -q '^delete ' "$calls"; then
printf 'retirement must wait one reconcile interval after disable\n' >&2
exit 1
fi
NOW=2980
gcr_sweep_retiring_vms
test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = retiring_vm
if grep -q '^delete ' "$calls"; then
printf 'quiet in-progress job must block retirement when busy=false\n' >&2
exit 1
fi
JOB_465_STATE=completed:success
export JOB_465_STATE
gcr_sweep_retiring_vms
test "$(gcr_record_field "$(gcr_record_get 464 1)" status)" = idle_vm
grep -q '^delete repo=hectic-lab/util.nix id=127$' "$calls"
gcr_retry_deferred
test ! -e "$(gcr_record_path 465 1)"
test "$(grep -Ec '^(unexpected-destroy|unexpected-create)' "$calls" || true)" = 0
# Re-disabling a runner refreshes even an expired persisted barrier. Model a
# PATCH taking longer than one reconciliation interval and require the fresh
# barrier to start after PATCH completion.
runner_present=true
runner_disabled=false
NOW=4000
stale_barrier='{"job_id":"467","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":480,"vm_id":174028838,"vm_name":"gcr-464-1","runner_name":"gcr-464-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"retiring_vm","retirement_repo":"hectic-lab/util.nix","retirement_runner_name":"gcr-464-1","retirement_delete_not_before":3000,"idle_since":2800,"idle_expires_at":4600}'
gcr_record_put 467 1 "$stale_barrier"
retire_status=0
gcr_vm_retire_pending 467 1 "$stale_barrier" || retire_status="$?"
test "$retire_status" = 2
test "$(gcr_record_field "$(gcr_record_get 467 1)" retirement_delete_not_before)" = 4180
test "$(grep -c '^delete ' "$calls")" = 1
gcr_record_del 467 1
# Busy migration safety never weakens profile hard TTL. Expired retirement is
# destroyed without waiting for runner status or attempting deregistration.
gcr_gitea_deregister_runner_exact() {
printf 'unexpected-deregister\n' >> "$calls"
return 2
}
gcr_vm_destroy() {
printf 'hard-ttl-destroy vm=%s\n' "$1" >> "$calls"
}
hard_ttl='{"job_id":"466","run_attempt":"1","repo":"hectic-lab/util.nix","label":"nix","created_at":1000,"ttl_min":30,"vm_id":174028837,"vm_name":"gcr-466-1","runner_name":"gcr-466-1","server_type":"cx23","budget_rate":"0.004","location":"nbg1","arch":"amd64","image_id":"424242","bootstrapped":false,"status":"retiring_vm","retirement_repo":"hectic-lab/util.nix","retirement_runner_name":"gcr-466-1","idle_since":2000,"idle_expires_at":2500}'
gcr_record_put 466 1 "$hard_ttl"
gcr_vm_retire_pending 466 1 "$hard_ttl"
test ! -e "$(gcr_record_path 466 1)"
grep -q '^hard-ttl-destroy vm=174028837$' "$calls"
if grep -q '^unexpected-deregister$' "$calls"; then
printf 'hard TTL must not wait on busy runner deregistration\n' >&2
exit 1
fi
# Registration-time ephemeral state is mandatory; daemon --once is not enough
# because it does not provide Gitea's server-side one-task enforcement.
bootstrap="$(gcr_bootstrap_script nix test-token 480 gcr-465-1)"
printf '%s\n' "$bootstrap" \
| grep -Fq 'gitea-runner register --no-interactive --ephemeral'
if printf '%s\n' "$bootstrap" | grep -Fq 'daemon --once'; then
printf 'daemon --once must not replace ephemeral registration\n' >&2
exit 1
fi
# Stale registrations have no allocation record to persist a barrier. Disable
# on the first sweep, retain active jobs, then delete after they drain.
(
. "$GITEA_SH"
runner_present=true
runner_disabled=false
stale_active=true
stale_calls="$GCR_STATE_DIR/stale-calls"
gcr_gitea_list_runners() {
[ "$runner_present" = false ] \
|| printf '128 false %s gcr-999-1\n' "$runner_disabled"
}
gcr_record_exists_for_runner() { return 1; }
gcr_gitea_set_runner_disabled() {
printf 'disable\n' >> "$stale_calls"
runner_disabled=true
}
gcr_gitea_runner_has_active_job() {
[ "$stale_active" = true ] && return 0
return 1
}
gcr_gitea_delete_runner() {
printf 'delete\n' >> "$stale_calls"
runner_present=false
}
gcr_sweep_stale_runners
test "$runner_disabled" = true
test "$(grep -c '^disable$' "$stale_calls")" = 1
test "$(grep -c '^delete$' "$stale_calls" || true)" = 0
gcr_sweep_stale_runners
test "$runner_present" = true
stale_active=false
gcr_sweep_stale_runners
test "$runner_present" = false
test "$(grep -c '^delete$' "$stale_calls")" = 1
)