source: trip-planner-front/node_modules/jest-worker/build/base/BaseWorkerPool.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function path() {
9 const data = _interopRequireWildcard(require('path'));
10
11 path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _mergeStream() {
19 const data = _interopRequireDefault(require('merge-stream'));
20
21 _mergeStream = function () {
22 return data;
23 };
24
25 return data;
26}
27
28var _types = require('../types');
29
30function _interopRequireDefault(obj) {
31 return obj && obj.__esModule ? obj : {default: obj};
32}
33
34function _getRequireWildcardCache(nodeInterop) {
35 if (typeof WeakMap !== 'function') return null;
36 var cacheBabelInterop = new WeakMap();
37 var cacheNodeInterop = new WeakMap();
38 return (_getRequireWildcardCache = function (nodeInterop) {
39 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
40 })(nodeInterop);
41}
42
43function _interopRequireWildcard(obj, nodeInterop) {
44 if (!nodeInterop && obj && obj.__esModule) {
45 return obj;
46 }
47 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
48 return {default: obj};
49 }
50 var cache = _getRequireWildcardCache(nodeInterop);
51 if (cache && cache.has(obj)) {
52 return cache.get(obj);
53 }
54 var newObj = {};
55 var hasPropertyDescriptor =
56 Object.defineProperty && Object.getOwnPropertyDescriptor;
57 for (var key in obj) {
58 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
59 var desc = hasPropertyDescriptor
60 ? Object.getOwnPropertyDescriptor(obj, key)
61 : null;
62 if (desc && (desc.get || desc.set)) {
63 Object.defineProperty(newObj, key, desc);
64 } else {
65 newObj[key] = obj[key];
66 }
67 }
68 }
69 newObj.default = obj;
70 if (cache) {
71 cache.set(obj, newObj);
72 }
73 return newObj;
74}
75
76function _defineProperty(obj, key, value) {
77 if (key in obj) {
78 Object.defineProperty(obj, key, {
79 value: value,
80 enumerable: true,
81 configurable: true,
82 writable: true
83 });
84 } else {
85 obj[key] = value;
86 }
87 return obj;
88}
89
90// How long to wait for the child process to terminate
91// after CHILD_MESSAGE_END before sending force exiting.
92const FORCE_EXIT_DELAY = 500;
93/* istanbul ignore next */
94
95const emptyMethod = () => {};
96
97class BaseWorkerPool {
98 constructor(workerPath, options) {
99 _defineProperty(this, '_stderr', void 0);
100
101 _defineProperty(this, '_stdout', void 0);
102
103 _defineProperty(this, '_options', void 0);
104
105 _defineProperty(this, '_workers', void 0);
106
107 this._options = options;
108 this._workers = new Array(options.numWorkers);
109
110 if (!path().isAbsolute(workerPath)) {
111 workerPath = require.resolve(workerPath);
112 }
113
114 const stdout = (0, _mergeStream().default)();
115 const stderr = (0, _mergeStream().default)();
116 const {forkOptions, maxRetries, resourceLimits, setupArgs} = options;
117
118 for (let i = 0; i < options.numWorkers; i++) {
119 const workerOptions = {
120 forkOptions,
121 maxRetries,
122 resourceLimits,
123 setupArgs,
124 workerId: i,
125 workerPath
126 };
127 const worker = this.createWorker(workerOptions);
128 const workerStdout = worker.getStdout();
129 const workerStderr = worker.getStderr();
130
131 if (workerStdout) {
132 stdout.add(workerStdout);
133 }
134
135 if (workerStderr) {
136 stderr.add(workerStderr);
137 }
138
139 this._workers[i] = worker;
140 }
141
142 this._stdout = stdout;
143 this._stderr = stderr;
144 }
145
146 getStderr() {
147 return this._stderr;
148 }
149
150 getStdout() {
151 return this._stdout;
152 }
153
154 getWorkers() {
155 return this._workers;
156 }
157
158 getWorkerById(workerId) {
159 return this._workers[workerId];
160 }
161
162 createWorker(_workerOptions) {
163 throw Error('Missing method createWorker in WorkerPool');
164 }
165
166 async end() {
167 // We do not cache the request object here. If so, it would only be only
168 // processed by one of the workers, and we want them all to close.
169 const workerExitPromises = this._workers.map(async worker => {
170 worker.send(
171 [_types.CHILD_MESSAGE_END, false],
172 emptyMethod,
173 emptyMethod,
174 emptyMethod
175 ); // Schedule a force exit in case worker fails to exit gracefully so
176 // await worker.waitForExit() never takes longer than FORCE_EXIT_DELAY
177
178 let forceExited = false;
179 const forceExitTimeout = setTimeout(() => {
180 worker.forceExit();
181 forceExited = true;
182 }, FORCE_EXIT_DELAY);
183 await worker.waitForExit(); // Worker ideally exited gracefully, don't send force exit then
184
185 clearTimeout(forceExitTimeout);
186 return forceExited;
187 });
188
189 const workerExits = await Promise.all(workerExitPromises);
190 return workerExits.reduce(
191 (result, forceExited) => ({
192 forceExited: result.forceExited || forceExited
193 }),
194 {
195 forceExited: false
196 }
197 );
198 }
199}
200
201exports.default = BaseWorkerPool;
Note: See TracBrowser for help on using the repository browser.