From 36367c31509f7b23c5788ecf6c84ad9be3734c73 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 4 Mar 2022 15:37:17 +0100 Subject: [PATCH] Fix workerSet linear ramp up at startup MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStationWorker.ts | 3 ++- src/types/Worker.ts | 1 + src/worker/WorkerSet.ts | 3 +-- 3 files changed, 4 insertions(+), 3 deletions(-) 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); } /** -- 2.34.1