diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 936a535977c5cb..937d3311bca4fe 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -840,6 +840,45 @@ Spawn a new worker process. This can only be called from the primary process. +## `cluster.getSettings()` + + + +* 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` -* Type: {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`. - * `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 settings object will -contain the settings, including the default values. +> Stability: 0 - Deprecated -This object is not intended to be changed or set manually. +Deprecated alias for [`cluster.getSettings()`][]. ## `cluster.setupMaster([settings])` diff --git a/lib/internal/cluster/primary.js b/lib/internal/cluster/primary.js index 6ab845c6d122aa..573933aebee921 100644 --- a/lib/internal/cluster/primary.js +++ b/lib/internal/cluster/primary.js @@ -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; @@ -65,6 +64,7 @@ cluster.setupPrimary = function(options) { exec: process.argv[1], execArgv: process.execArgv, silent: false, + schedulingPolicy: cluster.schedulingPolicy, ...cluster.settings, ...options, }; @@ -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); @@ -158,6 +159,10 @@ function removeHandlesForWorker(worker) { } } +cluster.getSettings = function() { + return { ...cluster.settings }; +}; + cluster.fork = function(env) { cluster.setupPrimary(); const id = ++ids; diff --git a/test/known_issues/test-cluster-import-scheduling-policy.mjs b/test/known_issues/test-cluster-import-scheduling-policy.mjs new file mode 100644 index 00000000000000..658935aaf7bf84 --- /dev/null +++ b/test/known_issues/test-cluster-import-scheduling-policy.mjs @@ -0,0 +1,16 @@ +// 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(); +// configured scheduling policy is now cluster.SCHED_NONE. +assert.strictEqual(settings.schedulingPolicy, cluster.SCHED_NONE); +// current scheduling policy is still cluster.SCHED_RR. +assert.strictEqual(cluster.schedulingPolicy, cluster.SCHED_RR); diff --git a/test/parallel/test-cluster-setup-primary-cumulative.js b/test/parallel/test-cluster-setup-primary-cumulative.js index cf62291e9d4ce0..fa1e1dc53e0de8 100644 --- a/test/parallel/test-cluster-setup-primary-cumulative.js +++ b/test/parallel/test-cluster-setup-primary-cumulative.js @@ -35,6 +35,7 @@ assert.deepStrictEqual(cluster.settings, { exec: process.argv[1], execArgv: process.execArgv, silent: false, + schedulingPolicy: 2, }); console.log('ok sets defaults'); @@ -58,5 +59,6 @@ assert.deepStrictEqual(cluster.settings, { exec: 'overridden', execArgv: ['baz', 'bang'], silent: false, + schedulingPolicy: 2, }); console.log('ok preserves current settings');