Git Product home page Git Product logo

node-graceful-shutdown's Introduction

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

node-graceful-shutdown's People

Contributors

darkle avatar niekvelde avatar nikitaeverywhere avatar nordfjord avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-graceful-shutdown's Issues

Check dependency loop logic is incorrect

The checkDependencyLoop logic is incorrect. Even the exampled code doesn't work:

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.
});

You should have independent visited list for each recursion branch.

Parameter has a name but no type.

When I compile my project which depends on
"node-graceful-shutdown": "^1.1.0",
I get the error you see below.
Until 1.1.2 everything Ok the error occurs with 1.1.3
Schermata 2022-12-13 alle 12 43 53

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.