All Projects → ZitRos → node-graceful-shutdown

ZitRos / node-graceful-shutdown

Licence: MIT license
Gracefully shutdown your modular NodeJS application.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to node-graceful-shutdown

hiatus-spring-boot
No description or website provided.
Stars: ✭ 23 (+15%)
Mutual labels:  shutdown, graceful-shutdown
fine
🧹 Gracefully shutdown Node.js application: help you handle exit signals and cleanup
Stars: ✭ 20 (+0%)
Mutual labels:  shutdown, graceful
sigctx
Go contexts for graceful shutdown
Stars: ✭ 55 (+175%)
Mutual labels:  graceful-shutdown, graceful
co demo
A step-by-step guide about how to avoid callback hell with ES6 Promises + generators (aka make your own "co")
Stars: ✭ 17 (-15%)
Mutual labels:  error-handling
koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (+155%)
Mutual labels:  error-handling
raygun4py
Python provider for Raygun
Stars: ✭ 18 (-10%)
Mutual labels:  error-handling
kotlin-multiplatform-example
A Kotlin multiplatform example app that targets Android, ReactJS, iOS, JavaFx, and Spring Boot
Stars: ✭ 115 (+475%)
Mutual labels:  error-handling
SolveWithStack
Android library for helping you to reach out to best possible answer for your bug/error available on stack overflow and will show it in your Android Studio Console only.
Stars: ✭ 15 (-25%)
Mutual labels:  error-handling
async armor
Graceful drop-in replacement for asyncio.shield
Stars: ✭ 15 (-25%)
Mutual labels:  graceful
kms
🔪 Is a library that aids in graceful shutdown of a process/application
Stars: ✭ 44 (+120%)
Mutual labels:  graceful-shutdown
fortran-error-handler
Comprehensive error framework for applications requiring functional and robust error handling, utilising the power of modern object-oriented Fortran.
Stars: ✭ 19 (-5%)
Mutual labels:  error-handling
raygun4android
Android crash reporting provider for Raygun
Stars: ✭ 19 (-5%)
Mutual labels:  error-handling
elmah.io
ELMAH error logger for sending errors to elmah.io.
Stars: ✭ 31 (+55%)
Mutual labels:  error-handling
waitabit
😴 A tiny library for handling system interrupts
Stars: ✭ 38 (+90%)
Mutual labels:  shutdown
auto-async-wrap
automatic async middleware wrapper for expressjs errorhandler.
Stars: ✭ 21 (+5%)
Mutual labels:  error-handling
nested-error-stacks
A node.js module for creating Error objects with nested Errors in stacktraces
Stars: ✭ 86 (+330%)
Mutual labels:  error-handling
python-graceful-shutdown
Example of a Python code that implements graceful shutdown while using asyncio, threading and multiprocessing
Stars: ✭ 109 (+445%)
Mutual labels:  graceful-shutdown
go-errors
A super tiny package for error encapsulation in idiomatic Go
Stars: ✭ 14 (-30%)
Mutual labels:  error-handling
optionals
Rust-like error handling and options for TypeScript and Deno!
Stars: ✭ 97 (+385%)
Mutual labels:  error-handling
custom error
Define custom errors without boilerplate using the custom_error! macro.
Stars: ✭ 70 (+250%)
Mutual labels:  error-handling

node-graceful-shutdown

npm License Build Status

Gracefully handle your modular NodeJS application's shutdown (termination), using dependencies.

Process signals captured: SIGINT, SIGTERM, SIGQUIT.

Example

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 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.

Or, if you have all your modules as exports and they all shutdown one after another, this will work at its best in your application's main.js:

import { onShutdown } from "node-graceful-shutdown";
import {
  startModule1,
  startModule2,
  stopModule1,
  stopModule2 /*, ...*/,
} from "./src";

export const startMyApp = async () => {
  await startModule1();
  await startModule2();
};

export const stopMyApp = async () => {
  // Stop modules one after another.
  await stopModule1();
  await stopModule2();
  // ...
};

// Handle application's shutdown.
onShutdown(stopMyApp);

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

Recommendations:

  1. Please, do not use this module in libraries (packages). It is intended for end applications only (see why in 5.).
  2. Once imported, onShutdown is application-wide (in terms of a single process), so the callbacks and their dependencies will see each other when imported from multiple files.
  3. Circular shutdown handler dependencies will throw an error immediately once declared.
  4. There's also an onShutdownError export which takes an error as an argument when any of assigned shutdown handlers throw an error (added for very-very prudent programmers only).
  5. Importing this module deletes existing handlers (SIGINT, SIGTERM, SIGQUIT) if there are any. This is intended as other custom handlers can exit the process at any time.
  6. You may also consider defining constants in your application, instead of string arguments (names).

Licence

MIT © Nikita Savchenko

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].