fix: fix race condition at counting executing tasks on worker node
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index 00c56dad8f555f64afc0a4d21a693d6a97b956f5..36d1cd4c407c89de4231911bd22cb57b23350781 100644 (file)
@@ -70,8 +70,7 @@ export class FairShareWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    this.fairShareNextWorkerNodeKey()
-    return this.nextWorkerNodeKey
+    return this.fairShareNextWorkerNodeKey()
   }
 
   /** @inheritDoc */
@@ -80,7 +79,7 @@ export class FairShareWorkerChoiceStrategy<
     return true
   }
 
-  private fairShareNextWorkerNodeKey (): void {
+  private fairShareNextWorkerNodeKey (): number {
     let minWorkerVirtualTaskEndTimestamp = Infinity
     for (const [workerNodeKey] of this.pool.workerNodes.entries()) {
       if (this.workersVirtualTaskEndTimestamp[workerNodeKey] == null) {
@@ -89,13 +88,14 @@ export class FairShareWorkerChoiceStrategy<
       const workerVirtualTaskEndTimestamp =
         this.workersVirtualTaskEndTimestamp[workerNodeKey]
       if (
-        this.isWorkerNodeReady(workerNodeKey) &&
+        this.isWorkerNodeEligible(workerNodeKey) &&
         workerVirtualTaskEndTimestamp < minWorkerVirtualTaskEndTimestamp
       ) {
         minWorkerVirtualTaskEndTimestamp = workerVirtualTaskEndTimestamp
         this.nextWorkerNodeKey = workerNodeKey
       }
     }
+    return this.nextWorkerNodeKey
   }
 
   /**