Revert "fix: use version from package.json"
[poolifier.git] / src / pools / selection-strategies / least-used-worker-choice-strategy.ts
index 4161a1d266634518c5800d57a421acb46b690fe6..53aa05eed641d557631e56e964b7d57739a4a7f8 100644 (file)
@@ -11,8 +11,8 @@ import type {
  * Selects the least used worker.
  *
  * @typeParam Worker - Type of worker which manages the strategy.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  */
 export class LeastUsedWorkerChoiceStrategy<
     Worker extends IWorker,
@@ -42,26 +42,22 @@ export class LeastUsedWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public choose (): number {
-    const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
-    if (freeWorkerNodeKey !== -1) {
-      return freeWorkerNodeKey
-    }
     let minNumberOfTasks = Infinity
-    let leastUsedWorkerNodeKey!: number
     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) {
-        return workerNodeKey
+        this.nextWorkerNodeId = workerNodeKey
+        break
       } else if (workerTasks < minNumberOfTasks) {
         minNumberOfTasks = workerTasks
-        leastUsedWorkerNodeKey = workerNodeKey
+        this.nextWorkerNodeId = workerNodeKey
       }
     }
-    return leastUsedWorkerNodeKey
+    return this.nextWorkerNodeId
   }
 
   /** @inheritDoc */