photoreport-boot.service runs infra/deploy/boot.sh at boot (starts the PM2 apps from the last built code, without depending on a saved PM2 list); tmpfiles rule for TMPDIR; deploys pm2 save; PM2 retries with exponential backoff while the database starts. Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
56 lines
2.4 KiB
Bash
Executable File
56 lines
2.4 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"
|
|
|
|
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
|