Skip to content

Commit

Permalink
graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
SAINIAbhishek committed Oct 13, 2024
1 parent 94522df commit 041b52d
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@ import app from './app';
import Logger from './middleware/Logger';
import { PORT } from './config';

app
.listen(PORT, () => {
Logger.info(`Server running on port : ${PORT}`);
})
.on('error', (e) => Logger.error(e));
const startServer = () => {
const server = app.listen(PORT, () => {
Logger.info(`Server running on port: ${PORT}`);
});

// Handle server errors
server.on('error', (error) => {
Logger.error(`Server error: ${error.message}`);
});

// Graceful shutdown on termination signals
const shutdown = (signal: string) => {
Logger.info(`Received ${signal}. Closing HTTP server.`);
server.close((err) => {
if (err) {
Logger.error(`Error closing server: ${err.message}`);
} else {
Logger.info('HTTP server closed successfully.');
}
process.exit(err ? 1 : 0); // Exit with error code if there's an error
});
};

// Listen for termination signals
process.on('SIGINT', () => shutdown('SIGINT'));
process.on('SIGTERM', () => shutdown('SIGTERM'));
};

// Start the server
startServer();

0 comments on commit 041b52d

Please sign in to comment.