Nonconformist Propaganda Machine
Publish your own packages to the world's most popular software ecosystem with npm Pro.Get started »

http-graceful-shutdown

2.3.2 • Public • Published

http-graceful-shutdown

Gracefully shuts down node.js http server.

NPM Version NPM Downloads Git Issues Closed Issues deps status Code Quality: Javascript Total alerts Caretaker MIT license

  • can be used with express, koa, native node http, ...
  • simple to use
  • configurable to your needs
  • add your own cleanup function

Quick Start

Installation

$ npm install http-graceful-shutdown

Basic Usage

var gracefulShutdown = require('http-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('http-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()
})

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.

If you add an input parameter to your cleanup function (optional),
the signal type that caused the shutdown is passed to your
cleanup function - example.
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-graceful-shutdown

OR on Windows:

set DEBUG=http-graceful-shutdown

Version history

Version Date Comment
2.3.2 2019-06-14 typescript typings fix
2.3.1 2019-05-31 updated docs, added typescript typings
2.3.0 2019-05-30 added manual shutdown (for tests) see docs below
2.2.3 2019-02-01 updated docs, debug
2.2.2 2018-12-28 updated docs, keywords
2.2.1 2018-11-20 updated docs
2.2.0 2018-11-19 added (optional) signal type to shutdown function - see example
2.1.3 2018-11-06 updated docs
2.1.2 2018-11-03 updated dependencies (version bump), updated docs
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-2019 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.

Install

npm i http-graceful-shutdown

DownloadsWeekly Downloads

19,722

Version

2.3.2

License

MIT

Unpacked Size

16.6 kB

Total Files

5

Last publish

Collaborators

  • avatar