ChildProcessWorker.js 7.9 KB

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