Created
June 7, 2018 14:12
-
-
Save miguel-amaral/2360e7fc509e09b0cbdc2c3bc00356ec to your computer and use it in GitHub Desktop.
Example of dealing with the close of a nodejs process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
process.stdin.resume();//so the program will not close instantly | |
function exitHandler(options, err) { | |
if (options.cleanup) console.log('clean'); | |
if (err) console.log(err.stack); | |
if (options.exit) process.exit(); | |
} | |
//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', exitHandler.bind(null, {exit:true})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment