Merge pull request #2164 from poolifier/dependabot/npm_and_yarn/tatami-ng-0.3.0
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index d8d8f32d2f9dc2eee45f63c75f2aa0ece167b108..3618944c2043a07b1e8e709aa877d831b2bfc76b 100644 (file)
@@ -1,8 +1,8 @@
-import {
-  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  buildWorkerChoiceStrategyOptions
-} from '../../utils.js'
 import type { IPool } from '../pool.js'
+import {
+  buildWorkerChoiceStrategyOptions,
+  DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS
+} from '../utils.js'
 import type { IWorker } from '../worker.js'
 import type {
   IWorkerChoiceStrategy,
@@ -32,7 +32,7 @@ export abstract class AbstractWorkerChoiceStrategy<
   /**
    * The previous worker node key.
    */
-  protected previousWorkerNodeKey: number = 0
+  protected previousWorkerNodeKey = 0
 
   /** @inheritDoc */
   public readonly strategyPolicy: StrategyPolicy = {
@@ -57,8 +57,8 @@ export abstract class AbstractWorkerChoiceStrategy<
     protected readonly pool: IPool<Worker, Data, Response>,
     protected opts?: WorkerChoiceStrategyOptions
   ) {
-    this.setOptions(this.opts)
     this.choose = this.choose.bind(this)
+    this.setOptions(this.opts)
   }
 
   protected setTaskStatisticsRequirements (
@@ -121,11 +121,6 @@ export abstract class AbstractWorkerChoiceStrategy<
     this.setTaskStatisticsRequirements(this.opts)
   }
 
-  /** @inheritDoc */
-  public hasPoolWorkerNodesReady (): boolean {
-    return this.pool.workerNodes.some(workerNode => workerNode.info.ready)
-  }
-
   /**
    * Whether the worker node is ready or not.
    *
@@ -137,11 +132,14 @@ export abstract class AbstractWorkerChoiceStrategy<
   }
 
   /**
-   * Check the next worker node readiness.
+   * Check the next worker node key.
    */
-  protected checkNextWorkerNodeReadiness (): void {
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
-    if (!this.isWorkerNodeReady(this.nextWorkerNodeKey!)) {
+  protected checkNextWorkerNodeKey (): void {
+    if (
+      this.nextWorkerNodeKey != null &&
+      (this.nextWorkerNodeKey < 0 ||
+        !this.isWorkerNodeReady(this.nextWorkerNodeKey))
+    ) {
       delete this.nextWorkerNodeKey
     }
   }
@@ -194,6 +192,9 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @param workerNodeKey - The worker node key.
    */
   protected setPreviousWorkerNodeKey (workerNodeKey: number | undefined): void {
-    this.previousWorkerNodeKey = workerNodeKey ?? this.previousWorkerNodeKey
+    this.previousWorkerNodeKey =
+      workerNodeKey != null && workerNodeKey >= 0
+        ? workerNodeKey
+        : this.previousWorkerNodeKey
   }
 }