Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cluster: support stdio option for workers #7838

Merged
merged 1 commit into from
Aug 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.
Copy link
Member

@bnoordhuis bnoordhuis Jul 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should explain that fd 3 must be 'ipc', otherwise the cluster module won't work. It's unfortunate in that it leaks what is essentially an implementation detail.

EDIT: Or at least one 'ipc' element, if I read #7811 right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm... perhaps the implementation should simply override or error if ipc is not passed. If overridden, a warning can be emitted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will throw in child_process.fork() if there is no IPC. I'll update these docs accordingly.


`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);
});
}