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(); + } }; /**