-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraceful-shutdown.js
48 lines (41 loc) · 1.41 KB
/
graceful-shutdown.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Gracefully shutdown the application when requested, rejecting any requests that come through.
*
* Based entirely from express-graceful-shutdown:
* @link https://npm.im/express-graceful-shutdown
*/
module.exports = function createShutdownMiddleware(server, opts = {}) {
const logger = opts.logger || console; // Defaults to console
const forceTimeout = typeof opts.forceTimeout === 'number' ? opts.forceTimeout : (30 * 1000); // Defaults to 30s
let shuttingDown = false;
process.on('SIGTERM', function gracefulExit() {
if (shuttingDown) {
// We already know we're shutting down, don't continue this function
return;
} else {
shuttingDown = true;
}
// Don't bother with graceful shutdown in development
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
return process.exit(0);
}
logger.warn('Received kill signal (SIGTERM), shutting down...');
setTimeout(() => {
logger.error('Could not close connections in time, forcefully shutting down');
process.exit(1);
}, forceTimeout);
server.close(() => {
logger.info('Closed out remaining connections');
process.exit(0);
});
});
return function shutdown(ctx, next) {
if (shuttingDown) {
ctx.status = 503;
ctx.set('Connection', 'close');
ctx.body = 'Server is in the process of shutting down';
} else {
return next();
}
};
};