Gracefully handle your modular NodeJS application's shutdown (termination).
It doesn't matter in which order and where you define these graceful handlers, node-graceful-shutdown
will
handle them appropriately and exit the process once all of them are processed.
import { onShutdown } from "node-graceful-shutdown";
// module1.js
onShutdown("http-server", async function () {
// Stop your http server here.
});
// module2.js
onShutdown("message-bus", ["http-server"], async function () {
// Close your RabbitMQ connection here ONLY AFTER http server's onShutdown completed.
});
// moduleX.js
onShutdown("database", ["http-server", "message-bus"], async function () {
// Shut down your database here, ONLY AFTER http-server and message-bus are completed.
});
// After all handlers are processed without errors, process exits with code 0. Otherwise it exits with code 42759.
This library, along existing ones, allow your application to be modular. You define a cleanup callback in-place, in the same module, where initialization happens.
Recommendations:
- Please do not use this module in libraries (modules, packages). Use for the end application only.
- Once imported,
onShutdown
is application-wide, so the callbacks and their dependencies will see each other when required from the samenode_modules
folder. - Circular dependencies will throw an error immediately once declared.
- There's also an
onShutdownError
export which takes an error inside handlers handler (added for very-very prudent programmers only). - Importing this module deletes existing handlers (
SIGINT
,SIGTERM
,SIGQUIT
) if there are any (normally, there should be no other handlers).