cluster.setupPrimary([settings])
settings<Object> 参见cluster.settings。
setupPrimary 用于更改默认的 'fork' 行为。一旦调用,该设置将出现在 cluster.settings 中。
任何设置更改仅影响对 .fork() 的未来调用,对已运行的工作不会有影响。
【Any settings changes only affect future calls to .fork() and have no
effect on workers that are already running.】
通过 .setupPrimary() 无法设置的工人唯一属性是传递给 .fork() 的 env。
【The only attribute of a worker that cannot be set via .setupPrimary() is
the env passed to .fork().】
上述默认值仅适用于第一次调用;后续调用的默认值是在调用 cluster.setupPrimary() 时的当前值。
【The defaults above apply to the first call only; the defaults for later
calls are the current values at the time of cluster.setupPrimary() is called.】
import cluster from 'node:cluster';
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'https'],
silent: true,
});
cluster.fork(); // https worker
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'http'],
});
cluster.fork(); // http workerconst cluster = require('node:cluster');
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'https'],
silent: true,
});
cluster.fork(); // https worker
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'http'],
});
cluster.fork(); // http worker这只能从主进程调用。
【This can only be called from the primary process.】