Add schedule force exit to SIGINT, SIGTERM, and unhandledRejection events
This commit is contained in:
parent
29e5b1ff97
commit
7dad3eb454
47
app.js
47
app.js
@ -123,7 +123,32 @@ process.on('SIGHUP', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
process.once('SIGTERM', function () {
|
addHandlers({ killTimeout: 45000 });
|
||||||
|
|
||||||
|
function addHandlers({ killTimeout }) {
|
||||||
|
// FIXME: minimize the number of 'uncaughtException' before uncomment the following line
|
||||||
|
// process.on('uncaughtException', exitProcess(listener, logger, killTimeout));
|
||||||
|
process.once('unhandledRejection', exitProcess(killTimeout));
|
||||||
|
process.once('SIGINT', exitProcess(killTimeout));
|
||||||
|
process.once('SIGTERM', exitProcess(killTimeout));
|
||||||
|
}
|
||||||
|
|
||||||
|
function exitProcess (killTimeout) {
|
||||||
|
return function exitProcessFn (signal) {
|
||||||
|
scheduleForcedExit(killTimeout, logger);
|
||||||
|
|
||||||
|
let code = 0;
|
||||||
|
|
||||||
|
if (!['SIGINT', 'SIGTERM'].includes(signal)) {
|
||||||
|
const err = signal instanceof Error ? signal : new Error(signal);
|
||||||
|
signal = undefined;
|
||||||
|
code = 1;
|
||||||
|
|
||||||
|
global.logger.fatal(err);
|
||||||
|
} else {
|
||||||
|
global.logger.info(`Process has received signal: ${signal}`);
|
||||||
|
}
|
||||||
|
|
||||||
listener.close(function () {
|
listener.close(function () {
|
||||||
server.batch.stop(function () {
|
server.batch.stop(function () {
|
||||||
server.batch.drain(function (err) {
|
server.batch.drain(function (err) {
|
||||||
@ -132,14 +157,28 @@ process.once('SIGTERM', function () {
|
|||||||
return process.exit(1);
|
return process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
global.logger.info('Exit gracefully');
|
global.logger.info(`Process is going to exit with code: ${code}`);
|
||||||
|
global.log4js.shutdown(function () {
|
||||||
server.batch.logger.end(function () {
|
server.batch.logger.end(function () {
|
||||||
process.exit(0);
|
process.exit(code);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function scheduleForcedExit (killTimeout) {
|
||||||
|
// Schedule exit if there is still ongoing work to deal with
|
||||||
|
const killTimer = setTimeout(() => {
|
||||||
|
global.logger.info('Process didn\'t close on time. Force exit');
|
||||||
|
process.exit(1);
|
||||||
|
}, killTimeout);
|
||||||
|
|
||||||
|
// Don't keep the process open just for this
|
||||||
|
killTimer.unref();
|
||||||
|
}
|
||||||
|
|
||||||
function isGteMinVersion(version, minVersion) {
|
function isGteMinVersion(version, minVersion) {
|
||||||
var versionMatch = /[a-z]?([0-9]*)/.exec(version);
|
var versionMatch = /[a-z]?([0-9]*)/.exec(version);
|
||||||
|
Loading…
Reference in New Issue
Block a user