5de059d00c6229153937b3d690f7bb7fa444c410
1 const Configuration
= require('../utils/Configuration');
2 const EventEmitter
= require('events');
3 const {Worker
} = require('worker_threads');
4 const Pool
= require('worker-threads-pool');
10 * @param {String} workerScript
11 * @param {Object} workerData
13 constructor(workerScript
, workerData
) {
14 if (Configuration
.useWorkerPool()) {
15 this._pool
= new Pool({max
: Configuration
.getWorkerPoolSize()});
17 this._workerData
= workerData
;
18 this._workerScript
= workerScript
;
26 _startWorkerWithPool() {
27 return new Promise((resolve
, reject
) => {
28 this._pool
.acquire(this._workerScript
, {workerData
: this._workerData
}, (err
, worker
) => {
32 worker
.once('message', resolve
);
33 worker
.once('error', reject
);
44 return new Promise((resolve
, reject
) => {
45 const worker
= new Worker(this._workerScript
, {workerData
: this._workerData
});
46 worker
.on('message', resolve
);
47 worker
.on('error', reject
);
48 worker
.on('exit', (code
) => {
50 reject(new Error(`Worker stopped with exit code ${code}`));
62 if (Configuration
.useWorkerPool()) {
63 if (Configuration
.getWorkerPoolSize() > 10) {
64 EventEmitter
.defaultMaxListeners
= Configuration
.getWorkerPoolSize() + 1;
66 return this._startWorkerWithPool();
68 return this._startWorker();