Last active
May 3, 2018 16:35
-
-
Save tracker1/05d9b9a055f1a4c17d05156ceb127160 to your computer and use it in GitHub Desktop.
node process and cleanup
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
// logger - https://gist.github.com/tracker1/655be1ac690d23988816a48d52e91550 | |
import log from './logger'; | |
async function main() { | |
// TODO: main processes | |
} | |
let cleanedUp = false; | |
async function cleanup(error) { | |
if (error) log.fatal(error); | |
// only run through cleanup once | |
if (cleanedUp) return; | |
cleanedUp = true; | |
try { | |
// TODO: cleanup any outstanding resources | |
} catch (err) { | |
log.fatal(err); | |
} | |
// nextTick allows for buffered console.log statements to flush | |
process.nextTick(() => process.exit(error ? 666 : 0)); | |
} | |
process.on('unhandledRejection', cleanup); | |
process.on('uncaughtException', cleanup); | |
main().then(() => cleanup(), cleanup); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment