igneum/tools/attack/adv-cache-2/drain.sh

34 lines
2.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# adv-cache-2 queue drain, run ON a build box with nohup: claims and runs every unclaimed file of
# /srv/builds/_adv/cache/queue/ in name order, one at a time, no gap between them (the brief's queue rule). A file is
# claimed by `mkdir claims/<file>` (atomic; exists = taken, by this lane or a sibling). Files whose name does not carry
# `adv-cache-2` belong to a sibling lane; they are run too when nothing of ours is left, and listed in drain.log.
# Stop the drain by its pid file; a running item is stopped by its own pid file, never by name.
# Box rule of 19:55 BST, 7 October 2026 (coordinator): one sweep per box at a time across every lane, taken under
# `flock /srv/builds/_adv/locks/sweep.lock`; every item below runs under that lock. `drain.sh [wait-pid]` first waits
# for an already running item of ours (started before the rule) to exit.
set -uo pipefail
Q=/srv/builds/_adv/cache/queue; C=/srv/builds/_adv/cache/claims; R=/srv/builds/_adv-adv-cache-2
LOCK=/srv/builds/_adv/locks/sweep.lock
mkdir -p "$Q" "$C" "$R" /srv/builds/_adv/locks; echo $$ > "$R/drain.pid"
say() { echo "[$(date -u +%FT%TZ)] $*" >> "$R/drain.log"; }
if [ -n "${1:-}" ]; then say "waiting for pid $1 to exit before the first lock take"; while kill -0 "$1" 2>/dev/null; do sleep 5; done; fi
ran=0
while :; do
picked=""
# ours first, then any sibling file, name order
for pass in own any; do
for f in $(ls "$Q" 2>/dev/null | sort); do
case "$pass" in own) case "$f" in *adv-cache-2*) ;; *) continue ;; esac ;; esac
[ -x "$Q/$f" ] || continue
if mkdir "$C/$f" 2>/dev/null; then picked="$f"; break 2; fi
done
done
[ -n "$picked" ] || { say "queue empty after $ran items; exiting"; break; }
owner=$(echo "$picked" | sed -E 's/^[0-9]+-(adv-[a-z0-9-]*?)-.*/\1/' | sed -E 's/^(adv-[a-z]+(-[0-9]+)?).*/\1/')
say "claimed $picked (owner $owner); waiting for $LOCK"
tw=$(date +%s)
flock "$LOCK" -c "echo \"[\$(date -u +%FT%TZ)] start $picked (owner $owner) after $(( $(date +%s) - tw )) s wait\" >> $R/drain.log; setsid $Q/$picked"; rc=$?
say "end $picked rc $rc after $(( $(date +%s) - tw )) s (wait + run)"
ran=$((ran+1))
done