node-graceful-shutdown1.0.3 • Public • Published
Gracefully handle your modular NodeJS application's shutdown (termination), using dependencies.
It doesn't matter in which order and where you define these graceful handlers,
handle them appropriately and exit the process once all of them are processed.
;// module1.js;// module2.js;// moduleX.js;// After all handlers are processed without errors, process exits with code 0.// Otherwise it exits with exit code 42759, or exit code 42758 if there are any errors in assigned shutdown handlers.// If some of specified dependencies are not defined (like when "http-server" is missing in the above example),// node-graceful-shutdown will run the current handler without waiting for the undefined dependency.
Features and Guidelines
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. In addition, it allows specifying the order
- Please, do not use this module in libraries (modules, packages). Use for the end application only.
- Once imported,
onShutdownis application-wide (in terms of a single process), so the callbacks and their dependencies will see each other when imported from multiple files.
- Circular shutdown handler dependencies will throw an error immediately once declared.
- There's also an
onShutdownErrorexport which takes an error as an argument when any of assigned shutdown handlers throw an error (added for very-very prudent programmers only).
- Importing this module deletes existing handlers (
SIGQUIT) if there are any (normally, there should be no other handlers).
- You may also consider defining constants