Last active
January 3, 2018 13:27
-
-
Save aGuyNamedJonas/9cdf8cc4a92aeee2e7cf478ea6154983 to your computer and use it in GitHub Desktop.
Register cleanup procedures in node.js (stolen from: http://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits)
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 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