// 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);
});
}
- // 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);