From c4bec3094c144d9901c43de673f74d9f81e07e8b Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 4 Nov 2011 19:11:58 -0400 Subject: [PATCH] * emit 'worker close' in master process when a worker closes * delay execution of process.exit with a callback when listening on 'worker close' Closes #167 --- lib/worker.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/worker.js b/lib/worker.js index 1628ca6..135e834 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -139,14 +139,30 @@ Worker.prototype.connect = function(id, options){ }; /** - * Immediate shutdown. + * Shutdown the process (using process.nextTick to put it at the end of + * event queue). + * + * If the master is listening on the 'worker close' event, a callback + * is passed. The callback *must* be called to shutdown the worker. * * @api private */ -Worker.prototype.destroy = function(){ - this.emit('close'); - process.nextTick(process.exit); +Worker.prototype.destroy = function() { + var eventName = 'worker close' + , listeners = this.master.listeners(eventName); + + var exit = function() { + process.nextTick(process.exit()); + }; + + if (listeners.length > 0) { + this.master.emit(eventName, function() { + exit(); + }); + } else { + exit(); + } }; /**