Skip to content

Commit

Permalink
cluster: emit worker as first 'message' event arg
Browse files Browse the repository at this point in the history
It's documented as such but didn't actually behave that way.

Bug introduced in commit 66fc8ca ("cluster: emit 'message' event on
cluster master"), which is the commit that introduced the event.

Fixes: #5126
PR-URL: #5361
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
  • Loading branch information
bnoordhuis committed Feb 25, 2016
1 parent 0dc216f commit 66f4586
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
18 changes: 18 additions & 0 deletions doc/api/cluster.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,29 @@ The `addressType` is one of:

* `worker` {cluster.Worker}
* `message` {Object}
* `handle` {undefined|Object}

Emitted when any worker receives a message.

See [child_process event: 'message'][].

Before Node.js v6.0, this event emitted only the message and the handle,
but not the worker object, contrary to what the documentation stated.

If you need to support older versions and don't need the worker object,
you can work around the discrepancy by checking the number of arguments:

```js
cluster.on('message', function(worker, message, handle) {
if (arguments.length === 2) {
handle = message;
message = worker;
worker = undefined;
}
// ...
});
```

## Event: 'online'

* `worker` {cluster.Worker}
Expand Down
6 changes: 3 additions & 3 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ function masterInit() {
process: workerProcess
});

worker.on('message', (message, handle) =>
cluster.emit('message', message, handle)
);
worker.on('message', function(message, handle) {
cluster.emit('message', this, message, handle);
});

worker.process.once('exit', function(exitCode, signalCode) {
/*
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-cluster-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ else if (cluster.isMaster) {
worker.on('message', function(message) {
check('master', message === 'message from worker');
});
cluster.on('message', function(message) {
cluster.on('message', function(worker_, message) {
assert.strictEqual(worker_, worker);
check('global', message === 'message from worker');
});

Expand Down

0 comments on commit 66f4586

Please sign in to comment.