cluster.setupPrimary([settings])
settings
<Object> 参见cluster.settings
。
setupPrimary
用于更改默认的 'fork' 行为。
调用后,设置将出现在 cluster.settings
中。
任何设置更改只会影响未来对 .fork()
的调用,而不会影响已经运行的工作进程。
唯一不能通过 .setupPrimary()
设置的工作进程属性是传给 .fork()
的 env
。
上述默认值仅适用于第一次调用;以后调用的默认值是调用 cluster.setupPrimary()
时的当前值。
import cluster from 'node:cluster';
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'https'],
silent: true,
});
cluster.fork(); // https 工作进程
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'http'],
});
cluster.fork(); // http 工作进程
const cluster = require('node:cluster');
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'https'],
silent: true,
});
cluster.fork(); // https 工作进程
cluster.setupPrimary({
exec: 'worker.js',
args: ['--use', 'http'],
});
cluster.fork(); // http 工作进程
这只能从主进程调用。
settings
<Object> Seecluster.settings
.
setupPrimary
is used to change the default 'fork' behavior. Once called,
the settings will be present in cluster.settings
.
Any settings changes only affect future calls to .fork()
and have no
effect on workers that are already running.
The only attribute of a worker that cannot be set via .setupPrimary()
is
the env
passed to .fork()
.
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 worker
const 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.