refactor: cleanup worker choice strategy options handling
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index a060cea3a5ba4486059fec1f6b45dac515f155e3..121647b697139e9c068170d266d96f2c0837edad 100644 (file)
@@ -21,10 +21,15 @@ export abstract class AbstractWorkerChoiceStrategy<
   Data = unknown,
   Response = unknown
 > implements IWorkerChoiceStrategy {
+  // /**
+  //  * Toggles finding the last free worker node key.
+  //  */
+  // private toggleFindLastFreeWorkerNodeKey: boolean = false
+
   /**
-   * Toggles finding the last free worker node key.
+   * Id of the next worker node.
    */
-  private toggleFindLastFreeWorkerNodeKey: boolean = false
+  protected nextWorkerNodeId: number = 0
 
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
@@ -128,24 +133,23 @@ export abstract class AbstractWorkerChoiceStrategy<
 
   /** @inheritDoc */
   public setOptions (opts: WorkerChoiceStrategyOptions): void {
-    opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
-    this.setTaskStatisticsRequirements(opts)
-    this.opts = opts
+    this.opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    this.setTaskStatisticsRequirements(this.opts)
   }
 
-  /**
-   * Finds a free worker node key.
-   *
-   * @returns The free worker node key or `-1` if there is no free worker node.
-   */
-  protected findFreeWorkerNodeKey (): number {
-    if (this.toggleFindLastFreeWorkerNodeKey) {
-      this.toggleFindLastFreeWorkerNodeKey = false
-      return this.findLastFreeWorkerNodeKey()
-    }
-    this.toggleFindLastFreeWorkerNodeKey = true
-    return this.findFirstFreeWorkerNodeKey()
-  }
+  // /**
+  //  * Finds a free worker node key.
+  //  *
+  //  * @returns The free worker node key or `-1` if there is no free worker node.
+  //  */
+  // protected findFreeWorkerNodeKey (): number {
+  //   if (this.toggleFindLastFreeWorkerNodeKey) {
+  //     this.toggleFindLastFreeWorkerNodeKey = false
+  //     return this.findLastFreeWorkerNodeKey()
+  //   }
+  //   this.toggleFindLastFreeWorkerNodeKey = true
+  //   return this.findFirstFreeWorkerNodeKey()
+  // }
 
   /**
    * Gets the worker task runtime.
@@ -171,8 +175,8 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   protected getWorkerTaskWaitTime (workerNodeKey: number): number {
     return this.taskStatisticsRequirements.waitTime.median
-      ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median
-      : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average
+      ? this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.median
+      : this.pool.workerNodes[workerNodeKey].workerUsage.waitTime.average
   }
 
   /**
@@ -200,46 +204,46 @@ export abstract class AbstractWorkerChoiceStrategy<
     return Math.round(cpusCycleTimeWeight / cpus().length)
   }
 
-  /**
-   * Finds the first free worker node key based on the number of tasks the worker has applied.
-   *
-   * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-   *
-   * If no free worker is found, `-1` is returned.
-   *
-   * @returns A worker node key if there is one, `-1` otherwise.
-   */
-  private findFirstFreeWorkerNodeKey (): number {
-    return this.pool.workerNodes.findIndex(workerNode => {
-      return workerNode.workerUsage.tasks.executing === 0
-    })
-  }
-
-  /**
-   * Finds the last free worker node key based on the number of tasks the worker has applied.
-   *
-   * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
-   *
-   * If no free worker is found, `-1` is returned.
-   *
-   * @returns A worker node key if there is one, `-1` otherwise.
-   */
-  private findLastFreeWorkerNodeKey (): number {
-    // It requires node >= 18.0.0:
-    // return this.workerNodes.findLastIndex(workerNode => {
-    //   return workerNode.workerUsage.tasks.executing === 0
-    // })
-    for (
-      let workerNodeKey = this.pool.workerNodes.length - 1;
-      workerNodeKey >= 0;
-      workerNodeKey--
-    ) {
-      if (
-        this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0
-      ) {
-        return workerNodeKey
-      }
-    }
-    return -1
-  }
+  // /**
+  //  * Finds the first free worker node key based on the number of tasks the worker has applied.
+  //  *
+  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
+  //  *
+  //  * If no free worker is found, `-1` is returned.
+  //  *
+  //  * @returns A worker node key if there is one, `-1` otherwise.
+  //  */
+  // private findFirstFreeWorkerNodeKey (): number {
+  //   return this.pool.workerNodes.findIndex(workerNode => {
+  //     return workerNode.workerUsage.tasks.executing === 0
+  //   })
+  // }
+
+  // /**
+  //  * Finds the last free worker node key based on the number of tasks the worker has applied.
+  //  *
+  //  * If a worker is found with `0` executing tasks, it is detected as free and its worker node key is returned.
+  //  *
+  //  * If no free worker is found, `-1` is returned.
+  //  *
+  //  * @returns A worker node key if there is one, `-1` otherwise.
+  //  */
+  // private findLastFreeWorkerNodeKey (): number {
+  //   // It requires node >= 18.0.0:
+  //   // return this.workerNodes.findLastIndex(workerNode => {
+  //   //   return workerNode.workerUsage.tasks.executing === 0
+  //   // })
+  //   for (
+  //     let workerNodeKey = this.pool.workerNodes.length - 1;
+  //     workerNodeKey >= 0;
+  //     workerNodeKey--
+  //   ) {
+  //     if (
+  //       this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0
+  //     ) {
+  //       return workerNodeKey
+  //     }
+  //   }
+  //   return -1
+  // }
 }