Created
November 21, 2018 10:11
-
-
Save Sv443/1d56de5ff9f8c214990f78325282f824 to your computer and use it in GitHub Desktop.
NodeJS soft shutdown
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
// not compatible with ES5, only ES6 and up | |
process.on('SIGINT', ()=>{ | |
try { | |
// end your processes here, for example | |
// http_connection.close(); | |
// or | |
// mysql_connection.end(); | |
process.exit(); // <-- this ends the whole script normally (without throwing an error) | |
} | |
catch(err) { | |
// this crashes the script if the processes couldn't be shut down in the try{} block | |
console.log("\x1b[31m\x1b[1m Couldn't shut down all processes due to error: '" + err + "', crashing script instead.\x1b[0m"); | |
process.exit(1); // <-- this ends the whole script and uses the exit code 1, which is an error | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment