Skip to content
Open
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
45 changes: 45 additions & 0 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,45 @@ Spawn a new worker process.

This can only be called from the primary process.

## `cluster.getSettings()`

<!-- YAML
added: REPLACEME
-->

* Returns: {Object}
* `execArgv` {string\[]} List of string arguments passed to the Node.js
executable. **Default:** `process.execArgv`.
* `exec` {string} File path to worker file. **Default:** `process.argv[1]`.
* `args` {string\[]} 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).
* `serialization` {string} Specify the kind of serialization used for sending
messages between processes. Possible values are `'json'` and `'advanced'`.
See [Advanced serialization for `child_process`][] for more details.
**Default:** `false`.
* `schedulingPolicy` {string} Scheduling policy to use between processes.
**Default:** `cluster.SCHED_RR`. See \[`cluster.schedulingPolicy`]\[] for
details.
* `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`. See
[`child_process.spawn()`][]'s [`stdio`][].
* `uid` {number} Sets the user identity of the process. (See setuid(2).)
* `gid` {number} Sets the group identity of the process. (See setgid(2).)
* `inspectPort` {number|Function} Sets inspector port of worker.
This can be a number, or a function that takes no arguments and returns a
number. By default each worker gets its own port, incremented from the
primary's `process.debugPort`.
* `windowsHide` {boolean} Hide the forked processes console window that would
normally be created on Windows systems. **Default:** `false`.

After calling [`.setupPrimary()`][] (or [`.fork()`][]) this function will return
the cluster settings, including the default values.

## `cluster.isMaster`

<!-- YAML
Expand Down Expand Up @@ -900,6 +939,9 @@ values are `'rr'` and `'none'`.
<!-- YAML
added: v0.7.1
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/49292
description: The `schedulingPolicy` option is supported now.
- version:
- v13.2.0
- v12.16.0
Expand Down Expand Up @@ -931,6 +973,9 @@ changes:
messages between processes. Possible values are `'json'` and `'advanced'`.
See [Advanced serialization for `child_process`][] for more details.
**Default:** `false`.
* `schedulingPolicy` {string} Scheduling policy to use between processes.
**Default:** `cluster.SCHED_RR`. See \[`cluster.schedulingPolicy`]\[] for
details.
* `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
11 changes: 8 additions & 3 deletions lib/internal/cluster/primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ cluster.SCHED_RR = SCHED_RR; // Primary distributes connections.
let ids = 0;
let initialized = false;

// XXX(bnoordhuis) Fold cluster.schedulingPolicy into cluster.settings?
let schedulingPolicy = process.env.NODE_CLUSTER_SCHED_POLICY;
if (schedulingPolicy === 'rr')
schedulingPolicy = SCHED_RR;
Expand All @@ -65,6 +64,7 @@ cluster.setupPrimary = function(options) {
exec: process.argv[1],
execArgv: process.execArgv,
silent: false,
schedulingPolicy: cluster.schedulingPolicy,
...cluster.settings,
...options,
};
Expand All @@ -86,9 +86,10 @@ cluster.setupPrimary = function(options) {
return process.nextTick(setupSettingsNT, settings);

initialized = true;
schedulingPolicy = cluster.schedulingPolicy; // Freeze policy.
schedulingPolicy = settings.schedulingPolicy; // Freeze policy.
assert(schedulingPolicy === SCHED_NONE || schedulingPolicy === SCHED_RR,
`Bad cluster.schedulingPolicy: ${schedulingPolicy}`);
`Bad settings.schedulingPolicy: ${schedulingPolicy}`);
cluster.schedulingPolicy = schedulingPolicy; // Show to the world.

process.nextTick(setupSettingsNT, settings);

Expand Down Expand Up @@ -158,6 +159,10 @@ function removeHandlesForWorker(worker) {
});
}

cluster.getSettings = function() {
return { ...cluster.settings };
};

cluster.fork = function(env) {
cluster.setupPrimary();
const id = ++ids;
Expand Down
13 changes: 13 additions & 0 deletions test/known_issues/test-cluster-import-scheduling-policy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Refs: https://github.com/nodejs/node/issues/49240
// When importing cluster in ESM, cluster.schedulingPolicy cannot be set;
// and the env variable doesn't work since imports are hoisted to the top.
// Therefore the scheduling policy is still cluster.SCHED_RR.
import '../common/index.mjs';
import assert from 'node:assert';
import * as cluster from 'cluster';


assert.strictEqual(cluster.schedulingPolicy, cluster.SCHED_RR);
cluster.setupPrimary({ schedulingPolicy: cluster.SCHED_NONE });
const settings = cluster.getSettings();
assert.strictEqual(settings.schedulingPolicy, cluster.SCHED_RR);
2 changes: 2 additions & 0 deletions test/parallel/test-cluster-setup-primary-cumulative.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ assert.deepStrictEqual(cluster.settings, {
exec: process.argv[1],
execArgv: process.execArgv,
silent: false,
schedulingPolicy: 2,
});
console.log('ok sets defaults');

Expand All @@ -58,5 +59,6 @@ assert.deepStrictEqual(cluster.settings, {
exec: 'overridden',
execArgv: ['baz', 'bang'],
silent: false,
schedulingPolicy: 2,
});
console.log('ok preserves current settings');