ChildProcessWorker.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _child_process() {
  7. const data = _interopRequireDefault(require('child_process'));
  8. _child_process = function _child_process() {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _stream() {
  14. const data = require('stream');
  15. _stream = function _stream() {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _mergeStream() {
  21. const data = _interopRequireDefault(require('merge-stream'));
  22. _mergeStream = function _mergeStream() {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _supportsColor() {
  28. const data = _interopRequireDefault(require('supports-color'));
  29. _supportsColor = function _supportsColor() {
  30. return data;
  31. };
  32. return data;
  33. }
  34. var _types = require('../types');
  35. function _interopRequireDefault(obj) {
  36. return obj && obj.__esModule ? obj : {default: obj};
  37. }
  38. function _objectSpread(target) {
  39. for (var i = 1; i < arguments.length; i++) {
  40. var source = arguments[i] != null ? arguments[i] : {};
  41. var ownKeys = Object.keys(source);
  42. if (typeof Object.getOwnPropertySymbols === 'function') {
  43. ownKeys = ownKeys.concat(
  44. Object.getOwnPropertySymbols(source).filter(function(sym) {
  45. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  46. })
  47. );
  48. }
  49. ownKeys.forEach(function(key) {
  50. _defineProperty(target, key, source[key]);
  51. });
  52. }
  53. return target;
  54. }
  55. function _defineProperty(obj, key, value) {
  56. if (key in obj) {
  57. Object.defineProperty(obj, key, {
  58. value: value,
  59. enumerable: true,
  60. configurable: true,
  61. writable: true
  62. });
  63. } else {
  64. obj[key] = value;
  65. }
  66. return obj;
  67. }
  68. /**
  69. * This class wraps the child process and provides a nice interface to
  70. * communicate with. It takes care of:
  71. *
  72. * - Re-spawning the process if it dies.
  73. * - Queues calls while the worker is busy.
  74. * - Re-sends the requests if the worker blew up.
  75. *
  76. * The reason for queueing them here (since childProcess.send also has an
  77. * internal queue) is because the worker could be doing asynchronous work, and
  78. * this would lead to the child process to read its receiving buffer and start a
  79. * second call. By queueing calls here, we don't send the next call to the
  80. * children until we receive the result of the previous one.
  81. *
  82. * As soon as a request starts to be processed by a worker, its "processed"
  83. * field is changed to "true", so that other workers which might encounter the
  84. * same call skip it.
  85. */
  86. class ChildProcessWorker {
  87. constructor(options) {
  88. _defineProperty(this, '_child', void 0);
  89. _defineProperty(this, '_options', void 0);
  90. _defineProperty(this, '_onProcessEnd', void 0);
  91. _defineProperty(this, '_fakeStream', void 0);
  92. _defineProperty(this, '_request', void 0);
  93. _defineProperty(this, '_retries', void 0);
  94. _defineProperty(this, '_stderr', void 0);
  95. _defineProperty(this, '_stdout', void 0);
  96. this._options = options;
  97. this._fakeStream = null;
  98. this._request = null;
  99. this._stderr = null;
  100. this._stdout = null;
  101. this.initialize();
  102. }
  103. initialize() {
  104. const forceColor = _supportsColor().default.stdout
  105. ? {
  106. FORCE_COLOR: '1'
  107. }
  108. : {};
  109. const child = _child_process().default.fork(
  110. require.resolve('./processChild'),
  111. [],
  112. _objectSpread(
  113. {
  114. cwd: process.cwd(),
  115. env: _objectSpread(
  116. {},
  117. process.env,
  118. {
  119. JEST_WORKER_ID: String(this._options.workerId + 1)
  120. },
  121. forceColor
  122. ),
  123. // Suppress --debug / --inspect flags while preserving others (like --harmony).
  124. execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
  125. silent: true
  126. },
  127. this._options.forkOptions
  128. )
  129. );
  130. if (child.stdout) {
  131. if (!this._stdout) {
  132. // We need to add a permanent stream to the merged stream to prevent it
  133. // from ending when the subprocess stream ends
  134. this._stdout = (0, _mergeStream().default)(this._getFakeStream());
  135. }
  136. this._stdout.add(child.stdout);
  137. }
  138. if (child.stderr) {
  139. if (!this._stderr) {
  140. // We need to add a permanent stream to the merged stream to prevent it
  141. // from ending when the subprocess stream ends
  142. this._stderr = (0, _mergeStream().default)(this._getFakeStream());
  143. }
  144. this._stderr.add(child.stderr);
  145. }
  146. child.on('message', this.onMessage.bind(this));
  147. child.on('exit', this.onExit.bind(this));
  148. child.send([
  149. _types.CHILD_MESSAGE_INITIALIZE,
  150. false,
  151. this._options.workerPath,
  152. this._options.setupArgs
  153. ]);
  154. this._child = child;
  155. this._retries++; // If we exceeded the amount of retries, we will emulate an error reply
  156. // coming from the child. This avoids code duplication related with cleaning
  157. // the queue, and scheduling the next call.
  158. if (this._retries > this._options.maxRetries) {
  159. const error = new Error('Call retries were exceeded');
  160. this.onMessage([
  161. _types.PARENT_MESSAGE_CLIENT_ERROR,
  162. error.name,
  163. error.message,
  164. error.stack,
  165. {
  166. type: 'WorkerError'
  167. }
  168. ]);
  169. }
  170. }
  171. _shutdown() {
  172. // End the temporary streams so the merged streams end too
  173. if (this._fakeStream) {
  174. this._fakeStream.end();
  175. this._fakeStream = null;
  176. }
  177. }
  178. onMessage(response) {
  179. let error;
  180. switch (response[0]) {
  181. case _types.PARENT_MESSAGE_OK:
  182. this._onProcessEnd(null, response[1]);
  183. break;
  184. case _types.PARENT_MESSAGE_CLIENT_ERROR:
  185. error = response[4];
  186. if (error != null && typeof error === 'object') {
  187. const extra = error; // @ts-ignore: no index
  188. const NativeCtor = global[response[1]];
  189. const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
  190. error = new Ctor(response[2]);
  191. error.type = response[1];
  192. error.stack = response[3];
  193. for (const key in extra) {
  194. // @ts-ignore: adding custom properties to errors.
  195. error[key] = extra[key];
  196. }
  197. }
  198. this._onProcessEnd(error, null);
  199. break;
  200. case _types.PARENT_MESSAGE_SETUP_ERROR:
  201. error = new Error('Error when calling setup: ' + response[2]); // @ts-ignore: adding custom properties to errors.
  202. error.type = response[1];
  203. error.stack = response[3];
  204. this._onProcessEnd(error, null);
  205. break;
  206. default:
  207. throw new TypeError('Unexpected response from worker: ' + response[0]);
  208. }
  209. }
  210. onExit(exitCode) {
  211. if (exitCode !== 0) {
  212. this.initialize();
  213. if (this._request) {
  214. this._child.send(this._request);
  215. }
  216. } else {
  217. this._shutdown();
  218. }
  219. }
  220. send(request, onProcessStart, onProcessEnd) {
  221. onProcessStart(this);
  222. this._onProcessEnd = (...args) => {
  223. // Clean the request to avoid sending past requests to workers that fail
  224. // while waiting for a new request (timers, unhandled rejections...)
  225. this._request = null;
  226. return onProcessEnd(...args);
  227. };
  228. this._request = request;
  229. this._retries = 0;
  230. this._child.send(request);
  231. }
  232. getWorkerId() {
  233. return this._options.workerId;
  234. }
  235. getStdout() {
  236. return this._stdout;
  237. }
  238. getStderr() {
  239. return this._stderr;
  240. }
  241. _getFakeStream() {
  242. if (!this._fakeStream) {
  243. this._fakeStream = new (_stream()).PassThrough();
  244. }
  245. return this._fakeStream;
  246. }
  247. }
  248. exports.default = ChildProcessWorker;