1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _child_process = require('child_process');
var _child_process2 = _interopRequireDefault(_child_process);
var _queue = require('neo-async/queue');
var _queue2 = _interopRequireDefault(_queue);
var _mapSeries = require('neo-async/mapSeries');
var _mapSeries2 = _interopRequireDefault(_mapSeries);
var _readBuffer = require('./readBuffer');
var _readBuffer2 = _interopRequireDefault(_readBuffer);
var _WorkerError = require('./WorkerError');
var _WorkerError2 = _interopRequireDefault(_WorkerError);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const workerPath = require.resolve('./worker'); /* eslint-disable no-console */
let workerId = 0;
class PoolWorker {
constructor(options, onJobDone) {
this.disposed = false;
this.nextJobId = 0;
this.jobs = Object.create(null);
this.activeJobs = 0;
this.onJobDone = onJobDone;
this.id = workerId;
workerId += 1;
this.worker = _child_process2.default.spawn(process.execPath, [].concat(options.nodeArgs || []).concat(workerPath, options.parallelJobs), {
detached: true,
stdio: ['ignore', 'pipe', 'pipe', 'pipe', 'pipe']
});
this.worker.unref();
// This prevents a problem where the worker stdio can be undefined
// when the kernel hits the limit of open files.
// More info can be found on: https://github.com/webpack-contrib/thread-loader/issues/2
if (!this.worker.stdio) {
throw new Error(`Failed to create the worker pool with workerId: ${workerId} and ${''}configuration: ${JSON.stringify(options)}. Please verify if you hit the OS open files limit.`);
}
const [,,, readPipe, writePipe] = this.worker.stdio;
this.readPipe = readPipe;
this.writePipe = writePipe;
this.listenStdOutAndErrFromWorker(this.worker.stdout, this.worker.stderr);
this.readNextMessage();
}
listenStdOutAndErrFromWorker(workerStdout, workerStderr) {
if (workerStdout) {
workerStdout.on('data', this.writeToStdout);
}
if (workerStderr) {
workerStderr.on('data', this.writeToStderr);
}
}
ignoreStdOutAndErrFromWorker(workerStdout, workerStderr) {
if (workerStdout) {
workerStdout.removeListener('data', this.writeToStdout);
}
if (workerStderr) {
workerStderr.removeListener('data', this.writeToStderr);
}
}
writeToStdout(data) {
if (!this.disposed) {
process.stdout.write(data);
}
}
writeToStderr(data) {
if (!this.disposed) {
process.stderr.write(data);
}
}
run(data, callback) {
const jobId = this.nextJobId;
this.nextJobId += 1;
this.jobs[jobId] = { data, callback };
this.activeJobs += 1;
this.writeJson({
type: 'job',
id: jobId,
data
});
}
warmup(requires) {
this.writeJson({
type: 'warmup',
requires
});
}
writeJson(data) {
const lengthBuffer = Buffer.alloc(4);
const messageBuffer = Buffer.from(JSON.stringify(data), 'utf-8');
lengthBuffer.writeInt32BE(messageBuffer.length, 0);
this.writePipe.write(lengthBuffer);
this.writePipe.write(messageBuffer);
}
writeEnd() {
const lengthBuffer = Buffer.alloc(4);
lengthBuffer.writeInt32BE(0, 0);
this.writePipe.write(lengthBuffer);
}
readNextMessage() {
this.state = 'read length';
this.readBuffer(4, (lengthReadError, lengthBuffer) => {
if (lengthReadError) {
console.error(`Failed to communicate with worker (read length) ${lengthReadError}`);
return;
}
this.state = 'length read';
const length = lengthBuffer.readInt32BE(0);
this.state = 'read message';
this.readBuffer(length, (messageError, messageBuffer) => {
if (messageError) {
console.error(`Failed to communicate with worker (read message) ${messageError}`);
return;
}
this.state = 'message read';
const messageString = messageBuffer.toString('utf-8');
const message = JSON.parse(messageString);
this.state = 'process message';
this.onWorkerMessage(message, err => {
if (err) {
console.error(`Failed to communicate with worker (process message) ${err}`);
return;
}
this.state = 'soon next';
setImmediate(() => this.readNextMessage());
});
});
});
}
onWorkerMessage(message, finalCallback) {
const { type, id } = message;
switch (type) {
case 'job':
{
const { data, error, result } = message;
(0, _mapSeries2.default)(data, (length, callback) => this.readBuffer(length, callback), (eachErr, buffers) => {
const { callback: jobCallback } = this.jobs[id];
const callback = (err, arg) => {
if (jobCallback) {
delete this.jobs[id];
this.activeJobs -= 1;
this.onJobDone();
if (err) {
jobCallback(err instanceof Error ? err : new Error(err), arg);
} else {
jobCallback(null, arg);
}
}
finalCallback();
};
if (eachErr) {
callback(eachErr);
return;
}
let bufferPosition = 0;
if (result.result) {
result.result = result.result.map(r => {
if (r.buffer) {
const buffer = buffers[bufferPosition];
bufferPosition += 1;
if (r.string) {
return buffer.toString('utf-8');
}
return buffer;
}
return r.data;
});
}
if (error) {
callback(this.fromErrorObj(error), result);
return;
}
callback(null, result);
});
break;
}
case 'resolve':
{
const { context, request, questionId } = message;
const { data } = this.jobs[id];
data.resolve(context, request, (error, result) => {
this.writeJson({
type: 'result',
id: questionId,
error: error ? {
message: error.message,
details: error.details,
missing: error.missing
} : null,
result
});
});
finalCallback();
break;
}
case 'emitWarning':
{
const { data } = message;
const { data: jobData } = this.jobs[id];
jobData.emitWarning(this.fromErrorObj(data));
finalCallback();
break;
}
case 'emitError':
{
const { data } = message;
const { data: jobData } = this.jobs[id];
jobData.emitError(this.fromErrorObj(data));
finalCallback();
break;
}
default:
{
console.error(`Unexpected worker message ${type} in WorkerPool.`);
finalCallback();
break;
}
}
}
fromErrorObj(arg) {
let obj;
if (typeof arg === 'string') {
obj = { message: arg };
} else {
obj = arg;
}
return new _WorkerError2.default(obj, this.id);
}
readBuffer(length, callback) {
(0, _readBuffer2.default)(this.readPipe, length, callback);
}
dispose() {
if (!this.disposed) {
this.disposed = true;
this.ignoreStdOutAndErrFromWorker(this.worker.stdout, this.worker.stderr);
this.writeEnd();
}
}
}
class WorkerPool {
constructor(options) {
this.options = options || {};
this.numberOfWorkers = options.numberOfWorkers;
this.poolTimeout = options.poolTimeout;
this.workerNodeArgs = options.workerNodeArgs;
this.workerParallelJobs = options.workerParallelJobs;
this.workers = new Set();
this.activeJobs = 0;
this.timeout = null;
this.poolQueue = (0, _queue2.default)(this.distributeJob.bind(this), options.poolParallelJobs);
this.terminated = false;
this.setupLifeCycle();
}
isAbleToRun() {
return !this.terminated;
}
terminate() {
if (this.terminated) {
return;
}
this.terminated = true;
this.poolQueue.kill();
this.disposeWorkers(true);
}
setupLifeCycle() {
process.on('exit', () => {
this.terminate();
});
}
run(data, callback) {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
}
this.activeJobs += 1;
this.poolQueue.push(data, callback);
}
distributeJob(data, callback) {
// use worker with the fewest jobs
let bestWorker;
for (const worker of this.workers) {
if (!bestWorker || worker.activeJobs < bestWorker.activeJobs) {
bestWorker = worker;
}
}
if (bestWorker && (bestWorker.activeJobs === 0 || this.workers.size >= this.numberOfWorkers)) {
bestWorker.run(data, callback);
return;
}
const newWorker = this.createWorker();
newWorker.run(data, callback);
}
createWorker() {
// spin up a new worker
const newWorker = new PoolWorker({
nodeArgs: this.workerNodeArgs,
parallelJobs: this.workerParallelJobs
}, () => this.onJobDone());
this.workers.add(newWorker);
return newWorker;
}
warmup(requires) {
while (this.workers.size < this.numberOfWorkers) {
this.createWorker().warmup(requires);
}
}
onJobDone() {
this.activeJobs -= 1;
if (this.activeJobs === 0 && isFinite(this.poolTimeout)) {
this.timeout = setTimeout(() => this.disposeWorkers(), this.poolTimeout);
}
}
disposeWorkers(fromTerminate) {
if (!this.options.poolRespawn && !fromTerminate) {
this.terminate();
return;
}
if (this.activeJobs === 0 || fromTerminate) {
for (const worker of this.workers) {
worker.dispose();
}
this.workers.clear();
}
}
}
exports.default = WorkerPool;