fix: graceful shutdown — write last-run file on SIGTERM, show interrupted state in status
This commit is contained in:
20
lib/lock.mjs
20
lib/lock.mjs
@@ -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) };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user