
Security News
Axios Maintainer Confirms Social Engineering Attack Behind npm Compromise
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.
node-shutdown-manager
Advanced tools
The node shutdown manager manages a chain of functions to be executed when the application is shutting down. This manager allows both synchronous and asynchronous functions to be added, and only complete the shutdown process once the functions have finished executing.
This can be extremely helpful if you need to perform a few actions to cleanup your application before exiting.
npm install node-shutdown-manager
You can create an instance of the shutdown manager by calling the createShutdownManager method.
var shutdownManager = require('node-shutdown-manager').createShutdownManager();
Once created, you can add a method to the shutdown action chain by simply calling the addShutdownAction method. This method will accept a function. The function must not require any arguments.
manager.addShutdownAction(sampleFunction);
Additionally you can use the addFinalShutdownAction method. This method is identical to addShutdownAction BUT it these methods are executed only after all the addShutdownAction promises are fulfilled. This allows you to delay shutdown of some components (such as loggers) so that they are available until all the components shutdown.
You can add asynchronous functions to the action chain as long as they return a promise (that conforms to CommonJS Promises/A). I would recommend using Q if you are looking for a library that supports the promise spec.
var async1Function = function() {
var deferred = q.defer();
setTimeout(function() {
console.log("Async Action 1 Completed");
deferred.resolve();
}, 1000);
return deferred.promise;
};
manager.addShutdownAction(async1Function);
One aspect of customization with the manager is how it handles logging. You can pass in any object as a custom logger as long as it has a log function with accepts the log message as an argument. In addition, you can pass in a custom logging prefix (there is one used by default).
var shutdownManager = require('node-shutdown-manager').createShutdownManager({
logger: customLogger,
loggingPrefix: "[Custom Logging Prefix]: "
});
Set a configuration setting of 'timeout' in milliseconds to limit how long the manager waits for promises to be fulfilled.
var shutdownManager = require('node-shutdown-manager').createShutdownManager({
timeout: 10000 // Limit async shutdown promises to 10 seconds to execute
});
Event 'preShutdown' calls a method with the arguments (reason, err). If no listener is specified, the default action is to log the received event. Reason will be SIGINT, TERM, exit, or uncaughtException.
Event 'shutdownActionException' is emitted if an exception is thrown out of a shutdown action. Function is passed (err, method). If no listener is specified, the default action is to log the received event.
Event 'shutdownComplete' is emitted once the shutdown actions are all executed. Function is passed (exitCode, errors). If no listener is specified, the default action is to log and call process.exit. WARNING: If you register a listener to this event, you are responsible for calling Process.exit() This allows the caller to alter exit codes or other details about the exit.
var shutdownManager = require('node-shutdown-manager').createShutdownManager({});
shutdownManager.on('preShutdown', function( reason, err) {
log.console("Shutting down for: "+reason);
log.console("Error: "+err.stack || err);
});
shutdownManager.on('shutdownActionException', function(err, method) {
log.console("Error in shutdown action being ignored:");
log.console("Error: "+err.stack || err);
});
shutdownManager.on('shutdownComplete', function(err, method) {
log.console("Shutdown actions complete");
log.console("Error: "+err.stack || err);
process.exit(0); // For example, if you never want to exit with an error code
});
The manager detects the following situations:
It is important to know that other situations will cause the manager not to function. For example, if your application is force closed (by receiving the SIGKILL signal), then the shutdown manager will not be executed.
Copyright 2013 David Tucker All rights reserved.
The software is licensed under an MIT License:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
performs a chain of actions when a shutdown is detected
We found that node-shutdown-manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.