Skip to content

Commit dc000a5

Browse files
cjihrigrvagg
authored andcommitted
cluster: add cwd to cluster.settings
This commit allows cluster workers to be created with configurable working directories. Fixes: #16388 PR-URL: #18399 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 76805f0 commit dc000a5

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

doc/api/cluster.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ changes:
745745
* `exec` {string} File path to worker file. **Default:** `process.argv[1]`
746746
* `args` {Array} String arguments passed to worker.
747747
**Default:** `process.argv.slice(2)`
748+
* `cwd` {string} Current working directory of the worker process. **Default:**
749+
`undefined` (inherits from parent process)
748750
* `silent` {boolean} Whether or not to send output to parent's stdio.
749751
**Default:** `false`
750752
* `stdio` {Array} Configures the stdio of forked processes. Because the

lib/internal/cluster/master.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function createWorkerProcess(id, env) {
129129
}
130130

131131
return fork(cluster.settings.exec, cluster.settings.args, {
132+
cwd: cluster.settings.cwd,
132133
env: workerEnv,
133134
silent: cluster.settings.silent,
134135
windowsHide: cluster.settings.windowsHide,

test/parallel/test-cluster-cwd.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
const common = require('../common');
3+
const tmpdir = require('../common/tmpdir');
4+
const assert = require('assert');
5+
const cluster = require('cluster');
6+
7+
if (cluster.isMaster) {
8+
tmpdir.refresh();
9+
10+
assert.strictEqual(cluster.settings.cwd, undefined);
11+
cluster.fork().on('message', common.mustCall((msg) => {
12+
assert.strictEqual(msg, process.cwd());
13+
}));
14+
15+
cluster.setupMaster({ cwd: tmpdir.path });
16+
assert.strictEqual(cluster.settings.cwd, tmpdir.path);
17+
cluster.fork().on('message', common.mustCall((msg) => {
18+
assert.strictEqual(msg, tmpdir.path);
19+
}));
20+
} else {
21+
process.send(process.cwd());
22+
process.disconnect();
23+
}

0 commit comments

Comments
 (0)