Fix workerSet linear ramp up at startup
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Mar 2022 14:37:17 +0000 (15:37 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 4 Mar 2022 14:37:17 +0000 (15:37 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/ChargingStationWorker.ts
src/types/Worker.ts
src/worker/WorkerSet.ts

index cc0f62726a174350095ade3fd4805a0204c753bd..5ad5f6646920246a248ee93596e4325d907df1c8 100644 (file)
@@ -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);
     }
   });
 }
index aca1922e02a1df88e098068f12f295ab9f16fe1e..633fb7a688c920cdfc085e5cb53eb063aa984f56 100644 (file)
@@ -27,6 +27,7 @@ export interface WorkerSetElement {
 
 export interface WorkerMessage<T extends WorkerData> {
   id: WorkerMessageEvents;
+  workerOptions?: WorkerOptions;
   data: T;
 }
 
index 81a6b05fdc3f1898c73cb99b18ed4a7f1bbf85a3..b62f63df546d3709e3c00bb4836131501b0076f2 100644 (file)
@@ -44,9 +44,8 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
     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);
   }
 
   /**