npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-shutdown-events

v0.1.1

Published

xyzzy

Downloads

6

Readme

Build Status NPM Status

Overview

This library provides orderly shutdown for Node.js with ordered shutdown and asynchronous shutdown actions using event listeners.

While there are a number of shutdown libraries, I encountered problems with most of the ones I tried using asynchronous, ordered shutdown actions, so I created this library.

On initialization it does the following:

  • Verifies it was not already registered
  • Verified none of the relevant handlers are already trapped
  • Creates a shutdown handler object and stores it in process.nodeShutdownHandler
  • Traps SIGTERM, SIGINT, uncaughtException events from Node
  • Traps unhandledRejection events from Bluebird
  • Traps error events from the specified domain (optional)
  • Provides methods to handle other error events or unexpected errors

Code can then register shutdown events via process.on

  • Events can return a Promise if the action is asynchronous.

When a shutdown occurs,

  • If the process is a Node cluster worker, the process is disconnected from the cluster
  • All event handlers for shutdown are called first and the shutdown handler waits until any promises are settled
  • All event handlers for shutdown0 are called and the shutdown handler waits until any promises are settled
  • This is repeated for shutdown1 to shutdown9
  • Any other event handlers named shutdownXXXX are called (in the reverse of the order they were initially registered) and the shutdown handler waits until any promises are settled
  • All handlers for shutdownFinal are called and the shutdown handler waits until any promises are settled
  • Finally it waits until the node queue drains and terminates with the desired error level
  • If all of the above takes longer than the timeout specified, the process exits after the timeout.

This sounds more complex to use than it really is. In general basic non-dependent shutdown actions just register to process.on("shutdown") and remote connections tend to register to process.on("shutdownFinal") - the other options are only used to allow flexibility for ordering shutdown actions (like a log services which depends on a database).

Examples of use:

Registration

var ShutdownHandler = require("node-shutdown-events");
new ShutdownHandler();

MongoDB

var Promise = require("bluebird");
var mongodb = require("mongodb");
Promise.promisifyAll(mongodb);
mongodb.MongoClient.connectAsync() // Add URL and options here...
.tap(function(db) { process.on("shutdownFinal", function () { return db.closeAsync(); }); })
.then(function(db) { /* Do your work here */ });

Mongoose:

var Promise = require("bluebird");
var mongoose = require("mongoose");
Promise.promisifyAll(mongoose);
process.on("shutdownFinal", function () { return mongoose.disconnectAsync(); });

ioredis:

var ioredis = require("ioredis");
var redis = new ioredis(); // Add options here...
process.on("shutdownFinal", function () { return redis.quit() });

API

new ShutdownHandler(options)

Creates a new shutdown handler which is automatically registered in process.nodeShutdownHandler. Will throw an error if another handler is registered OR if any of the events are tapped already.

Options:

  • exitTimeout (default is 20000) - Maximum time to wait for shutdown handlers in milliseconds
  • unexpectedExceptionExitCode (default is 1) - Exit code to use for the process if an unexpected exception occurs
  • domain - If specified, the handler listens for errors on this domain
  • log (default is console) - If a console-compatible object is provided, then messages are logged via this object
  • process (default is global process object) - Overrides the use of the global process object - mainly used for testing
  • cluster (default is require("cluster")) - Overrides the use of the Node cluster module - mainly used for testing

process.nodeShutdownHandler.handleUnexpectedError(err)

Initiates a shutdown as if an unhandled exception err was thrown. Used to hook in other shutdown sources.

process.nodeShutdownHandler.hookErrorEvent(emitter, eventName = "error")

Adds an error event handler to an event emitter and handles any errors from this event by shutting down. The event name defaults to "error" but may be overridden.

process.nodeShutdownHandler.hookErrorEvent(signal, errorLevel)

Initiates a shutdown. Signal is the name of the shutdown signal or reason (for logging purposes) and errorLevel is the desired exit error level.

Contributing

Any PRs are welcome but please stick to following the general style of the code and stick to CoffeeScript. I know the opinions on CoffeeScript are...highly varied...I will not go into this debate here - this project is currently written in CoffeeScript and I ask you maintain that for any PRs.