fix: graceful shutdown — write last-run file on SIGTERM, show interrupted state in status

This commit is contained in:
2026-03-06 00:35:10 +00:00
parent 1920df51a4
commit b091473735
4 changed files with 48 additions and 19 deletions

View File

@@ -26,9 +26,21 @@ export function acquireLock(name, dataDir) {
const release = () => {
try { unlinkSync(lockFile); } catch {}
};
process.on('exit', release);
process.on('SIGINT', () => { release(); process.exit(130); });
process.on('SIGTERM', () => { release(); process.exit(143); });
return release;
// Graceful shutdown — call registered cleanup before exiting
const shutdownHandlers = [];
const shutdown = (code) => async () => {
console.log(`\n⚠️ ${name}: signal received, shutting down gracefully...`);
for (const fn of shutdownHandlers) {
try { await fn(); } catch {}
}
release();
process.exit(code);
};
process.on('exit', release);
process.on('SIGINT', shutdown(130));
process.on('SIGTERM', shutdown(143));
return { release, onShutdown: (fn) => shutdownHandlers.push(fn) };
}