X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2FWorker.ts;h=4137429780aa3fda1f12de4fdd8ab18be9098d48;hb=97ed5cec983f91ed1cdb903c13505994a6e0d23b;hp=57748831ed5b4d00e6b6bd94663e984916d437c8;hpb=f98fbdb9a436347eff2c0ba9590543d1d021db00;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/Worker.ts b/src/charging-station/Worker.ts index 57748831..41374297 100644 --- a/src/charging-station/Worker.ts +++ b/src/charging-station/Worker.ts @@ -1,6 +1,7 @@ import { Worker, WorkerOptions } from 'worker_threads'; import Configuration from '../utils/Configuration'; +import Constants from '../utils/Constants'; import Pool from 'worker-threads-pool'; import WorkerData from '../types/WorkerData'; @@ -8,33 +9,32 @@ export default class Wrk { private _workerScript: string; private _workerData: WorkerData; private _index: number; - private _concurrentWorkers: number; + private _maxWorkerElements: number; + private _worker: Worker; /** * Create a new `Wrk`. * * @param {string} workerScript * @param {WorkerData} workerData - * @param {number} numConcurrentWorkers + * @param {number} maxWorkerElements */ - constructor(workerScript: string, workerData: WorkerData, numConcurrentWorkers: number) { + constructor(workerScript: string, workerData: WorkerData, maxWorkerElements = 1) { this._workerData = workerData; this._index = workerData.index; this._workerScript = workerScript; if (Configuration.useWorkerPool()) { - this._concurrentWorkers = Configuration.getWorkerPoolSize(); - WorkerPool.concurrentWorkers = this._concurrentWorkers; - } else { - this._concurrentWorkers = numConcurrentWorkers; + WorkerPool.maxConcurrentWorkers = Configuration.getWorkerPoolSize(); } + this._maxWorkerElements = maxWorkerElements; } /** * @return {number} * @public */ - public get concurrentWorkers(): number { - return this._concurrentWorkers; + public get maxWorkerElements(): number { + return this._maxWorkerElements; } /** @@ -42,11 +42,28 @@ export default class Wrk { * @return {Promise} * @public */ - async start(): Promise { + async start(): Promise { + if (Configuration.useWorkerPool()) { + await this._startWorkerWithPool(); + } else { + await this._startWorker(); + } + return this._worker; + } + + /** + * + * @return {void} + * @public + */ + 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._index = workerData.index; + this._worker.postMessage({ id : Constants.START_WORKER_ELEMENT, workerData: workerData }); } /** @@ -62,6 +79,7 @@ export default class Wrk { } worker.once('message', resolve); worker.once('error', reject); + this._worker = worker; }); }); } @@ -81,19 +99,20 @@ export default class Wrk { reject(new Error(`Worker id ${this._index} stopped with exit code ${code}`)); } }); + this._worker = worker; }); } } class WorkerPool { - public static concurrentWorkers: number; + public static maxConcurrentWorkers: number; private static _instance: Pool; private constructor() { } public static getInstance(): Pool { if (!WorkerPool._instance) { - WorkerPool._instance = new Pool({ max: WorkerPool.concurrentWorkers }); + WorkerPool._instance = new Pool({ max: WorkerPool.maxConcurrentWorkers }); } return WorkerPool._instance; }