diff --git a/README.md b/README.md index f882b5d..ab278c8 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,8 @@ use: [ loader: "thread-loader", // loaders with equal options will share worker pools options: { - // the number of spawned workers, defaults to number of cpus + // the number of spawned workers, defaults to (number of cpus - 1) or + // fallback to 1 when require('os').cpus() is undefined workers: 2, // number of jobs a worker processes in parallel diff --git a/src/workerPools.js b/src/workerPools.js index 3a81b7a..a2204c5 100644 --- a/src/workerPools.js +++ b/src/workerPools.js @@ -3,10 +3,19 @@ import WorkerPool from './WorkerPool'; const workerPools = Object.create(null); +function calculateNumberOfWorkers() { + // There are situations when this call will return undefined so + // we are fallback here to 1. + // More info on: https://github.com/nodejs/node/issues/19022 + const cpus = os.cpus() || { length: 1 }; + + return Math.max(1, cpus.length - 1); +} + function getPool(options) { const workerPoolOptions = { name: options.name || '', - numberOfWorkers: options.workers || os.cpus().length, + numberOfWorkers: options.workers || calculateNumberOfWorkers(), workerNodeArgs: options.workerNodeArgs, workerParallelJobs: options.workerParallelJobs || 20, poolTimeout: options.poolTimeout || 500,