Skip to content

Commit 119a4e9

Browse files
Fix self-update hanging at the health-check step
Root cause: the updater was spawned as a child of the API process. PM2 restarts a process by tree-killing its descendants, so when the script ran 'pm2 reload', the API restart killed the script mid health-check — freezing the status at 'Redémarrage et vérification de l'état…' forever. - version.ts: launch the updater from a throwaway 'bash -c' that backgrounds it and exits immediately, so it is reparented to init (ppid 1) and detached via setsid. The reload can no longer reap it. - self-update.sh: reload only opencoperlock-api and opencoperlock-web by name (graceful, with a restart fallback) instead of the whole ecosystem — never touching a project-local Postgres, and fixing the case where only the web process was being restarted.
1 parent 1883079 commit 119a4e9

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎apps/api/src/services/version.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,16 @@ export function startUpdate(env: Env): StartUpdateResult {
183183
}
184184
}
185185

186-
const child = spawn('bash', [script], {
186+
// Spawn the updater so it OUTLIVES the PM2 reload it will trigger. PM2 restarts a process by
187+
// tree-killing its descendants, so the script must NOT stay a child of this API process —
188+
// otherwise reloading the API kills the updater mid-flight (freezing the status at "verifying").
189+
// We launch it from a throwaway `bash -c` that backgrounds the real script and exits at once;
190+
// once that shell exits the script is reparented to init (ppid 1), and `setsid` detaches it
191+
// from our session/process-group, so the reload can no longer reap it.
192+
const q = (s: string) => `'${s.split("'").join("'\\''")}'`;
193+
const run = `bash ${q(script)} </dev/null >>${q(join(repoRoot, '.update.log'))} 2>&1 &`;
194+
const launch = `if command -v setsid >/dev/null 2>&1; then setsid ${run} else ${run} fi`;
195+
const child = spawn('bash', ['-c', launch], {
187196
cwd: repoRoot,
188197
detached: true,
189198
stdio: 'ignore',

‎scripts/self-update.sh‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,23 @@ API_PORT="$(read_env API_PORT)"; : "${API_PORT:=4000}"
4343
HEALTH_URL="http://127.0.0.1:${API_PORT}/health"
4444

4545
reload_pm2() {
46-
if command -v pm2 >/dev/null 2>&1; then
47-
{ pm2 reload "$ROOT/ecosystem.config.cjs"; } >> "$LOG" 2>&1 || echo "pm2 reload warning" >> "$LOG"
48-
else
46+
if ! command -v pm2 >/dev/null 2>&1; then
4947
echo "pm2 not found on PATH — reload skipped; restart the processes manually." >> "$LOG"
48+
return
5049
fi
50+
# Reload ONLY our application processes, by name — never a project-local Postgres, which must
51+
# not be restarted by an app update. `reload` is graceful; fall back to `restart` if it can't
52+
# be reloaded. Each process is best-effort so one missing name doesn't abort the update.
53+
local app
54+
for app in opencoperlock-api opencoperlock-web; do
55+
if pm2 describe "$app" >/dev/null 2>&1; then
56+
echo "reloading $app" >> "$LOG"
57+
{ pm2 reload "$app" --update-env || pm2 restart "$app" --update-env; } >> "$LOG" 2>&1 \
58+
|| echo "warn: could not reload $app" >> "$LOG"
59+
else
60+
echo "note: pm2 process '$app' not found — skipped" >> "$LOG"
61+
fi
62+
done
5163
}
5264

5365
# True once the API answers its /health endpoint. Tries curl, then wget, then a raw TCP probe.

0 commit comments

Comments
 (0)