cluster: simplify process event handling

This simplify the internalMessage and exit event handling
And simply relay message and error event to the worker object
Note that the error event was not relayed before
This commit is contained in:
Andreas Madsen 2012-02-01 17:16:15 +01:00 committed by isaacs
parent 5e1471c806
commit a20872045a

View File

@ -169,7 +169,7 @@ function handleResponse(outMessage, outHandle, inMessage, inHandle, worker) {
// Handle messages from both master and workers // Handle messages from both master and workers
var messageHandingObject = {}; var messageHandingObject = {};
function handleMessage(inMessage, inHandle, worker) { function handleMessage(worker, inMessage, inHandle) {
//Remove internal prefix //Remove internal prefix
var message = extendObject({}, inMessage); var message = extendObject({}, inMessage);
@ -303,27 +303,13 @@ function Worker(customEnv) {
}); });
} }
// Internal message: handle message // handle internalMessage and exit event
this.process.on('internalMessage', function(message, handle) { this.process.on('internalMessage', handleMessage.bind(null, this));
debug('recived: ', message); this.process.on('exit', prepareDeath.bind(null, this, 'dead', 'death'));
// relay to handleMessage // relay message and error
handleMessage(message, handle, self); this.process.on('message', this.emit.bind(this, 'message'));
return; this.process.on('error', this.emit.bind(this, 'error'));
});
// Non-internal message: relay to Worker object
this.process.on('message', function(message, handle) {
self.emit('message', message, handle);
});
// Handle exit
self.process.on('exit', function() {
debug('worker id=' + self.uniqueID + ' died');
// Prepare worker to die and emit events
prepareDeath(self, 'dead', 'death');
});
} }
util.inherits(Worker, EventEmitter); util.inherits(Worker, EventEmitter);