← back to Carnegie Reprice

supervise.sh

35 lines

#!/bin/bash
# supervise.sh — keep the 3-shard Carnegie split rollout workers alive until the ledger is complete.
# Run under launchd (com.steve.carnegie-rollout, KeepAlive SuccessfulExit=false).
# Exits 0 when 0 SKUs remain OR a HALT flag is set (so launchd does NOT restart after clean completion).
cd "$(dirname "$0")" || exit 1
SHARDS=3
NODE="$(command -v node)"; [ -z "$NODE" ] && NODE=/opt/homebrew/bin/node
log(){ echo "[$(date -u +%H:%M:%S)] $*" >> supervise.log; }

log "supervisor start (pid $$), $SHARDS shards"
while true; do
  if [ -f rebuild-HALTED.flag ]; then log "HALT flag present — supervisor exiting"; exit 0; fi
  remaining="$("$NODE" rebuild-line.mjs --status 2>/dev/null | head -1 | sed -E 's/.* ([0-9]+) remaining.*/\1/')"
  if [ "$remaining" = "0" ]; then
    log "0 remaining — rollout complete, supervisor exiting"
    echo "CARNEGIE CREATES COMPLETE $(date -u +%FT%TZ) — run approved archive+redirect+GMC (see pending-approval memo)" > ROLLOUT-COMPLETE.marker
    curl -s -X POST http://127.0.0.1:3333/api/parking_lot -H 'Content-Type: application/json' \
      -d '{"title":"Carnegie split creates COMPLETE — run archive+redirect+GMC","body":"TK-10686: all 5,928 per-colorway products created. Approved post-create step (Acapella POC cleanup -> node archive-redirect.mjs apply -> GMC publish via google-merchant-agent) is ready to fire.","tag":"TK-10686"}' >/dev/null 2>&1 || true
    exit 0
  fi

  for n in $(seq 1 $SHARDS); do
    pidfile="worker-$n.pid"
    alive=0
    if [ -f "$pidfile" ] && kill -0 "$(cat "$pidfile" 2>/dev/null)" 2>/dev/null; then alive=1; fi
    if [ $alive -eq 0 ]; then
      "$NODE" rebuild-line.mjs --shard "$n/$SHARDS" >> "rebuild-shard-$n.log" 2>&1 &
      echo $! > "$pidfile"
      log "started shard $n/$SHARDS pid $! (remaining ${remaining:-?})"
      sleep 3
    fi
  done
  sleep 30
done