Skip to content

Commit a4f0b13

Browse files
committed
cluster: support stdio option for workers
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>
1 parent 0094adc commit a4f0b13

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/api/cluster.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ values are `"rr"` and `"none"`.
626626
(Default=`process.argv.slice(2)`)
627627
* `silent` {Boolean} whether or not to send output to parent's stdio.
628628
(Default=`false`)
629+
* `stdio` {Array} Configures the stdio of forked processes. Because the
630+
cluster module relies on IPC to function, this configuration must contain an
631+
`'ipc'` entry. When this option is provided, it overrides `silent`.
629632
* `uid` {Number} Sets the user identity of the process. (See setuid(2).)
630633
* `gid` {Number} Sets the group identity of the process. (See setgid(2).)
631634

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

646651
`setupMaster` is used to change the default 'fork' behavior. Once called,
647652
the settings will be present in `cluster.settings`.

lib/cluster.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ function masterInit() {
321321
env: workerEnv,
322322
silent: cluster.settings.silent,
323323
execArgv: execArgv,
324+
stdio: cluster.settings.stdio,
324325
gid: cluster.settings.gid,
325326
uid: cluster.settings.uid
326327
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const cluster = require('cluster');
5+
const net = require('net');
6+
7+
if (cluster.isMaster) {
8+
const buf = Buffer.from('foobar');
9+
10+
cluster.setupMaster({
11+
stdio: ['pipe', 'pipe', 'pipe', 'ipc', 'pipe']
12+
});
13+
14+
const worker = cluster.fork();
15+
const channel = worker.process.stdio[4];
16+
let response = '';
17+
18+
worker.on('exit', common.mustCall((code, signal) => {
19+
assert.strictEqual(code, 0);
20+
assert.strictEqual(signal, null);
21+
}));
22+
23+
channel.setEncoding('utf8');
24+
channel.on('data', (data) => {
25+
response += data;
26+
27+
if (response === buf.toString()) {
28+
worker.disconnect();
29+
}
30+
});
31+
channel.write(buf);
32+
} else {
33+
const pipe = new net.Socket({ fd: 4 });
34+
35+
pipe.unref();
36+
pipe.on('data', (data) => {
37+
assert.ok(data instanceof Buffer);
38+
pipe.write(data);
39+
});
40+
}

0 commit comments

Comments
 (0)