What is node-cleanup?
The node-cleanup package allows you to handle cleanup tasks when your Node.js process is exiting. This can be useful for tasks such as closing database connections, clearing temporary files, or other necessary cleanup operations.
What are node-cleanup's main functionalities?
Basic Cleanup Handler
This feature allows you to register a basic cleanup handler that will be called when the Node.js process is exiting. You can perform any necessary cleanup tasks within this handler.
const nodeCleanup = require('node-cleanup');
nodeCleanup(function (exitCode, signal) {
console.log('Cleaning up before exit...');
// Perform cleanup tasks here
});
Custom Exit Code
This feature allows you to customize the exit code of the process. By returning false from the cleanup handler, you can prevent the default exit code from being used.
const nodeCleanup = require('node-cleanup');
nodeCleanup(function (exitCode, signal) {
console.log('Cleaning up before exit...');
// Perform cleanup tasks here
return false; // Prevents the default exit code
});
Signal Handling
This feature allows you to handle specific signals (e.g., SIGINT, SIGTERM) and perform cleanup tasks accordingly. You can also re-emit the signal if needed.
const nodeCleanup = require('node-cleanup');
nodeCleanup(function (exitCode, signal) {
if (signal) {
console.log(`Received signal: ${signal}`);
// Perform cleanup tasks here
process.kill(process.pid, signal);
return false;
}
});
Other packages similar to node-cleanup
graceful-shutdown
The graceful-shutdown package provides a way to gracefully shut down your Node.js application. It allows you to register shutdown handlers that will be called when the process is exiting. Compared to node-cleanup, it offers more advanced features such as handling multiple shutdown handlers and providing a timeout for the shutdown process.
signal-exit
The signal-exit package listens for exit signals and calls a callback when the process is exiting. It supports various exit signals and provides a simple API for registering exit handlers. Compared to node-cleanup, it is more lightweight and focuses solely on handling exit signals.
exit-hook
The exit-hook package allows you to register hooks that will be called when the Node.js process is exiting. It provides a straightforward API for adding and removing hooks. Compared to node-cleanup, it is simpler and does not offer advanced features like signal handling.
node-cleanup
installs a cleanup handler that always runs on exiting node
Installation
npm install node-cleanup --save
Usage
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.
var nodeCleanup = require('node-cleanup');
nodeCleanup(function () {
});
nodeCleanup() also ensures that ctrl-C is handled gracefully in contexts that already have exit handlers installed, such as Node Express. To receive just this benefit, the caller need not provide a cleanup handler.
By default, nodeCleanup()
writes "[ctrl-C]" to stderr
when interrupted and "Uncaught exception..." to stderr
when an uncaught exception occurs. You may override either or both of these values in a second parameter:
var nodeCleanup = require('node-cleanup');
nodeCleanup(function () {
}, { ctrl_C: '^C' });
Reference
function nodeCleanup(cleanupHandler, messages)
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. |
messages | An optional object mapping any of the keys ctrl_C and uncaughtException to the message strings that output to stderr. Set a message to the empty string '' to prevent output to stderr for its case. Default messages are provided omitted messages. |
Credit
This code was borrowed and modified from CanyonCasa's answer to a stackoverflow question. I found the code necessary for all my node projects. See the stackoverflow answer for more examples of use.