[bug] make shutdown reliable

This commit is contained in:
Sosthene 2025-08-24 15:39:42 +02:00
parent afe12860cf
commit 45a1478210

View File

@ -377,25 +377,50 @@ export class Server {
public shutdown() {
console.log('🛑 Shutting down server...');
// Close all active client connections first
for (const [ws, clientId] of this.clients.entries()) {
console.log(`🔌 Closing connection to ${clientId}...`);
ws.close(1000, 'Server shutting down');
}
this.clients.clear();
// Close the WebSocket server
this.wss.close(() => {
console.log('✅ Server shutdown complete');
process.exit(0);
});
// Force exit after a timeout if graceful shutdown fails
setTimeout(() => {
console.log('⚠️ Force shutdown after timeout');
process.exit(1);
}, 5000);
}
}
// Handle graceful shutdown
let isShuttingDown = false;
process.on('SIGINT', () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\n🛑 Received SIGINT, shutting down gracefully...');
if (server) {
server.shutdown();
} else {
process.exit(0);
}
});
process.on('SIGTERM', () => {
if (isShuttingDown) return;
isShuttingDown = true;
console.log('\n🛑 Received SIGTERM, shutting down gracefully...');
if (server) {
server.shutdown();
} else {
process.exit(0);
}
});