Skip to content

Commit

Permalink
cluster: support stdio option for workers
Browse files Browse the repository at this point in the history
This commit allows setupMaster() to configure the stdio channels
for worker processes.

Refs: nodejs/node-v0.x-archive#5727
Refs: #7811
PR-URL: #7838
Reviewed-By: Santiago Gimeno <santiago.gimeno@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
cjihrig committed Aug 1, 2016
1 parent 6d9a500 commit 75c6d9d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,9 @@ values are `"rr"` and `"none"`.
(Default=`process.argv.slice(2)`)
* `silent` {Boolean} whether or not to send output to parent's stdio.
(Default=`false`)
* `stdio` {Array} Configures the stdio of forked processes. Because the
cluster module relies on IPC to function, this configuration must contain an
`'ipc'` entry. When this option is provided, it overrides `silent`.
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)

Expand All @@ -642,6 +645,8 @@ This object is not supposed to be changed or set manually, by you.
(Default=`process.argv.slice(2)`)
* `silent` {Boolean} whether or not to send output to parent's stdio.
(Default=`false`)
* `stdio` {Array} Configures the stdio of forked processes. When this option
is provided, it overrides `silent`.

`setupMaster` is used to change the default 'fork' behavior. Once called,
the settings will be present in `cluster.settings`.
Expand Down
1 change: 1 addition & 0 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ function masterInit() {
env: workerEnv,
silent: cluster.settings.silent,
execArgv: execArgv,
stdio: cluster.settings.stdio,
gid: cluster.settings.gid,
uid: cluster.settings.uid
});
Expand Down
40 changes: 40 additions & 0 deletions test/parallel/test-cluster-fork-stdio.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
const net = require('net');

if (cluster.isMaster) {
const buf = Buffer.from('foobar');

cluster.setupMaster({
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
});

const worker = cluster.fork();
const channel = worker.process.stdio[4];
let response = '';

worker.on('exit', common.mustCall((code, signal) => {
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
}));

channel.setEncoding('utf8');
channel.on('data', (data) => {
response += data;

if (response === buf.toString()) {
worker.disconnect();
}
});
channel.write(buf);
} else {
const pipe = new net.Socket({ fd: 4 });

pipe.unref();
pipe.on('data', (data) => {
assert.ok(data instanceof Buffer);
pipe.write(data);
});
}

0 comments on commit 75c6d9d

Please sign in to comment.