Skip to content

Commit

Permalink
```
Browse files Browse the repository at this point in the history
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.
```
  • Loading branch information
fine-agent committed Feb 10, 2025
1 parent bc03a25 commit b900c5c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit b900c5c

Please sign in to comment.