index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. Object.defineProperty(exports, 'FifoQueue', {
  6. enumerable: true,
  7. get: function () {
  8. return _FifoQueue.default;
  9. }
  10. });
  11. Object.defineProperty(exports, 'PriorityQueue', {
  12. enumerable: true,
  13. get: function () {
  14. return _PriorityQueue.default;
  15. }
  16. });
  17. exports.Worker = void 0;
  18. Object.defineProperty(exports, 'messageParent', {
  19. enumerable: true,
  20. get: function () {
  21. return _messageParent.default;
  22. }
  23. });
  24. function _os() {
  25. const data = require('os');
  26. _os = function () {
  27. return data;
  28. };
  29. return data;
  30. }
  31. var _Farm = _interopRequireDefault(require('./Farm'));
  32. var _WorkerPool = _interopRequireDefault(require('./WorkerPool'));
  33. var _PriorityQueue = _interopRequireDefault(require('./PriorityQueue'));
  34. var _FifoQueue = _interopRequireDefault(require('./FifoQueue'));
  35. var _messageParent = _interopRequireDefault(require('./workers/messageParent'));
  36. function _interopRequireDefault(obj) {
  37. return obj && obj.__esModule ? obj : {default: obj};
  38. }
  39. function _defineProperty(obj, key, value) {
  40. if (key in obj) {
  41. Object.defineProperty(obj, key, {
  42. value: value,
  43. enumerable: true,
  44. configurable: true,
  45. writable: true
  46. });
  47. } else {
  48. obj[key] = value;
  49. }
  50. return obj;
  51. }
  52. function getExposedMethods(workerPath, options) {
  53. let exposedMethods = options.exposedMethods; // If no methods list is given, try getting it by auto-requiring the module.
  54. if (!exposedMethods) {
  55. const module = require(workerPath);
  56. exposedMethods = Object.keys(module).filter(
  57. // @ts-expect-error: no index
  58. name => typeof module[name] === 'function'
  59. );
  60. if (typeof module === 'function') {
  61. exposedMethods = [...exposedMethods, 'default'];
  62. }
  63. }
  64. return exposedMethods;
  65. }
  66. /**
  67. * The Jest farm (publicly called "Worker") is a class that allows you to queue
  68. * methods across multiple child processes, in order to parallelize work. This
  69. * is done by providing an absolute path to a module that will be loaded on each
  70. * of the child processes, and bridged to the main process.
  71. *
  72. * Bridged methods are specified by using the "exposedMethods" property of the
  73. * "options" object. This is an array of strings, where each of them corresponds
  74. * to the exported name in the loaded module.
  75. *
  76. * You can also control the amount of workers by using the "numWorkers" property
  77. * of the "options" object, and the settings passed to fork the process through
  78. * the "forkOptions" property. The amount of workers defaults to the amount of
  79. * CPUS minus one.
  80. *
  81. * Queueing calls can be done in two ways:
  82. * - Standard method: calls will be redirected to the first available worker,
  83. * so they will get executed as soon as they can.
  84. *
  85. * - Sticky method: if a "computeWorkerKey" method is provided within the
  86. * config, the resulting string of this method will be used as a key.
  87. * Every time this key is returned, it is guaranteed that your job will be
  88. * processed by the same worker. This is specially useful if your workers
  89. * are caching results.
  90. */
  91. class Worker {
  92. constructor(workerPath, options) {
  93. var _this$_options$enable,
  94. _this$_options$forkOp,
  95. _this$_options$maxRet,
  96. _this$_options$numWor,
  97. _this$_options$resour,
  98. _this$_options$setupA;
  99. _defineProperty(this, '_ending', void 0);
  100. _defineProperty(this, '_farm', void 0);
  101. _defineProperty(this, '_options', void 0);
  102. _defineProperty(this, '_workerPool', void 0);
  103. this._options = {...options};
  104. this._ending = false;
  105. const workerPoolOptions = {
  106. enableWorkerThreads:
  107. (_this$_options$enable = this._options.enableWorkerThreads) !== null &&
  108. _this$_options$enable !== void 0
  109. ? _this$_options$enable
  110. : false,
  111. forkOptions:
  112. (_this$_options$forkOp = this._options.forkOptions) !== null &&
  113. _this$_options$forkOp !== void 0
  114. ? _this$_options$forkOp
  115. : {},
  116. maxRetries:
  117. (_this$_options$maxRet = this._options.maxRetries) !== null &&
  118. _this$_options$maxRet !== void 0
  119. ? _this$_options$maxRet
  120. : 3,
  121. numWorkers:
  122. (_this$_options$numWor = this._options.numWorkers) !== null &&
  123. _this$_options$numWor !== void 0
  124. ? _this$_options$numWor
  125. : Math.max((0, _os().cpus)().length - 1, 1),
  126. resourceLimits:
  127. (_this$_options$resour = this._options.resourceLimits) !== null &&
  128. _this$_options$resour !== void 0
  129. ? _this$_options$resour
  130. : {},
  131. setupArgs:
  132. (_this$_options$setupA = this._options.setupArgs) !== null &&
  133. _this$_options$setupA !== void 0
  134. ? _this$_options$setupA
  135. : []
  136. };
  137. if (this._options.WorkerPool) {
  138. // @ts-expect-error: constructor target any?
  139. this._workerPool = new this._options.WorkerPool(
  140. workerPath,
  141. workerPoolOptions
  142. );
  143. } else {
  144. this._workerPool = new _WorkerPool.default(workerPath, workerPoolOptions);
  145. }
  146. this._farm = new _Farm.default(
  147. workerPoolOptions.numWorkers,
  148. this._workerPool.send.bind(this._workerPool),
  149. {
  150. computeWorkerKey: this._options.computeWorkerKey,
  151. taskQueue: this._options.taskQueue,
  152. workerSchedulingPolicy: this._options.workerSchedulingPolicy
  153. }
  154. );
  155. this._bindExposedWorkerMethods(workerPath, this._options);
  156. }
  157. _bindExposedWorkerMethods(workerPath, options) {
  158. getExposedMethods(workerPath, options).forEach(name => {
  159. if (name.startsWith('_')) {
  160. return;
  161. }
  162. if (this.constructor.prototype.hasOwnProperty(name)) {
  163. throw new TypeError('Cannot define a method called ' + name);
  164. } // @ts-expect-error: dynamic extension of the class instance is expected.
  165. this[name] = this._callFunctionWithArgs.bind(this, name);
  166. });
  167. }
  168. _callFunctionWithArgs(method, ...args) {
  169. if (this._ending) {
  170. throw new Error('Farm is ended, no more calls can be done to it');
  171. }
  172. return this._farm.doWork(method, ...args);
  173. }
  174. getStderr() {
  175. return this._workerPool.getStderr();
  176. }
  177. getStdout() {
  178. return this._workerPool.getStdout();
  179. }
  180. async end() {
  181. if (this._ending) {
  182. throw new Error('Farm is ended, no more calls can be done to it');
  183. }
  184. this._ending = true;
  185. return this._workerPool.end();
  186. }
  187. }
  188. exports.Worker = Worker;