X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcharging-station%2FWorker.ts;h=efa327fd8a0666d301bfcae95d8b332a79158958;hb=4faad557cd49253297c7d0230db2eecfd850b4f4;hp=93aade5046b44284ee4604e1b17df0e3a9f5d46d;hpb=6af9012e5b9ef2ed6f4fe8a9696b40ac0e8da4d0;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Worker.ts b/src/charging-station/Worker.ts index 93aade50..efa327fd 100644 --- a/src/charging-station/Worker.ts +++ b/src/charging-station/Worker.ts @@ -1,51 +1,55 @@ +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 { + if (Configuration.useWorkerPool()) { + await this._startWorkerWithPool(); + } else { + await this._startWorker(); + } + return this._worker; } /** * - * @return {Promise} + * @return {void} * @public */ - async start(): Promise { + 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 }); } /** @@ -55,12 +59,13 @@ export default class Wrk { */ 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; }); }); } @@ -80,6 +85,25 @@ export default class Wrk { 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); + } +}