http-graceful-shutdown manages a secure and save shutdown of your http server application:
tracks all connections
stops the server from accepting new connections on shutdown
graceful communication to all connected clients of server intention to shutdown
immediately destroys all sockets without an attached HTTP request
properly handles all HTTP and HTTPS connections
possibility to define cleanup functions (e.g. closing DB connections)
preShutdown function if you need to have all HTTP sockets available and untouched
choose between shutting down by function call or triggered by SIGINT, SIGTERM, ...
choose between final forcefull process termination node.js (process.exit) or clearing event loop (options).
Quick Start
Installation
$ npm install http-graceful-shutdown
Basic Usage
constgracefulShutdown=require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...server=app.listen(...);
...
// this enables the graceful shutdowngracefulShutdown(server);
usually your NODE http server (the black bar in the middle) replies to client requests and sends responses
if your server receives a termination signal (e.g. SIGINT - Ctrl-C) from its parent, http-graceful-shutdown starts the shutdown procedure
first http-graceful-shutdown will run the "preShutdown" (async) function. Place your own function here (passed to the options object), if you need to have all HTTP sockets available and untouched.
then alle empty connections are closed and destroyed and
http-graceful-shutdown will block any new requests
If possible, http-graceful-shutdown communicates to the clients that the server is about to close (connection close header)
http-graceful-shutdown now tries to wait till all sockets are finished, then destroys the all remaining sockets
now it is time to run the "onShutdown" (async) function (if such a function is passed to the options object)
as soon as this onShutdown function has ended, the "finally" (sync) function is executed (if passed to the options)
now the event loop cleared up OR process.exit() is triggered (can be defined in the options) and the server process ends.
Options
option
default
Comments
timeout
30000
timeout till forced shutdown (in milli seconds)
signals
'SIGINT SIGTERM'
define the signals, that should be handled (separated by SPACE)
development
false
if set to true, no graceful shutdown is proceeded to speed up dev-process
preShutdown
-
not time consuming callback function. Needs to return a promise. Here all HTTP sockets are still available and untouched
onShutdown
-
not time consuming callback function. Needs to return a promise.
forceExit
true
force process.exit - otherwise just let event loop clear
finally
-
small, not time consuming function, that will be handled at the end of the shutdown (not in dev-mode)
Option Explanation
timeout: You can define the maximum time that the shutdown process may take (timeout option). If after this time, connections are still open or the shutdown process is still running, then the remaining connections will be forcibly closed and the server process is terminated.
signals Here you can define which signals can trigger the shutdown process (SIGINT, SIGTERM, SIGKILL, SIGHUP, SIGUSR2, ...)
development If true, the shutdown process is much shorter, because it just terminates the server, ignoring open connections, shutdown function, finally function ...
preShutdown Place your own (not time consuming) callback function here, if you need to have all HTTP sockets available and untouched during cleanup. Needs to return a promise. (async). If you add an input parameter to your cleanup function (optional), the SIGNAL type that caused the shutdown is passed to your cleanup function. See example.
onShutdown place your (not time consuming) callback function, that will handle your additional cleanup things (e.g. close DB connections). Needs to return a promise. (async). If you add an input parameter to your cleanup function (optional), the SIGNAL type that caused the shutdown is passed to your cleanup function. See example.
finally here you can place a small (not time consuming) function, that will be handled at the end of the shutdown e.g. for logging of shutdown. (sync)
forceExit force process.exit() at the end oof the shutdown process - otherwise just let event loop clear
Advanced Options Example
You can pass an options-object to specify your specific options for the graceful shutdown
The following example uses all possible options:
constgracefulShutdown=require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...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 completefunctionshutdownFunction(signal){returnnewPromise((resolve)=>{console.log('... called signal: '+signal);console.log('... in cleanup')setTimeout(function(){console.log('... cleanup finished');resolve();},1000)});}// finally function// -- sync function// -- should be very short (not time consuming)functionfinalFunction(){console.log('Server gracefulls shutted down.....')}// this enables the graceful shutdown with advanced optionsgracefulShutdown(server,{signals: 'SIGINT SIGTERM',timeout: 10000,// timeout: 10 secsdevelopment: false,// not in dev modeforceExit: true,// triggers process.exit() at the end of shutdown processpreShutdown: preShutdownFunction,// needed operation before httpConnections are shutted downonShutdown: shutdownFunction,// shutdown function (async) - e.g. for cleanup DB, ...finally: finalFunction// finally function (sync) - e.g. for logging});
Trigger shutdown manually
You can now trigger gracefulShutdown programatically (e.g. for tests) like so:
With the forceExit option, you can define how your node server process ends: when setting forceExit to false, you just let the event loop clear and then the proccess ends automatically:
constgracefulShutdown=require('http-graceful-shutdown');
...
// app: can be http, https, express, koa, fastity, ...server=app.listen(...);
...
// enable graceful shutdown with options:// this option lets the event loop clear to end your node server// no explicit process.exit() will be triggered.gracefulShutdown(server,{forceExit: false});
If you want an explicit process.exit() at the end, set forceExit to true (which is the default).
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
Examples
You can find examples how to use http-graceful-shutdown with Express, Koa, http, http2, fastify in the examples directory.
To run the examples, be sure to install debug and express, koa or fastify.
npm install debug express koa fastify
Version history
Version
Date
Comment
3.1.7
2022-03-18
updated dependencies, updated docs
3.1.6
2022-02-27
updated dependencies
3.1.5
2021-11-08
updated docs
3.1.4
2021-08-27
updated docs
3.1.3
2021-08-03
fixed handle events once (thanks to Igor Basov)
3.1.2
2021-06-15
fixed cleanupHttp() no timeout
3.1.1
2021-05-13
updated docs
3.1.0
2021-05-08
refactoring, added preShutdown
3.0.2
2021-04-08
updated docs
3.0.1
2021-02-26
code cleanup
3.0.0
2021-02-25
version 3.0 release
2.4.0
2021-02-15
added forceExit option (defaults to true)
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
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.
请发表评论