feat: add daemon skeleton with lifecycle management

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
William Valentin
2026-02-02 20:56:45 -08:00
parent 4adf172c25
commit 70c3960527
4 changed files with 133 additions and 1 deletions
+34
View File
@@ -0,0 +1,34 @@
type ShutdownHandler = () => Promise<void>;
export class Lifecycle {
private shutdownHandlers: ShutdownHandler[] = [];
private shuttingDown = false;
private _isRunning = true;
get isRunning(): boolean {
return this._isRunning;
}
onShutdown(handler: ShutdownHandler): void {
this.shutdownHandlers.push(handler);
}
async shutdown(): Promise<void> {
if (this.shuttingDown) return;
this.shuttingDown = true;
this._isRunning = false;
console.log('Shutting down...');
// Execute handlers in reverse order (LIFO)
for (const handler of [...this.shutdownHandlers].reverse()) {
try {
await handler();
} catch (error) {
console.error('Shutdown handler error:', error);
}
}
console.log('Shutdown complete');
}
}