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:
parent
5e1471c806
commit
a20872045a
@ -169,7 +169,7 @@ function handleResponse(outMessage, outHandle, inMessage, inHandle, worker) {
|
||||
|
||||
// Handle messages from both master and workers
|
||||
var messageHandingObject = {};
|
||||
function handleMessage(inMessage, inHandle, worker) {
|
||||
function handleMessage(worker, inMessage, inHandle) {
|
||||
|
||||
//Remove internal prefix
|
||||
var message = extendObject({}, inMessage);
|
||||
@ -303,27 +303,13 @@ function Worker(customEnv) {
|
||||
});
|
||||
}
|
||||
|
||||
// Internal message: handle message
|
||||
this.process.on('internalMessage', function(message, handle) {
|
||||
debug('recived: ', message);
|
||||
// handle internalMessage and exit event
|
||||
this.process.on('internalMessage', handleMessage.bind(null, this));
|
||||
this.process.on('exit', prepareDeath.bind(null, this, 'dead', 'death'));
|
||||
|
||||
// relay to handleMessage
|
||||
handleMessage(message, handle, self);
|
||||
return;
|
||||
});
|
||||
|
||||
// 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');
|
||||
});
|
||||
// relay message and error
|
||||
this.process.on('message', this.emit.bind(this, 'message'));
|
||||
this.process.on('error', this.emit.bind(this, 'error'));
|
||||
|
||||
}
|
||||
util.inherits(Worker, EventEmitter);
|
||||
|
Loading…
x
Reference in New Issue
Block a user