Skip to content

Commit

Permalink
* emit 'worker close' in master process when a worker closes
Browse files Browse the repository at this point in the history
* delay execution of process.exit with a callback when listening on 'worker close'

Closes LearnBoost#167
  • Loading branch information
smtlaissezfaire committed Nov 4, 2011
1 parent 2a3e68b commit c4bec30
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/worker.js
Expand Up @@ -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();
}
};

/**
Expand Down

0 comments on commit c4bec30

Please sign in to comment.