Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
cluster: simplify process event handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AndreasMadsen authored and isaacs committed Feb 6, 2012
1 parent 5e1471c commit a208720
Showing 1 changed file with 7 additions and 21 deletions.
28 changes: 7 additions & 21 deletions lib/cluster.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a208720

Please sign in to comment.