Gracefully clean up your program at termination with multiple sync and/or async handlers. Wraps the <code>death</code> npm module.
Diehard is a node.js module that helps you gracefully clean up your program at termination with multiple sync and/or async handlers. Wraps the
death npm module.
death is an incredibly useful module that abstracts out the need to handle the various kinds of termination events, but ultimately only supports a single handler. I've found that in more complex projects you often have multiple loose ends to clean up, and I desired a solution that would allow me to properly isolate the responsibility for cleaning up each individual resource into dedicated handlers that would are run in parallel.
diehard solves that problem.
$ npm install diehard
var diehard = require'diehard';setIntervalconsole.log'Blah blah blah.'; // keeps running until we CTRL+C250;diehardregister//TODO: clean up some resource// this is a synchronous handler;diehardregister//TODO: clean up some resourcedone; // async, ftw!;diehardregister//TODO: clean up some resource// we also have the `signal` that terminated the process here, in case we caredone;;diehardregister//TODO: clean up some resource// if an uncaught error was the reason the process is terminating, we can access that, toodone;;diehardlisten;
In the above example, all five termination handlers will be run (in parallel) before the process exits.