Skip to content

gavinhsmith/shutdown

Repository files navigation

shutdown

A simple shutdown handler for Node.

NPM Version NPM Downloads License

Install

Install shutdown via your favorite package manager.

npm install @gavinhsmith/shutdown
yarn add @gavinhsmith/shutdown

Usage

Include in your project, init shutdown, and add as many handlers as you'd like. TypeScript definitions are included in the module.

// Import the module.
import runAtShutdown, { init } from "@gavinhsmith/shutdown";

// Initiate the module in your startup file.
init({...});

// Add your handlers anywhere in your project.
runAtShutdown("handler_1", (code, name) => {...});

// Add another that requires "handler_1" to be completed first.
runAtShutdown("handler_1", (code, name) => {...}, ["handler_1"]);

Config

Module can be configed in the init() function.

// Changes the signals to listen to, and the exit code to throw on a signal.
init({
  signalExitCode: 2,
  watchedSignals: ["SIGTERM"],
  ...
});

// Your code, including your shutdowns...
runAtShutdown("handler_1", (code, name) => {...});
runAtShutdown("handler_2", (code, name) => {...}, ["handler_1"]);
runAtShutdown("handler_3", (code, name) => {...}, ["handler_1"]);

// End your program somehow.
process.kill(process.pid, "SIGTERM");
Process exited with code 2.

Config options

These config options can be used in the init() function to modify shutdown.

Config Option Description Type Default
signalExitCode The exit code that signal events like SIGTERM will throw when invoked. int 1
watchedSignals The signals that shutdown will listen to. string[] SIGINT, SIGTERM, SIGUSR1, SIGUSR2
watchedEvents The process events that shutdown will listen to. Can't be exit or beforeExit, these are already processed. string[] uncaughtException
quiet If shutdown messages should be suppresed. boolean true

Contributing

Clone the repository, and run npm i or yarn to install the dependancies and build the module. Run module tests via the test script in package.json.

Workflow tests require act. You do not need this for module development, as workflow tests are not run during CI.

I'll review pull requests in time.