Files
paula-alves 7813475a7c
ci / rust (pull_request) Successful in 4m29s
ci / typescript (pull_request) Successful in 9m10s
fix(boot): bound post-start health poll so a slow API cannot kill PM2 (BIT-32855)
wait_for polled on a fixed try count (HEALTH_TRIES=90, ~450s worst case),
which exceeded photoreport-boot.service's TimeoutStartSec. When the API a
container depends on was briefly down (pr-api rebooting after a vzmigrate),
pr-site's wait_for never got 200, systemd timed the oneshot out, killed the
whole cgroup — including the PM2 daemon boot.sh had just launched — and left
nginx with no upstream (502) that never self-healed even after the API
recovered.

Bound wait_for by wall clock (BOOT_HEALTH_BUDGET_SEC, default 150s) so
boot.sh always exits within TimeoutStartSec with margin for the pm2_start
retry path. PM2 (autorestart) and the next-env watchdog handle real
recovery; this poll only confirms the boot.

Paperclip-Issue: BIT-32855
2026-11-22 01:13:58 +00:00

84 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Brings this container's PhotoReport service back after a reboot (container or Proxmox host), from the code that
# is already checked out and built by the last deploy: nothing is fetched, built or migrated here.
# Run by photoreport-boot.service: boot.sh <api|app|site|ingest>. Safe to run by hand at any time.
set -Eeuo pipefail
SERVICE="${1:-${DEPLOY_SERVICE:-}}"
[[ -n "$SERVICE" ]] || { echo "usage: boot.sh <api|app|site|ingest>" >&2; exit 2; }
# The unit reads the receiver's env file for DEPLOY_SERVICE; its own PORT (and secret) must not reach PM2, which
# copies this environment into the apps (--update-env): the API would try to listen on the receiver's port.
unset PORT DEPLOY_WEBHOOK_SECRET DEPLOY_SERVICE DEPLOY_SCRIPT DEPLOY_LOG
REPO_DIR="${DEPLOY_REPO_DIR:-/srv/photoreport}"
API_PORT="${API_PORT:-3000}"
SITE_PORT="${SITE_PORT:-3100}"
# Bound the post-start health poll by wall-clock, not by a try count. A count of 90 (sleep 2 +
# max-time 3) could run ~450s and exceed photoreport-boot.service's TimeoutStartSec, at which
# point systemd kills the whole cgroup — including the PM2 daemon this script just launched —
# and leaves the port closed with no self-heal (BIT-32855: pr-site turned a transient pr-api
# reboot into a persistent 502 this way). PM2 (autorestart) and the next-env watchdog handle
# real recovery; this poll only confirms the boot, so a 150s budget fits under both the
# deployed 300s and the repo's 420s TimeoutStartSec with margin for the pm2_start retry path.
HEALTH_BUDGET_SEC="${BOOT_HEALTH_BUDGET_SEC:-150}"
log() { printf '[%s] boot %s: %s\n' "$(date -u +%FT%TZ)" "$SERVICE" "$*"; }
# pnpm and other tools write to $TMPDIR; on these containers it points under /tmp, which is emptied at boot.
[[ -n "${TMPDIR:-}" ]] && mkdir -p "$TMPDIR" && chmod 700 "$TMPDIR"
export PATH="$HOME/.cargo/bin:/usr/local/bin:$PATH"
# Observed 2026-10-19: a concurrent second run of this script raced `pm2 startOrReload` against a PM2 daemon
# that had just been spawned (empty process list) and lost the "is it already running" check, launching a
# duplicate photoreport-api that crash-looped forever on EADDRINUSE. Serialize with deploy.sh via the same lock
# so a boot racing a webhook-triggered deploy can't do the same thing.
exec 9>/run/photoreport-pm2.lock
flock 9
cd "$REPO_DIR"
SHA="$(git rev-parse HEAD)"
wait_for() {
local url="$1" start=$SECONDS
while (( SECONDS - start < HEALTH_BUDGET_SEC )); do
curl -fsS --max-time 3 -o /dev/null "$url" 2>/dev/null && return 0
sleep 2
done
return 1
}
# pm2 startOrReload has hung forever against a half-spawned daemon (pr-api, 2026-11-18): systemd then
# killed this unit at TimeoutStartSec and left the process list empty, so nothing was listening and nothing
# could report the outage. Bound the call; if it does not return, kill that daemon and try once more.
pm2_start() {
local config="$1"
if timeout 60s env APP_VERSION="$SHA" pm2 startOrReload "$config" --update-env; then
pm2 save --force >/dev/null
return 0
fi
log "pm2 startOrReload did not finish; resetting the daemon and retrying once"
pm2 kill >/dev/null 2>&1 || true
sleep 2
timeout 60s env APP_VERSION="$SHA" pm2 startOrReload "$config" --update-env
pm2 save --force >/dev/null
}
case "$SERVICE" in
api)
pm2_start infra/pm2/api.config.cjs
# The database may come up after this container: PM2 keeps retrying (exp_backoff_restart_delay), so a slow
# start is reported, not treated as fatal.
wait_for "http://127.0.0.1:${API_PORT}/health" && log "api up at $SHA" || log "WARNING: api not healthy yet (PM2 keeps retrying)"
;;
site)
if [[ -f apps/site/.env ]]; then set -a; source apps/site/.env; set +a; fi
pm2_start infra/pm2/site.config.cjs
wait_for "http://127.0.0.1:${SITE_PORT}/" && log "site up at $SHA" || log "WARNING: site not answering yet (PM2 keeps retrying)"
;;
app | ingest | workers | ml)
# Static files behind nginx (app) and SFTPGo (ingest) are plain systemd services, enabled at install.
log "nothing to start (systemd services)"
;;
*)
log "unknown service"; exit 2 ;;
esac