From c9c994e0a0d2f83d3570823aed7fb2185688c323 Mon Sep 17 00:00:00 2001 From: spark Date: Thu, 19 Nov 2026 00:11:50 +0000 Subject: [PATCH] fix(boot): retry a hung PM2 start so a reboot cannot leave the API down pm2 startOrReload hung on pr-api after the 2026-11-18 reboot. systemd killed photoreport-boot and the process list stayed empty, so nothing could report the outage. Bound the call, reset the daemon once, and retry the unit. --- infra/deploy/boot.sh | 22 ++++++++++++++++++---- infra/deploy/deploy.sh | 18 ++++++++++++++++-- infra/systemd/photoreport-boot.service | 7 ++++++- 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/infra/deploy/boot.sh b/infra/deploy/boot.sh index 98a294d..22b32f7 100755 --- a/infra/deploy/boot.sh +++ b/infra/deploy/boot.sh @@ -39,18 +39,32 @@ wait_for() { 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) - APP_VERSION="$SHA" pm2 startOrReload infra/pm2/api.config.cjs --update-env - pm2 save --force >/dev/null + 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 - APP_VERSION="$SHA" pm2 startOrReload infra/pm2/site.config.cjs --update-env - pm2 save --force >/dev/null + 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) diff --git a/infra/deploy/deploy.sh b/infra/deploy/deploy.sh index 2b4a747..d54d829 100755 --- a/infra/deploy/deploy.sh +++ b/infra/deploy/deploy.sh @@ -45,6 +45,20 @@ wait_for_version() { return 1 } +# pm2 startOrReload has hung forever against a half-spawned daemon (pr-api, 2026-11-18). +# Bound it; if it does not return, kill that daemon and try once more. +pm2_reload() { + local sha="$1" + local config="$2" + if timeout 90s env APP_VERSION="$sha" pm2 startOrReload "$config" --update-env; then + 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 90s env APP_VERSION="$sha" pm2 startOrReload "$config" --update-env +} + deploy_api() { local sha="$1" node_install @@ -65,7 +79,7 @@ deploy_api() { # Same lock as boot.sh: a reboot racing this deploy's pm2 call against a freshly-spawned, still-empty PM2 # daemon duplicated photoreport-api on 2026-10-19 (crash-looped on EADDRINUSE forever). Serialize instead. ( exec 9>/run/photoreport-pm2.lock; flock 9 - APP_VERSION="$sha" pm2 startOrReload infra/pm2/api.config.cjs --update-env + pm2_reload "$sha" infra/pm2/api.config.cjs ) wait_for_version "http://127.0.0.1:${API_PORT}/health" "$sha" # The process list PM2 restores after a reboot follows every deploy (photoreport-boot.service starts it too). @@ -151,7 +165,7 @@ deploy_site() { # class as the prisma CLI fix above, so a host's API_URL override actually reaches pm2. if [[ -f apps/site/.env ]]; then set -a; source apps/site/.env; set +a; fi ( exec 9>/run/photoreport-pm2.lock; flock 9 - APP_VERSION="$sha" pm2 startOrReload infra/pm2/site.config.cjs --update-env + pm2_reload "$sha" infra/pm2/site.config.cjs ) local i for ((i = 1; i <= HEALTH_TRIES; i++)); do diff --git a/infra/systemd/photoreport-boot.service b/infra/systemd/photoreport-boot.service index 7b5f385..5ee46bb 100644 --- a/infra/systemd/photoreport-boot.service +++ b/infra/systemd/photoreport-boot.service @@ -11,7 +11,12 @@ RemainAfterExit=yes Environment=HOME=/root EnvironmentFile=/etc/photoreport/deploy-receiver.env ExecStart=/bin/bash /srv/photoreport/infra/deploy/boot.sh -TimeoutStartSec=300 +TimeoutStartSec=420 +# A hung PM2 leaves this oneshot failed and the port closed. Retry instead of staying down until someone notices. +Restart=on-failure +RestartSec=30 +StartLimitIntervalSec=900 +StartLimitBurst=5 [Install] WantedBy=multi-user.target -- 2.47.3