Skip to content

Instantly share code, notes, and snippets.

@tracker1
Last active May 3, 2018 16:35
Show Gist options
  • Save tracker1/05d9b9a055f1a4c17d05156ceb127160 to your computer and use it in GitHub Desktop.
Save tracker1/05d9b9a055f1a4c17d05156ceb127160 to your computer and use it in GitHub Desktop.
node process and cleanup
// 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