Skip to content

Commit bd77eac

Browse files
* emit 'worker close' in master process when a worker closes
* delay execution of process.exit with a callback when listening on 'worker close' Closes LearnBoost#167
1 parent 2a3e68b commit bd77eac

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

lib/worker.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,30 @@ Worker.prototype.connect = function(id, options){
139139
};
140140

141141
/**
142-
* Immediate shutdown.
142+
* Shutdown the process (using process.nextTick to put it at the end of
143+
* event queue).
144+
*
145+
* If the master is listening on the 'worker close' event, a callback
146+
* is passed. The callback *must* be called to shutdown the worker.
143147
*
144148
* @api private
145149
*/
146150

147-
Worker.prototype.destroy = function(){
148-
this.emit('close');
149-
process.nextTick(process.exit);
151+
Worker.prototype.destroy = function() {
152+
var eventName = 'worker close'
153+
, listeners = this.master.listeners(eventName);
154+
155+
var exit = function() {
156+
process.nextTick(process.exit());
157+
};
158+
159+
if (listeners.length > 0) {
160+
this.master.emit(eventName, function() {
161+
exit();
162+
});
163+
} else {
164+
exit();
165+
}
150166
};
151167

152168
/**

0 commit comments

Comments
 (0)