WorkerSet: consolidate the worker start delay code
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 3 Mar 2022 23:01:34 +0000 (00:01 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 3 Mar 2022 23:01:34 +0000 (00:01 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/worker/WorkerFactory.ts
src/worker/WorkerSet.ts

index ccea2a38f918c99881d90af39812ac4484db2ed3..a72f2c945d6a40e05f3bb20fd92c27ec3119c851 100644 (file)
@@ -26,16 +26,31 @@ export default class WorkerFactory {
     switch (workerProcessType) {
       case WorkerProcessType.WORKER_SET:
         options.elementsPerWorker = options.elementsPerWorker ?? Constants.DEFAULT_CHARGING_STATIONS_PER_WORKER;
-        workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options);
+        workerImplementation = new WorkerSet(workerScript, options.elementsPerWorker,
+          {
+            workerStartDelay: options.workerStartDelay,
+            elementStartDelay: options.elementStartDelay
+          },
+          options);
         break;
       case WorkerProcessType.STATIC_POOL:
         options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
-        workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options.poolOptions);
+        workerImplementation = new WorkerStaticPool(workerScript, options.poolMaxSize,
+          {
+            workerStartDelay: options.workerStartDelay,
+            elementStartDelay: options.elementStartDelay
+          },
+          options.poolOptions);
         break;
       case WorkerProcessType.DYNAMIC_POOL:
         options.poolMinSize = options.poolMinSize ?? Constants.DEFAULT_WORKER_POOL_MIN_SIZE;
         options.poolMaxSize = options.poolMaxSize ?? Constants.DEFAULT_WORKER_POOL_MAX_SIZE;
-        workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize, { workerStartDelay: options.workerStartDelay, elementStartDelay: options.elementStartDelay }, options.poolOptions);
+        workerImplementation = new WorkerDynamicPool(workerScript, options.poolMinSize, options.poolMaxSize,
+          {
+            workerStartDelay: options.workerStartDelay,
+            elementStartDelay: options.elementStartDelay
+          },
+          options.poolOptions);
         break;
       default:
         throw new Error(`Worker implementation type '${workerProcessType}' not found`);
index dff07f2391756d882aeece772d225be182002577..5477ab0d744c2d783fe416ea55e4e0f1b0fdffc9 100644 (file)
@@ -42,9 +42,7 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
       throw new Error('Cannot add a WorkerSet element: workers\' set does not exist');
     }
     if (this.getLastWorkerSetElement().numberOfWorkerElements >= this.maxElementsPerWorker) {
-      this.startWorker();
-      // Start worker sequentially to optimize memory at startup
-      this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay);
+      await this.startWorker();
     }
     this.getLastWorker().postMessage({ id: WorkerMessageEvents.START_WORKER_ELEMENT, data: elementData });
     this.getLastWorkerSetElement().numberOfWorkerElements++;
@@ -57,9 +55,7 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
    * @public
    */
   public async start(): Promise<void> {
-    this.startWorker();
-    // Start worker sequentially to optimize memory at startup
-    this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay);
+    await this.startWorker();
   }
 
   /**
@@ -78,7 +74,7 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
    *
    * @private
    */
-  private startWorker(): void {
+  private async startWorker(): Promise<void> {
     const worker = new Worker(this.workerScript);
     worker.on('message', (msg) => {
       (async () => {
@@ -91,6 +87,8 @@ export default class WorkerSet extends WorkerAbstract<WorkerData> {
       this.workerSet.delete(this.getWorkerSetElementByWorker(worker));
     });
     this.workerSet.add({ worker, numberOfWorkerElements: 0 });
+    // Start worker sequentially to optimize memory at startup
+    this.workerStartDelay > 0 && await Utils.sleep(this.workerStartDelay);
   }
 
   private getLastWorkerSetElement(): WorkerSetElement {