workerPools.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPool = undefined;
  6. var _os = require('os');
  7. var _os2 = _interopRequireDefault(_os);
  8. var _WorkerPool = require('./WorkerPool');
  9. var _WorkerPool2 = _interopRequireDefault(_WorkerPool);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. const workerPools = Object.create(null);
  12. function calculateNumberOfWorkers() {
  13. // There are situations when this call will return undefined so
  14. // we are fallback here to 1.
  15. // More info on: https://github.com/nodejs/node/issues/19022
  16. const cpus = _os2.default.cpus() || { length: 1 };
  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 _WorkerPool2.default(workerPoolOptions);
  31. const workerPool = workerPools[tpKey];
  32. return workerPool;
  33. }
  34. exports.getPool = getPool; // eslint-disable-line import/prefer-default-export