1.0.7 • Published 5 years ago

nodejs-graceful-shutdown v1.0.7

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

nodejs-graceful-shutdown

Gracefully shuts down node.js http server.

NPM Version NPM Downloads Git Issues Closed Issues MIT license

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

optiondefaultComments
signals'SIGINT SIGTERM'define the signals, that should be handled (separated by SPACE)
timeout30000timeout till forced shutdown (in milli seconds)
developmentfalseif 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

VersionDateComment
1.0.02019-09-21initial release
1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago