workerPools.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPool = getPool;
  6. var _os = _interopRequireDefault(require("os"));
  7. var _WorkerPool = _interopRequireDefault(require("./WorkerPool"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. const workerPools = Object.create(null);
  10. function calculateNumberOfWorkers() {
  11. // There are situations when this call will return undefined so
  12. // we are fallback here to 1.
  13. // More info on: https://github.com/nodejs/node/issues/19022
  14. const cpus = _os.default.cpus() || {
  15. length: 1
  16. };
  17. return Math.max(1, cpus.length - 1);
  18. }
  19. function getPool(options) {
  20. const workerPoolOptions = {
  21. name: options.name || '',
  22. numberOfWorkers: options.workers || calculateNumberOfWorkers(),
  23. workerNodeArgs: options.workerNodeArgs,
  24. workerParallelJobs: options.workerParallelJobs || 20,
  25. poolTimeout: options.poolTimeout || 500,
  26. poolParallelJobs: options.poolParallelJobs || 200,
  27. poolRespawn: options.poolRespawn || false
  28. };
  29. const tpKey = JSON.stringify(workerPoolOptions);
  30. workerPools[tpKey] = workerPools[tpKey] || new _WorkerPool.default(workerPoolOptions);
  31. return workerPools[tpKey];
  32. } // eslint-disable-line import/prefer-default-export