Files
photoreport/infra/deploy/boot.sh
T
spark 4eac0e6f95
ci / rust (push) Successful in 3m51s
ci / typescript (push) Successful in 7m22s
fix(infra): serialize pm2 startOrReload between boot.sh and deploy.sh
Live incident 2026-10-19: node09 rebooted and photoreport-boot.service ran
twice concurrently against a freshly-spawned, still-empty PM2 daemon. Both
invocations saw "not running" and launched photoreport-api, creating a
duplicate that crash-looped forever on EADDRINUSE (1700+ restarts by the
time it was caught, silently -- the original instance kept serving so
/health never flagged it). Deleted the ghost process and re-saved live.

Wrap the pm2 startOrReload calls in both scripts with the same flock so a
boot racing a webhook-triggered deploy (or a double-invoked boot unit)
serializes instead of racing the "is it running" check.

Paperclip-Issue: BIT-31073
Paperclip-Run: 9c6946f6-df0c-4544-97ac-6b00a2de8bd1
2026-10-19 10:05:23 +01:00

63 lines
2.8 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}"
HEALTH_TRIES="${BOOT_HEALTH_TRIES:-90}"
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" i
for ((i = 1; i <= HEALTH_TRIES; i++)); do
curl -fsS --max-time 3 -o /dev/null "$url" 2>/dev/null && return 0
sleep 2
done
return 1
}
case "$SERVICE" in
api)
APP_VERSION="$SHA" pm2 startOrReload infra/pm2/api.config.cjs --update-env
pm2 save --force >/dev/null
# 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
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