error out when we can't spawn workers - fixes #586

Setting `clusterLimit` to 0 means no workers are started, which
usually breaks things. Also, some "hardening" things may prevent node
from seeing how many CPUs the machine has, which has the same effect.

With this commit we provide hopefully-useful error messages.
This commit is contained in:
dakkar 2024-07-21 11:23:49 +01:00
parent 69bdbf22b3
commit 3eff85a3d3
1 changed files with 10 additions and 1 deletions

View File

@ -180,7 +180,16 @@ async function connectDb(): Promise<void> {
*/
async function spawnWorkers(limit = 1) {
const workers = Math.min(limit, os.cpus().length);
const cpuCount = os.cpus().length;
const workers = Math.min(limit, cpuCount);
if (workers === 0) {
const cause = cpuCount === 0
? 'you seem to have no CPUs (this may be caused by some "hardening" system)'
: "`config.clusterLimit` is 0 (if you don't want to use clustering, set the environment variable `MK_DISABLE_CLUSTERING` to a non-empty value instead)";
bootLogger.error(`Configuration error: we can't create workers, ${cause}`, null, true);
process.exit(1);
}
bootLogger.info(`Starting ${workers} worker${workers === 1 ? '' : 's'}...`);
await Promise.all([...Array(workers)].map(spawnWorker));
bootLogger.succ('All workers started');