From b900c5c718eabf6d7faf7c89e25b6256f5736f14 Mon Sep 17 00:00:00 2001 From: fine-agent Date: Mon, 10 Feb 2025 14:46:17 +0000 Subject: [PATCH] ``` Implement graceful server shutdown in server.js - Replaced the existing shutdown function with a more robust implementation. - The new shutdown function: 1. Waits for the server to close existing connections before exiting. 2. Implements a 10-second timeout to force shutdown if the server takes too long. 3. Uses `process.exitCode` to set the exit code instead of calling `process.exit()` directly. - This ensures a cleaner and more predictable shutdown process. ``` --- server.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index e84121b..c638cf5 100644 --- a/server.js +++ b/server.js @@ -88,8 +88,18 @@ process.on('SIGTERM', shutdown); function shutdown() { console.log('SIGTERM signal received: closing HTTP server'); - server.close(); - process.exit(0); + server.close(() => { + console.log('HTTP server closed'); + + // Set exit code instead of calling exit directly + process.exitCode = 0; + }); + + // Add timeout to force exit if graceful shutdown takes too long + setTimeout(() => { + console.log('Forcing shutdown after timeout'); + process.exit(1); + }, 10000).unref(); } server.listen({