From: Jérôme Benoit Date: Fri, 4 Mar 2022 14:37:17 +0000 (+0100) Subject: Fix workerSet linear ramp up at startup X-Git-Tag: v1.1.50~2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=36367c31509f7b23c5788ecf6c84ad9be3734c73;p=e-mobility-charging-stations-simulator.git Fix workerSet linear ramp up at startup Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationWorker.ts b/src/charging-station/ChargingStationWorker.ts index cc0f6272..5ad5f664 100644 --- a/src/charging-station/ChargingStationWorker.ts +++ b/src/charging-station/ChargingStationWorker.ts @@ -24,9 +24,10 @@ if (Utils.workerPoolInUse()) { * Listen messages send by the main thread */ function addMessageListener(): void { - parentPort?.on('message', (message: ChargingStationWorkerMessage) => { + parentPort?.on('message', async (message: ChargingStationWorkerMessage) => { if (message.id === ChargingStationWorkerMessageEvents.START_WORKER_ELEMENT) { startChargingStation(message.data); + message.workerOptions?.elementStartDelay > 0 && await Utils.sleep(this.workerOptions.elementStartDelay); } }); } diff --git a/src/types/Worker.ts b/src/types/Worker.ts index aca1922e..633fb7a6 100644 --- a/src/types/Worker.ts +++ b/src/types/Worker.ts @@ -27,6 +27,7 @@ export interface WorkerSetElement { export interface WorkerMessage { id: WorkerMessageEvents; + workerOptions?: WorkerOptions; data: T; } diff --git a/src/worker/WorkerSet.ts b/src/worker/WorkerSet.ts index 81a6b05f..b62f63df 100644 --- a/src/worker/WorkerSet.ts +++ b/src/worker/WorkerSet.ts @@ -44,9 +44,8 @@ export default class WorkerSet extends WorkerAbstract { if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.workerOptions.elementsPerWorker) { await this.startWorker(); } - this.getLastWorker().postMessage({ id: WorkerMessageEvents.START_WORKER_ELEMENT, data: elementData }); + this.getLastWorker().postMessage({ id: WorkerMessageEvents.START_WORKER_ELEMENT, workerOptions: this.workerOptions, data: elementData }); this.getLastWorkerSetElement().numberOfWorkerElements++; - this.workerOptions.elementStartDelay > 0 && await Utils.sleep(this.workerOptions.elementStartDelay); } /**