Browse Source

cluster: emit 'message' event on cluster master

For consistency with the worker 'exit', 'online', 'disconnect', and
'listening' events which are emitted on worker and cluster, also emit
'message' on cluster.

Reviewed-by: Sam Roberts <vieuxtech@gmail.com>
Reviewed-by: Christian Tellnes <christian@tellnes.no>
Reviewed-by: Stephen Belanger <admin@stephenbelanger.com>
PR-URL: https://github.com/nodejs/io.js/pull/861
v4.0.0-rc
Sam Roberts 10 years ago
parent
commit
66fc8ca22b
  1. 12
      doc/api/cluster.markdown
  2. 2
      lib/cluster.js
  3. 13
      test/parallel/test-cluster-message.js

12
doc/api/cluster.markdown

@ -248,6 +248,16 @@ This can be used to restart the worker by calling `.fork()` again.
See [child_process event: 'exit'](child_process.html#child_process_event_exit). See [child_process event: 'exit'](child_process.html#child_process_event_exit).
## Event: 'message'
* `worker` {Worker object}
* `message` {Object}
Emitted when any worker receives a message.
See
[child_process event: 'message'](child_process.html#child_process_event_message).
## Event: 'setup' ## Event: 'setup'
* `settings` {Object} * `settings` {Object}
@ -530,6 +540,8 @@ created. It is disconnected after the `disconnect` event is emitted.
* `message` {Object} * `message` {Object}
Similar to the `cluster.on('message')` event, but specific to this worker.
This event is the same as the one provided by `child_process.fork()`. This event is the same as the one provided by `child_process.fork()`.
In a worker you can also use `process.on('message')`. In a worker you can also use `process.on('message')`.

2
lib/cluster.js

@ -319,6 +319,8 @@ function masterInit() {
process: workerProcess process: workerProcess
}); });
worker.on('message', this.emit.bind(this, 'message'));
function removeWorker(worker) { function removeWorker(worker) {
assert(worker); assert(worker);

13
test/parallel/test-cluster-message.js

@ -6,7 +6,7 @@ var net = require('net');
function forEach(obj, fn) { function forEach(obj, fn) {
Object.keys(obj).forEach(function(name, index) { Object.keys(obj).forEach(function(name, index) {
fn(obj[name], name, index); fn(obj[name], name);
}); });
} }
@ -45,6 +45,10 @@ if (cluster.isWorker) {
else if (cluster.isMaster) { else if (cluster.isMaster) {
var checks = { var checks = {
global: {
'receive': false,
'correct': false
},
master: { master: {
'receive': false, 'receive': false,
'correct': false 'correct': false
@ -76,12 +80,15 @@ else if (cluster.isMaster) {
// Spawn worker // Spawn worker
var worker = cluster.fork(); var worker = cluster.fork();
// When a IPC message is received form the worker // When a IPC message is received from the worker
worker.on('message', function(message) { worker.on('message', function(message) {
check('master', message === 'message from worker'); check('master', message === 'message from worker');
}); });
cluster.on('message', function(message) {
check('global', message === 'message from worker');
});
// When a TCP connection is made with the worker connect to it // When a TCP server is listening in the worker connect to it
worker.on('listening', function() { worker.on('listening', function() {
client = net.connect(common.PORT, function() { client = net.connect(common.PORT, function() {

Loading…
Cancel
Save