You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
|
|
|
|
var exitHandlers: (() => void)[] = []; |
|
var exitHandled = false; |
|
|
|
/** |
|
* Add a callback for closing programm bacause of any reason |
|
* |
|
* @param callback a sync function |
|
*/ |
|
export function registerExitHandler(callback: () => void) { |
|
exitHandlers.push(callback); |
|
} |
|
|
|
function exitHandler(options: any, err?: any) { |
|
if(exitHandled) { |
|
return; |
|
} |
|
exitHandled = true; |
|
for(let i = 0; i < exitHandlers.length; i++) { |
|
exitHandlers[i](); |
|
} |
|
console.log('process exit'); |
|
process.exit(); |
|
} |
|
|
|
function catchException(options: any, err: any) { |
|
console.log('Server exception:'); |
|
console.log(err); |
|
exitHandler({ exit: true }); |
|
} |
|
|
|
//do something when app is closing |
|
process.on('exit', exitHandler.bind(null, { cleanup:true })); |
|
|
|
//catches ctrl+c event |
|
process.on('SIGINT', exitHandler.bind(null, { exit:true })); |
|
|
|
// catches "kill pid" (for example: nodemon restart) |
|
process.on('SIGUSR1', exitHandler.bind(null, { exit:true })); |
|
process.on('SIGUSR2', exitHandler.bind(null, { exit:true })); |
|
|
|
//catches uncaught exceptions |
|
process.on('uncaughtException', catchException.bind(null, { exit:true })); |