diff --git a/lib/graceful-server.js b/lib/graceful-server.js index 701ddc5..1c66ef0 100644 --- a/lib/graceful-server.js +++ b/lib/graceful-server.js @@ -10,6 +10,7 @@ var GracefulServer = function(options) { this.log = options.log || console.log; this.shutdownTimeout = options.shutdownTimeout || 5000; + this._shutdownTimer = null; // Solution got from: https://github.com/nodejs/node-v0.x-archive/issues/9066#issuecomment-124210576 @@ -66,13 +67,14 @@ GracefulServer.prototype.shutdown = function() { var that = this; - setTimeout(function() { + this._shutdownTimer = setTimeout(function() { that.log('pid:' + process.pid + ' graceful shutdown: timeout, force exit.'); process.exit(0); }, this.shutdownTimeout); this.server.close(function() { that.log('pid:' + process.pid + ' graceful shutdown: done.'); + if (that._shutdownTimer) clearTimeout(that._shutdownTimer); process.exit(0); });