Skip to content
Permalink
Browse files

cluster: emit worker as first 'message' event arg

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 22, 2016
1 parent 0dc216f commit 66f4586dd031435984ed77837512531dfc4fe893
Showing with 23 additions and 4 deletions.
  1. +18 −0 doc/api/cluster.markdown
  2. +3 −3 lib/cluster.js
  3. +2 −1 test/parallel/test-cluster-message.js
@@ -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}
@@ -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) {
/*
@@ -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');
});

0 comments on commit 66f4586

Please sign in to comment.
You can’t perform that action at this time.