Skip to content

Commit

Permalink
Can now install multiple cleanup handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
jtlapp committed Sep 27, 2016
1 parent 221d6ea commit 3308e39
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ npm install node-cleanup --save

nodeCleanup() installs a function that performs cleanup activities just before the node process exits. The cleanup function runs when the process exits normally, when the user presses *ctrl-C*, and when an exception is uncaught. The caller may specify the termination messages to use.

You may call nodeCleanup() multiple times to install multiple cleanup handlers, but only the messages provided with the first call get used.

```js
var nodeCleanup = require('node-cleanup');

Expand All @@ -38,6 +40,8 @@ nodeCleanup(function () {

Install a cleanup handler that reliably runs when node exits. Both parameters are optional. Calling `nodeCleanup()` without a `cleanupHandler` still provides the benefit of ensuring that other installed exit handlers run on *ctrl-C*.

Call this function multiple times to install multiple cleanup handlers. Only the messages provided with the first call are used.

| Param | Description |
| --- | --- |
| cleanupHandler | A function that performs the final cleanup of resources before the node process exits. The function may write to `stderr` and `stdout`. It takes no parameters and can't abort the exit. The handler is optional, defaulting to a function that does nothing. |
Expand Down
26 changes: 20 additions & 6 deletions node-cleanup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,37 @@
* exits normally, when the user presses ctrl-C, and when an exception is
* uncaught. The caller may specify the termination messages to use.
*
* Call this function multiple times to install multiple cleanup handlers.
* Only the messages provided with the first call are used.
*
* See https://github.com/jtlapp/node-cleanup for more information. Code
* largely borrowed from http://stackoverflow.com/a/21947851/650894.
*/

var installed = false;

module.exports = function nodeCleanup(cleanupHandler, messages) {

// attach user callback to the process event emitter.
// if no callback, it will still exit gracefully on Ctrl-C
cleanupHandler = cleanupHandler || noOp;
process.on('cleanup', cleanupHandler);

// only install the termination handlers once
if (!installed) {
install(messages);
installed = true;
}
};

function install(messages) {

messages = messages || {};
if (typeof messages.ctrl_C !== 'string')
messages.ctrl_C = '[ctrl-C]';
if (typeof messages.uncaughtException !== 'string')
messages.uncaughtException = 'Uncaught exception...';

// attach user callback to the process event emitter.
// if no callback, it will still exit gracefully on Ctrl-C
cleanupHandler = cleanupHandler || noOp;
process.on('cleanup', cleanupHandler);

// do app-specific cleaning before exiting
process.on('exit', function () {
process.emit('cleanup');
Expand All @@ -41,6 +55,6 @@ module.exports = function nodeCleanup(cleanupHandler, messages) {
}
process.exit(99);
});
};
}

function noOp() {}; // for just the benefit of graceful SIGINTs

0 comments on commit 3308e39

Please sign in to comment.