refactor: factor out worker runtime getter
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index c3fb28c4f1e162185f65e08029c886e8df826d99..a4455d28a8e33c5bac1bceaf679b8feab5de533b 100644 (file)
@@ -87,6 +87,20 @@ export abstract class AbstractWorkerChoiceStrategy<
     return this.findFirstFreeWorkerNodeKey()
   }
 
+  /**
+   * Gets the worker task run time.
+   * If the required statistics are `avgRunTime`, the average run time is returned.
+   * If the required statistics are `medRunTime`, the median run time is returned.
+   *
+   * @param workerNodeKey - The worker node key.
+   * @returns The worker task run time.
+   */
+  protected getWorkerTaskRunTime (workerNodeKey: number): number {
+    return this.requiredStatistics.medRunTime
+      ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime
+      : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime
+  }
+
   /**
    * Finds the first free worker node key based on the number of tasks the worker has applied.
    *