docs: refine code comment
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
index 7a64a7e563f9d6d8e1fffdac9ce91d1bc64d5b1e..34473ab4e2a3849bc96c6ac02bbe39bbbce3a47a 100644 (file)
@@ -1,16 +1,16 @@
 import { cpus } from 'node:os'
 import {
   DEFAULT_MEASUREMENT_STATISTICS_REQUIREMENTS,
-  DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+  getDefaultInternalWorkerChoiceStrategyOptions
 } from '../../utils'
 import type { IPool } from '../pool'
 import type { IWorker } from '../worker'
 import type {
   IWorkerChoiceStrategy,
+  InternalWorkerChoiceStrategyOptions,
   MeasurementStatisticsRequirements,
   StrategyPolicy,
-  TaskStatisticsRequirements,
-  WorkerChoiceStrategyOptions
+  TaskStatisticsRequirements
 } from './selection-strategies-types'
 
 /**
@@ -56,14 +56,14 @@ export abstract class AbstractWorkerChoiceStrategy<
    */
   public constructor (
     protected readonly pool: IPool<Worker, Data, Response>,
-    protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
+    protected opts: InternalWorkerChoiceStrategyOptions
   ) {
-    this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
+    this.setOptions(this.opts)
     this.choose = this.choose.bind(this)
   }
 
   protected setTaskStatisticsRequirements (
-    opts: WorkerChoiceStrategyOptions
+    opts: InternalWorkerChoiceStrategyOptions
   ): void {
     this.toggleMedianMeasurementStatisticsRequirements(
       this.taskStatisticsRequirements.runTime,
@@ -111,11 +111,19 @@ export abstract class AbstractWorkerChoiceStrategy<
   public abstract remove (workerNodeKey: number): boolean
 
   /** @inheritDoc */
-  public setOptions (opts: WorkerChoiceStrategyOptions): void {
-    this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
+  public setOptions (opts: InternalWorkerChoiceStrategyOptions): void {
+    this.opts = {
+      ...getDefaultInternalWorkerChoiceStrategyOptions(this.pool.info.maxSize),
+      ...opts
+    }
     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.
    *
@@ -123,7 +131,16 @@ export abstract class AbstractWorkerChoiceStrategy<
    * @returns Whether the worker node is ready or not.
    */
   protected isWorkerNodeReady (workerNodeKey: number): boolean {
-    return this.pool.workerNodes[workerNodeKey]?.info.ready ?? false
+    return this.pool.workerNodes[workerNodeKey]?.info?.ready ?? false
+  }
+
+  /**
+   * Check the next worker node readiness.
+   */
+  protected checkNextWorkerNodeReadiness (): void {
+    if (!this.isWorkerNodeReady(this.nextWorkerNodeKey as number)) {
+      delete this.nextWorkerNodeKey
+    }
   }
 
   /**