Skip to content

Commit

Permalink
better call stack output
Browse files Browse the repository at this point in the history
  • Loading branch information
ivolucien committed Jan 15, 2018
1 parent 5e1e60d commit 221714c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
0.4.1 / 2018-01-15
==================

* Name anonymous functions for call stack clarity

0.4.0 / 2017-03-19
==================

Expand Down
17 changes: 9 additions & 8 deletions lib/graceful-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var sockets = [];
* Keep track of open connections so we can forcibly close sockets when the suicide timeout elapses
* @param server HTTP server
*/
exports.init = function (server) {
exports.init = function init(server) {
server.on('connection', function (socket) {
sockets.push(socket);

Expand All @@ -16,7 +16,7 @@ exports.init = function (server) {
});
};

exports.gracefulExitHandler = function(app, server, _options) {
exports.gracefulExitHandler = function gracefulExitHandler(app, server, _options) {
// Get the options set up
if (!_options) {
_options = {};
Expand Down Expand Up @@ -45,13 +45,14 @@ exports.gracefulExitHandler = function(app, server, _options) {
if (options.callback) {
if (_.isFunction(options.callback)) {
options.callback(code);
options.callback = false; // prevent 2nd call
} else {
logger("Registered callback is not a function");
}
}
if (options.exitProcess) {
// leave a bit of time to write logs, callback to complete, etc
setTimeout(function() {
setTimeout(function exitProcess() {
process.exit(1);
}, options.exitDelay);
}
Expand All @@ -71,7 +72,7 @@ exports.gracefulExitHandler = function(app, server, _options) {
// Everything was closed successfully, mission accomplished!
connectionsClosed = true;

logger('All connections closed gracefully');
logger('No longer accepting connections');
exit(0);

clearTimeout(suicideTimeout);
Expand Down Expand Up @@ -108,7 +109,7 @@ exports.gracefulExitHandler = function(app, server, _options) {
// If after an acceptable time limit is reached and we still have some
// connections lingering around for some reason, just die... we tried to
// be graceful, but failed.
suicideTimeout = setTimeout(function() {
suicideTimeout = setTimeout(function suicideHandler() {
if (connectionsClosed) {
// this condition should never occur, see server.close() above
// user callback, if any, has already been called
Expand All @@ -131,14 +132,14 @@ exports.gracefulExitHandler = function(app, server, _options) {
}, options.suicideTimeout);
};

exports.middleware = function(app) {
exports.middleware = function middleware(app) {
// This flag is used to tell the middleware we create that the server wants
// to stop, so we do not allow anymore connections. This is done for all new
// to stop, so we do not allow any more connections. This is done for all new
// connections for us by Node, but we need to handle the connections that are
// using the Keep-Alive header to stay on.
app.set('graceful_exit', false);

return function(req, res, next) {
return function checkGracefulExit(req, res, next) {
// Sorry Keep-Alive connections, but we need to part ways
if (app.settings.graceful_exit === true) {
req.connection.setTimeout(1);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-graceful-exit",
"version": "0.4.0",
"version": "0.4.1",
"description": "Allow graceful exits for express apps, supporting zero downtime deploys",
"keywords": [
"express",
Expand Down

0 comments on commit 221714c

Please sign in to comment.