-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (39 loc) · 1.28 KB
/
index.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
49
50
'use strict';
const createHttpTerminator = require('lil-http-terminator');
function exit(timeout, fn, server) {
if (typeof (fn) !== 'function') {
return process.exit(0);
}
server.log('graceful-stop', `Running options.afterStop function with timeout ${timeout} ms`);
const id = setTimeout(() => {
server.log('graceful-stop', 'options.afterStop function timeout');
process.exit(0);
}, timeout);
fn(() => {
clearTimeout(id);
process.exit(0);
});
}
module.exports = {
name: 'graceful-stop',
register(server, options) {
const timeout = options.timeout || 5000;
const afterStopTimeout = options.afterStopTimeout || 2000;
const terminator = createHttpTerminator({
server: server.listener,
maxWaitTimeout: timeout,
gracefulTerminationTimeout: timeout * 0.9
});
async function shutdown(signal) {
server.log('graceful-stop', `Received ${signal}, initiating graceful stop with timeout ${timeout} ms`);
server.events.on('stop', () => {
exit(afterStopTimeout, options.afterStop, server);
});
await terminator.terminate();
await server.stop({timeout: afterStopTimeout});
server.log('graceful-stop', 'Server stopped');
}
process.once('SIGINT', async () => await shutdown('SIGINT'));
process.once('SIGTERM', async () => await shutdown('SIGTERM'));
}
};