refactor: prepare worker choice strategies code for worker readiness
[poolifier.git] / src / pools / selection-strategies / least-used-worker-choice-strategy.ts
index 5c266f282d229fd5c5647e3305237ac1ee33c865..910d10a3ac320db8f3feac42eb43293181a806d1 100644 (file)
@@ -42,26 +42,33 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
+    this.leastUsedNextWorkerNodeKey()
+    return this.nextWorkerNodeKey
+  }
+
+  /** @inheritDoc */
+  public remove (): boolean {
+    return true
+  }
+
+  private leastUsedNextWorkerNodeKey (): void {
     let minNumberOfTasks = Infinity
     for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
-      const workerTaskStatistics = workerNode.workerUsage.tasks
+      const workerTaskStatistics = workerNode.usage.tasks
       const workerTasks =
         workerTaskStatistics.executed +
         workerTaskStatistics.executing +
         workerTaskStatistics.queued
-      if (workerTasks === 0) {
-        this.nextWorkerNodeId = workerNodeKey
+      if (this.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) {
+        this.nextWorkerNodeKey = workerNodeKey
         break
-      } else if (workerTasks < minNumberOfTasks) {
+      } else if (
+        this.isWorkerNodeReady(workerNodeKey) &&
+        workerTasks < minNumberOfTasks
+      ) {
         minNumberOfTasks = workerTasks
-        this.nextWorkerNodeId = workerNodeKey
+        this.nextWorkerNodeKey = workerNodeKey
       }
     }
-    return this.nextWorkerNodeId
-  }
-
-  /** @inheritDoc */
-  public remove (): boolean {
-    return true
   }
 }