refactor: add helper to get worker info by worker
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 18:03:13 +0000 (20:03 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 19 Jul 2023 18:03:13 +0000 (20:03 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts

index 5481099f202c19f5ef57833d4f454a18757dce3c..10a2d514246748a7d21187f22b6b8400b1697864 100644 (file)
@@ -910,7 +910,7 @@ export abstract class AbstractPool<
         void (this.destroyWorker(worker) as Promise<void>)
       }
     })
-    const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker))
+    const workerInfo = this.getWorkerInfoByWorker(worker)
     workerInfo.dynamic = true
     if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) {
       workerInfo.ready = true
@@ -953,7 +953,7 @@ export abstract class AbstractPool<
   private sendWorkerStartupMessage (worker: Worker): void {
     this.sendToWorker(worker, {
       ready: false,
-      workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number
+      workerId: this.getWorkerInfoByWorker(worker).id as number
     })
   }
 
@@ -1006,9 +1006,9 @@ export abstract class AbstractPool<
   }
 
   private handleWorkerReadyResponse (message: MessageValue<Response>): void {
-    const worker = this.getWorkerById(message.workerId)
-    this.getWorkerInfo(this.getWorkerNodeKey(worker as Worker)).ready =
-      message.ready as boolean
+    this.getWorkerInfoByWorker(
+      this.getWorkerById(message.workerId) as Worker
+    ).ready = message.ready as boolean
     if (this.emitter != null && this.ready) {
       this.emitter.emit(PoolEvents.ready, this.info)
     }
@@ -1051,7 +1051,7 @@ export abstract class AbstractPool<
   }
 
   /**
-   * Gets the worker information.
+   * Gets the worker information from the given worker node key.
    *
    * @param workerNodeKey - The worker node key.
    */
@@ -1059,6 +1059,15 @@ export abstract class AbstractPool<
     return this.workerNodes[workerNodeKey].info
   }
 
+  /**
+   * Gets the worker information from the given worker.
+   *
+   * @param worker - The worker.
+   */
+  private getWorkerInfoByWorker (worker: Worker): WorkerInfo {
+    return this.workerNodes[this.getWorkerNodeKey(worker)].info
+  }
+
   /**
    * Adds the given worker in the pool worker nodes.
    *
@@ -1135,7 +1144,7 @@ export abstract class AbstractPool<
         elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements()
           .elu.aggregate
       },
-      workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number
+      workerId: this.getWorkerInfoByWorker(worker).id as number
     })
   }
 }