+import { Worker, WorkerOptions } from 'worker_threads';
+
import Configuration from '../utils/Configuration';
+import Constants from '../utils/Constants';
import Pool from 'worker-threads-pool';
-import { Worker } from 'worker_threads';
+import WorkerData from '../types/WorkerData';
export default class Wrk {
- private _workerData;
- private _workerScript;
- private _pool;
- private _concurrentWorkers: number;
+ private _workerScript: string;
+ private _workerData: WorkerData;
+ private _worker: Worker;
/**
* Create a new `Wrk`.
*
- * @param {String} workerScript
- * @param {Object} workerData
- * @param {Number} numConcurrentWorkers
+ * @param {string} workerScript
+ * @param {WorkerData} workerData
*/
- constructor(workerScript, workerData, numConcurrentWorkers) {
+ constructor(workerScript: string, workerData: WorkerData) {
this._workerData = workerData;
this._workerScript = workerScript;
- this._numConcurrentWorkers = numConcurrentWorkers;
if (Configuration.useWorkerPool()) {
- this._pool = new Pool({ max: Configuration.getWorkerPoolSize() });
+ WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize();
}
}
/**
- * @param {Number} numConcurrentWorkers
- * @private
+ *
+ * @return {Promise}
+ * @public
*/
- set _numConcurrentWorkers(numConcurrentWorkers: number) {
- this._concurrentWorkers = numConcurrentWorkers;
- }
-
- get _numConcurrentWorkers(): number {
- return this._concurrentWorkers;
+ async start(): Promise<Worker> {
+ if (Configuration.useWorkerPool()) {
+ await this._startWorkerWithPool();
+ } else {
+ await this._startWorker();
+ }
+ return this._worker;
}
/**
*
- * @return {Promise}
+ * @return {void}
* @public
*/
- async start(): Promise<unknown> {
+ addWorkerElement(workerData: WorkerData): void {
+ // FIXME: also forbid to add an element if the current number of elements > max number of elements
if (Configuration.useWorkerPool()) {
- return this._startWorkerWithPool();
+ return;
}
- return this._startWorker();
+ this._workerData = workerData;
+ this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData });
}
/**
*/
private async _startWorkerWithPool() {
return new Promise((resolve, reject) => {
- this._pool.acquire(this._workerScript, { workerData: this._workerData }, (err, worker) => {
+ WorkerPool.acquire(this._workerScript, { workerData: this._workerData }, (err, worker) => {
if (err) {
return reject(err);
}
worker.once('message', resolve);
worker.once('error', reject);
+ this._worker = worker;
});
});
}
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
+ this._worker = worker;
});
}
}
+
+class WorkerPool {
+ public static maxConcurrentWorkers: number;
+ private static _instance: Pool;
+
+ private constructor() { }
+
+ public static getInstance(): Pool {
+ if (!WorkerPool._instance) {
+ WorkerPool._instance = new Pool({ max: WorkerPool.maxConcurrentWorkers });
+ }
+ return WorkerPool._instance;
+ }
+
+ public static acquire(filename: string, options: WorkerOptions, callback: (error: Error | null, worker: Worker) => void): void {
+ WorkerPool.getInstance().acquire(filename, options, callback);
+ }
+}