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

nodejs-graceful-shutdown

v1.0.7

Published

nodejs graceful shutdown

Downloads

54

Readme

nodejs-graceful-shutdown

Gracefully shuts down node.js http server.

NPM Version NPM Downloads Git Issues Closed Issues MIT license

  • Simple to use
  • Configurable to your needs
  • Add your own cleanup function
  • The project referred to "https://github.com/sebhildebrandt/http-graceful-shutdown" and tried to improve it further.
  • If you use pm2 in a container environment, consider pm2-runtime

Quick Start

Installation

$ npm install nodejs-graceful-shutdown

Basic Usage

var gracefulShutdown = require('nodejs-graceful-shutdown');
...
// app: can be http, https, express, koa
server = app.listen(...);
...

// this enables the graceful shutdown
gracefulShutdown(server);

Advanced Options

You can pass an options-object to specify your specific options for the graceful shutdown

The following example uses all possible options (using more or less the default settings):

const gracefulShutdown = require('nodejs-graceful-shutdown');
...
// app: can be http, https, express, koa
server = app.listen(...);
...

// your personal cleanup function
// - must return a promise
// - the input parameter is optional (only needed if you want to
//   access the signal type inside this function)
// - this function here in this example takes one second to complete
function cleanup(signal) {
  return new Promise((resolve) => {
	console.log('... called signal: ', signal);
  	console.log('... in cleanup')
  	setTimeout(function() {
  		console.log('... cleanup finished');
  		resolve();
  	}, 1000)
  });
}

// this enables the graceful shutdown with advanced options
gracefulShutdown(server,
	{
		signals: 'SIGINT SIGTERM',
		timeout: 30000,
		development: false,
		onShutdown: cleanup,
		finally: function() {
			console.log('Server gracefulls shutted down.....')
		}
	}
);

Trigger shutdown manually

You can now trigger gracefulShutdown programatically (e.g. for tests) like so:

let shutdown
beforeAll(() => {
  shutdown = gracefulShutdown(...)
})

afterAll(async () => {
  await shutdown()
})

Option Reference

| option | default | Comments | | ----------- | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | signals | 'SIGINT SIGTERM' | define the signals, that should be handled (separated by SPACE) | | timeout | 30000 | timeout till forced shutdown (in milli seconds) | | development | false | if set to true, no graceful shutdown is proceeded to speed up dev-process | | onShutdown | - | place your (not time consuming) callback function, that willhandle your additional cleanup things. Needs to return a promise.If you add an input parameter to your cleanup function (optional),the signal type that caused the shutdown is passed to yourcleanup function - example. | | finally | - | here you can place a small (not time consuming) function, that willbe handled at the end of the shutdown (not in dev-mode) |

Debug

If you want to get debug notes (debug is a dependency of this module), just set the DEBUG environment variable to enable debugging:

export DEBUG=nodejs-graceful-shutdown

OR on Windows:

set DEBUG=nodejs-graceful-shutdown

Version history

| Version | Date | Comment | | ------- | ---------- | --------------- | | 1.0.0 | 2019-09-21 | initial release |