/http-graceful-shutdown

Gracefully shuts down node http server - can be used with http, express, koa, ...

Primary LanguageJavaScriptMIT LicenseMIT

http-graceful-shutdown

Gracefully shuts down node http server - can be used with express, koa, native node http, ...

NPM Version NPM Downloads Git Issues deps status MIT license

Quick Start

Installation

$ npm install http-graceful-shutdown

Basic Usage

var gracefulShutdown = require('http-graceful-shutdown');
...
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 specifies all possible options (using more or less the default settings):

const gracefulShutdown = require('http-graceful-shutdown');
...
server = app.listen(...);
...

// your personal cleanup function - this one takes one second to complete
function cleanup() {
  return new Promise((resolve) => {
  	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.....')
		}
	}
);

Major (breaking) Changes - Version 2

  • renamed option: callback: now to finally: place your (not time consuming) function, that will be handled at the end of the shutdown (not in dev-mode)
  • new option: onShutdown: place your function, that will handle your additional cleanup things. Needs to return a promise

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 will handle your additional cleanup things. Needs to return a promise
finally - here you can place a small (not time consuming) function, that will be 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=http-graeceful-shutdown

OR on Windows:

set DEBUG=http-graeceful-shutdown

Version history

Version Date Comment
2.1.1 2018-02-28 extended isFunction to support e.g. AsyncFunctions
2.1.0 2018-02-11 bug fixing onShutdown method was called before server.close
2.0.6 2017-11-06 updated docs, code cleanup
2.0.5 2017-11-06 updated dependencies, modifications gitignore, added docs
2.0.4 2017-09-21 updated dependencies, modifications gitignore
2.0.3 2017-06-18 updated dependencies
2.0.2 2017-05-27 fixed return value 0
2.0.1 2017-04-24 modified documentation
2.0.0 2017-04-24 added 'onShutdown' option, renamed 'callback' option to 'finally'
1.0.6 2016-02-03 adding more explicit debug information and documentation
1.0.5 2016-02-01 better handling of closing connections
1.0.4 2015-10-01 small fixes
1.0.3 2015-09-15 updated docs
1.0.1 2015-09-14 updated docs, reformated code
1.0.0 2015-09-14 initial release

Comments

If you have ideas or comments, please do not hesitate to contact me.

Sincerely,

Sebastian Hildebrandt, +innovations

Credits

Written by Sebastian Hildebrandt sebhildebrandt

Contributers

License MIT license

The MIT License (MIT)

Copyright © 2015-2018 Sebastian Hildebrandt, +innovations.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.