feat: add graceful shutdown handling to WorkerService
This commit is contained in:
@@ -250,6 +250,7 @@ export class WorkerService {
|
||||
// Initialization flags for MCP/SDK readiness tracking
|
||||
private mcpReady: boolean = false;
|
||||
private initializationCompleteFlag: boolean = false;
|
||||
private isShuttingDown: boolean = false;
|
||||
|
||||
// Service layer
|
||||
private dbManager: DatabaseManager;
|
||||
@@ -595,6 +596,27 @@ export class WorkerService {
|
||||
|
||||
logger.info('SYSTEM', 'Worker started', { host, port, pid: process.pid });
|
||||
|
||||
// Register signal handlers to ensure cleanup on exit
|
||||
const handleShutdown = async (signal: string) => {
|
||||
if (this.isShuttingDown) {
|
||||
logger.warn('SYSTEM', `Received ${signal} but shutdown already in progress`);
|
||||
return;
|
||||
}
|
||||
this.isShuttingDown = true;
|
||||
|
||||
logger.info('SYSTEM', `Received ${signal}, shutting down...`);
|
||||
try {
|
||||
await this.shutdown();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
logger.error('SYSTEM', 'Error during shutdown', {}, error as Error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
process.on('SIGTERM', () => handleShutdown('SIGTERM'));
|
||||
process.on('SIGINT', () => handleShutdown('SIGINT'));
|
||||
|
||||
// Do slow initialization in background (non-blocking)
|
||||
this.initializeBackground().catch((error) => {
|
||||
logger.error('SYSTEM', 'Background initialization failed', {}, error as Error);
|
||||
|
||||
Reference in New Issue
Block a user