fix: fix task dequeuing from the last bucket
[poolifier.git] / src / pools / selection-strategies / fair-share-worker-choice-strategy.ts
index e8f9cc619f6a7f0d8ffc596332e27a9159c72e01..d1a4919d70184950dcacdf4596db552579bc9fb1 100644 (file)
@@ -1,12 +1,11 @@
-import { DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS } from '../../utils.js'
 import type { IPool } from '../pool.js'
 import type { IWorker } from '../worker.js'
 import { AbstractWorkerChoiceStrategy } from './abstract-worker-choice-strategy.js'
 import {
   type IWorkerChoiceStrategy,
-  type InternalWorkerChoiceStrategyOptions,
   Measurements,
-  type TaskStatisticsRequirements
+  type TaskStatisticsRequirements,
+  type WorkerChoiceStrategyOptions
 } from './selection-strategies-types.js'
 
 /**
@@ -31,7 +30,11 @@ export class FairShareWorkerChoiceStrategy<
       average: true,
       median: false
     },
-    waitTime: DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
+    waitTime: {
+      aggregate: true,
+      average: true,
+      median: false
+    },
     elu: {
       aggregate: true,
       average: true,
@@ -42,7 +45,7 @@ export class FairShareWorkerChoiceStrategy<
   /** @inheritDoc */
   public constructor (
     pool: IPool<Worker, Data, Response>,
-    opts: InternalWorkerChoiceStrategyOptions
+    opts?: WorkerChoiceStrategyOptions
   ) {
     super(pool, opts)
     this.setTaskStatisticsRequirements(this.opts)
@@ -117,11 +120,13 @@ export class FairShareWorkerChoiceStrategy<
     workerNodeKey: number,
     workerNodeVirtualTaskStartTimestamp: number
   ): number {
-    const workerNodeTaskRunTime =
-      this.opts.measurement === Measurements.elu
+    const workerNodeTaskExecutionTime =
+      this.getWorkerNodeTaskWaitTime(workerNodeKey) +
+      // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
+      (this.opts!.measurement === Measurements.elu
         ? this.getWorkerNodeTaskElu(workerNodeKey)
-        : this.getWorkerNodeTaskRunTime(workerNodeKey)
-    return workerNodeVirtualTaskStartTimestamp + workerNodeTaskRunTime
+        : this.getWorkerNodeTaskRunTime(workerNodeKey))
+    return workerNodeVirtualTaskStartTimestamp + workerNodeTaskExecutionTime
   }
 
   private getWorkerNodeVirtualTaskStartTimestamp (
@@ -131,7 +136,7 @@ export class FairShareWorkerChoiceStrategy<
       this.pool.workerNodes[workerNodeKey]?.strategyData
         ?.virtualTaskEndTimestamp
     const now = performance.now()
-    return now < (virtualTaskEndTimestamp ?? -Infinity)
+    return now < (virtualTaskEndTimestamp ?? Number.NEGATIVE_INFINITY)
       ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
       virtualTaskEndTimestamp!
       : now