From 7a5383748c1193750ee4f27b8f173ea112f45fd4 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 2 Dec 2014 16:49:49 -0500 Subject: [PATCH] possible fix for #2492 --- loader.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/loader.js b/loader.js index 68d67db1e0..9a81ceefa4 100644 --- a/loader.js +++ b/loader.js @@ -21,7 +21,6 @@ var nconf = require('nconf'), Loader = { timesStarted: 0, - shutdown_queue: [], js: { cache: undefined, map: undefined @@ -86,12 +85,6 @@ Loader.addClusterEvents = function(callback) { hash: Loader.css.hash }); } - - // Kill an instance in the shutdown queue - var workerToKill = Loader.shutdown_queue.pop(); - if (workerToKill) { - cluster.workers[workerToKill].kill(); - } break; case 'restart': console.log('[cluster] Restarting...'); @@ -269,9 +262,16 @@ function clusterWorkers() { } Loader.restart = function(callback) { - // Slate existing workers for termination -- welcome to death row. - Loader.shutdown_queue = Loader.shutdown_queue.concat(Object.keys(cluster.workers)); - Loader.start(); + console.log('[cluster] closing server'); + + killWorkers(); + + closeHandles(); + + server.close(function() { + console.log('[cluster] server closed'); + Loader.start(); + }); }; Loader.reload = function() { @@ -283,10 +283,7 @@ Loader.reload = function() { }; Loader.stop = function() { - Object.keys(cluster.workers).forEach(function(id) { - // Gracefully close workers - cluster.workers[id].kill(); - }); + killWorkers(); // Clean up the pidfile fs.unlinkSync(__dirname + '/pidfile'); @@ -294,6 +291,22 @@ Loader.stop = function() { server.close(); }; +function killWorkers() { + Object.keys(cluster.workers).forEach(function(id) { + cluster.workers[id].kill(); + }); +} + +function closeHandles() { + for(var h in handles) { + var handle = handles[h]; + if (handle) { + h.close(); + delete handles[handle]; + } + } +} + Loader.notifyWorkers = function (msg) { Object.keys(cluster.workers).forEach(function(id) { cluster.workers[id].send(msg);