1 const Configuration
= require('../utils/Configuration');
2 const {Worker
} = require('worker_threads');
3 const Pool
= require('worker-threads-pool');
9 * @param {String} workerScript
10 * @param {Object} workerData
12 constructor(workerScript
, workerData
) {
13 if (Configuration
.useWorkerPool()) {
14 this._pool
= new Pool({max
: Configuration
.getWorkerPoolSize()});
16 this._workerData
= workerData
;
17 this._workerScript
= workerScript
;
25 _startWorkerWithPool() {
26 return new Promise((resolve
, reject
) => {
27 this._pool
.acquire(this._workerScript
, {workerData
: this._workerData
}, (err
, worker
) => {
31 worker
.once('message', resolve
);
32 worker
.once('error', reject
);
43 return new Promise((resolve
, reject
) => {
44 const worker
= new Worker(this._workerScript
, {workerData
: this._workerData
});
45 worker
.on('message', resolve
);
46 worker
.on('error', reject
);
47 worker
.on('exit', (code
) => {
49 reject(new Error(`Worker stopped with exit code ${code}`));
61 if (Configuration
.useWorkerPool()) {
62 return this._startWorkerWithPool();
64 return this._startWorker();