Skip to content

Commit

Permalink
cluster: add cwd to cluster.settings
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
cjihrig authored and rvagg committed Aug 16, 2018
1 parent 76805f0 commit dc000a5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,8 @@ changes:
* `exec` {string} File path to worker file. **Default:** `process.argv[1]`
* `args` {Array} String arguments passed to worker.
**Default:** `process.argv.slice(2)`
* `cwd` {string} Current working directory of the worker process. **Default:**
`undefined` (inherits from parent process)
* `silent` {boolean} Whether or not to send output to parent's stdio.
**Default:** `false`
* `stdio` {Array} Configures the stdio of forked processes. Because the
Expand Down
1 change: 1 addition & 0 deletions lib/internal/cluster/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ function createWorkerProcess(id, env) {
}

return fork(cluster.settings.exec, cluster.settings.args, {
cwd: cluster.settings.cwd,
env: workerEnv,
silent: cluster.settings.silent,
windowsHide: cluster.settings.windowsHide,
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-cluster-cwd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const cluster = require('cluster');

if (cluster.isMaster) {
tmpdir.refresh();

assert.strictEqual(cluster.settings.cwd, undefined);
cluster.fork().on('message', common.mustCall((msg) => {
assert.strictEqual(msg, process.cwd());
}));

cluster.setupMaster({ cwd: tmpdir.path });
assert.strictEqual(cluster.settings.cwd, tmpdir.path);
cluster.fork().on('message', common.mustCall((msg) => {
assert.strictEqual(msg, tmpdir.path);
}));
} else {
process.send(process.cwd());
process.disconnect();
}

0 comments on commit dc000a5

Please sign in to comment.