Skip to content

Commit

Permalink
Merge pull request #3 from art19/clear-timeouts
Browse files Browse the repository at this point in the history
Clear timeouts when exiting so we don't leave garbage behind
  • Loading branch information
nleush authored Jul 14, 2017
2 parents 30a0c07 + 29ba508 commit 002c4dd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/graceful-cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ GracefulCluster.start = function(options) {

if (cluster.isMaster) {

var sigkill = false;
var currentRestartingPid = null;
var currentWorkersCount = 0;
var listeningWorkersCount = 0;
var restartQueue = [];
var currentRestartingPid = null;
var shutdownTimer = null;
var sigkill = false;

// Prevent killing all workers at same time when restarting.
function checkRestartQueue() {
Expand Down Expand Up @@ -95,6 +96,7 @@ GracefulCluster.start = function(options) {
function checkIfNoWorkersAndExit() {
if (!currentWorkersCount) {
log('Cluster graceful shutdown: done.');
if (shutdownTimer) clearTimeout(shutdownTimer);
exitFunction();
} else {
log('Cluster graceful shutdown: wait ' + currentWorkersCount + ' worker' + (currentWorkersCount > 1 ? 's' : '') + '.');
Expand All @@ -104,6 +106,7 @@ GracefulCluster.start = function(options) {
function startShutdown() {

if (disableGraceful) {
if (shutdownTimer) clearTimeout(shutdownTimer);
exitFunction();
return;
}
Expand All @@ -116,7 +119,7 @@ GracefulCluster.start = function(options) {
}

// Shutdown timeout.
setTimeout(function() {
shutdownTimer = setTimeout(function() {
log('Cluster graceful shutdown: timeout, force exit.');
exitFunction();
}, shutdownTimeout);
Expand Down
4 changes: 3 additions & 1 deletion lib/graceful-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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);
});

Expand Down

0 comments on commit 002c4dd

Please sign in to comment.