express-graceful-exit
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -0,4 +1,34 @@ | ||
0.3.0 / 2016-08/05 | ||
================== | ||
* Released version to npm | ||
* Configurable delay for timer that calls process exit | ||
* Hard exit function now obeys exitProcess option | ||
* Doc updates, options in table format | ||
0.2.1 / 2016-07-27 | ||
================== | ||
* Released version to npm | ||
* Updated package metadata, version string | ||
* Code style overhaul, many semicolons | ||
0.2.0 / 2016-07-26 | ||
================== | ||
Thanks to shaharke for the majority of these changes. | ||
* Delay process exit to allow any streams to flush, etc. | ||
* Option to force close sockets on timeout | ||
* Minor doc and logging improvements | ||
Issue #1 feature request and fixes: | ||
* Exit handler callback when done or on timeout | ||
* Option for exit handler to not exit process itself | ||
* Clear hard exit timeout on successful server close | ||
* Avoid duplicate callback invocation | ||
0.1.0 / 2013-03-28 | ||
================== | ||
* Released version to npm | ||
* Don't keep track of Keep-Alive connections | ||
@@ -15,2 +45,2 @@ * Switch to not catching the exit message on our own | ||
* Initial Release | ||
* Initial Release |
@@ -29,2 +29,3 @@ | ||
exitProcess : true, | ||
exitDelay : 10, // wait in ms before process.exit, if exitProcess true | ||
force : false | ||
@@ -42,5 +43,6 @@ }); | ||
function exit(code) { | ||
if (options.exitProcess) { | ||
process.exit(code); | ||
} else if (options.callback) { | ||
if (suicideTimeout === null) { | ||
return; // server.close has finished, don't callback/exit twice | ||
} | ||
if (options.callback) { | ||
if (_.isFunction(options.callback)) { | ||
@@ -52,7 +54,12 @@ options.callback(code); | ||
} | ||
if (options.exitProcess) { | ||
// leave a bit of time to write logs, callback to complete, etc | ||
setTimeout(function() { | ||
process.exit(1); | ||
}, options.exitDelay); | ||
} | ||
} | ||
if (options.callback && options.exitProcess) { | ||
logger("Set exitProcess option to false, otherwise callback is ignored. " + | ||
"Register a callback when process exit is handled by the caller."); | ||
logger("Callback will have " + options.exitDelay + "ms to complete before hard exit"); | ||
} | ||
@@ -69,6 +76,8 @@ | ||
connectionsClosed = true; | ||
clearTimeout(suicideTimeout); | ||
logger('All connections done, stopping process'); | ||
logger('All connections closed gracefully'); | ||
exit(0); | ||
clearTimeout(suicideTimeout); | ||
suicideTimeout = null; // must be after calling exit() | ||
}); | ||
@@ -102,8 +111,6 @@ | ||
} else { | ||
logger('Exiting process before all connections are closed'); | ||
logger('Hard exit timer ran out before some connections closed'); | ||
} | ||
setTimeout(function() { | ||
// leave a bit of time to write logs | ||
process.exit(1); | ||
}, 10); | ||
exit(1); | ||
suicideTimeout = null; | ||
@@ -110,0 +117,0 @@ }, options.suicideTimeout); |
{ | ||
"name": "express-graceful-exit", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Allow graceful exits for express apps, supporting zero downtime deploys", | ||
@@ -5,0 +5,0 @@ "keywords": ["express", "graceful", "exit", "shutdown", "clean", "tidy"], |
@@ -5,3 +5,3 @@ # express-graceful-exit | ||
This module was developed for [Frafty](https://www.frafty.com/), a Daily Fantasy Sports site. | ||
This module was originally developed for [Frafty](https://www.frafty.com/), a Daily Fantasy Sports site. | ||
@@ -15,7 +15,11 @@ ## Installation | ||
## Compatibility | ||
v0.X.X versions are backwards API compatible, with the caveate that process exit is called in a `setTimeout` block from v0.2.0 forward, so the timing is slightly different between v0.1.0 to v0.2.x+. | ||
## Usage | ||
The following two components must be setup for this to work as planned. | ||
The following two components must both be used to enable fully graceful exits. | ||
### Middleware | ||
### middleware | ||
@@ -32,5 +36,5 @@ This middleware should be the very first middleware that gets setup with your Express app. | ||
### Graceful Exit Handler | ||
### gracefulExitHandler | ||
This function will cleanup the server and get it ready for shutting down. It can be attached to a signal, or used as a normal function call if another tool is used (such as [naught](https://github.com/indabamusic/naught)). | ||
This function tells express to accept no new requests and gracefully closes the http server. It can be attached to a signal, or used as a normal function call if another tool is used (such as [naught](https://github.com/indabamusic/naught)). | ||
@@ -43,5 +47,5 @@ ```` javascript | ||
socketio: app.settings.socketio | ||
}) | ||
}); | ||
} | ||
}) | ||
}); | ||
```` | ||
@@ -55,13 +59,16 @@ | ||
### Process Handler | ||
### Exit Handler | ||
The following options are available: | ||
* __log:__ Print status messages and errors to the logger (default false). | ||
* __logger:__ Function that accepts a string to output a log message (default console.log). | ||
* __suicideTimeout:__ How long to wait before giving up on graceful shutdown, then returns exit code of 1 (default 2m 10s). | ||
* __socketio:__ An instance of `socket.io`, used to close all open connections after timeout (default none) | ||
* __exitProcess:__ If true, the module calls `process.exit()` when express has shutdown, gracefully or not (default true). | ||
* __callback:__ Optional function that is called with the exit status code once express has shutdown, gracefully or not - use in conjunction with exitProcess=false, when the caller handles process shutdown (default none) | ||
* __force:__ Instructs the module to forcibly close sockets once the suicide timeout elapses. Requires that `gracefulExit.init(server)` be called when initializing the HTTP server (default: false) | ||
Option | Description | Default | ||
------ | ----------- | ------- | ||
__log__ | Print status messages and errors to the logger | false. | ||
__logger__ | Function that accepts a string to output a log message | console.log | ||
__callback__ | Optional function that is called with the exit status code once express has shutdown, gracefully or not - use in conjunction with exitProcess=false, when the caller handles process shutdown | no-op | ||
__exitProcess__ | If true, the module calls `process.exit()` when express has shutdown, gracefully or not | true | ||
__exitDelay__ | How long to wait in the final internal callback (gracefulExitHandler or suicideTimeout) before calling process.exit, if exitProcess is true | 10ms | ||
__suicideTimeout__ | How long to wait before giving up on graceful shutdown, then returns exit code of 1 | 2m 10s (130s) | ||
__socketio__ | An instance of `socket.io`, used to close all open connections after timeout | none | ||
__force__ | Instructs the module to forcibly close sockets once the suicide timeout elapses. Requires that `gracefulExit.init(server)` be called when initializing the HTTP server | false | ||
@@ -75,10 +82,11 @@ ## Details | ||
3. If a socket.io instance is passed in the options, it enumerates all connected clients and disconnects them. The client should have code to reconnect on disconnect | ||
4. Once all connected clients are disconnected, the server exits with an error code of 0 | ||
5. If there are still some remaining connections after the `suicideTimeout`, it stops waiting and returns exit code of 1 | ||
4a. Once all connected clients are disconnected, the exit handler returns 0 | ||
4b. OR If there are any remaining connections after `suicideTimeout` ms, the handler returns 1 | ||
5. After either 4a or 4b, if exitProcess is set to true, it waits exitDelay ms and calls process.exit | ||
## Getting zero downtime deploys | ||
## Zero Downtime Deploys | ||
This module does not give you zero downtime deploys automatically, but provides a server that is capable of exiting gracefully, which can then be used by a module like naught to provide zero downtime deploys. | ||
This module does not give you zero downtime deploys on its own. It enables the http server to exit gracefully, which when used with a module like naught can provide zero downtime deploys. | ||
#### Author: [Jon Keating](http://twitter.com/emostar) | ||
#### Maintainer: [Ivo Havener](https://github.com/ivolucien) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
10582
112
87
0