Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gracefulShutdown from './tools/graceful-shutdown';
- const server = app.listen(config.port, () => {
- console.log(`Server is running`);
- });
- app.use(gracefulShutdown(server));
- function gracefulShutdownMiddleware(server) {
- let shuttingDown = false;
- const forceTimeout = 10 * 1000; // giving the app 10 seconds to shutdown gracefully
- process.on('SIGTERM', gracefulExit); // listen for TERM signal (e.g. kill)
- process.on ('SIGINT', gracefulExit); // listen for INT signal (e.g. Ctrl-C)
- function gracefulExit() {
- if (shuttingDown) return;
- shuttingDown = true;
- console.log('Received kill signal (SIGTERM), shutting down');
- setTimeout(function () {
- console.log('Could not close connections in time, forcefully shutting down');
- process.exit(1);
- }, forceTimeout);
- server.close(function () {
- console.log('Closed out remaining connections.');
- process.exit();
- });
- }
- function middleware(req, res, next) {
- if (!shuttingDown) return next()
- res.set('Connection', 'close')
- res.status(503).send('Server is in the process of restarting.')
- }
- return middleware
- }
- export default gracefulShutdownMiddleware;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement