cluster: support stdio option for workers · nodejs/node@75c6d9d

3 files changed

lines changed

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`.

Original file line numberDiff line numberDiff line change

@@ -322,6 +322,7 @@ function masterInit() {

322322

env: workerEnv,

323323

silent: cluster.settings.silent,

324324

execArgv: execArgv,

325+

stdio: cluster.settings.stdio,

325326

gid: cluster.settings.gid,

326327

uid: cluster.settings.uid

327328

});

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+

}