Merge pull request #747 from poolifier/multiple-functions
[poolifier.git] / src / pools / selection-strategies / abstract-worker-choice-strategy.ts
CommitLineData
bbeadd16 1import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
65d7a1c9 2import { type IPool, PoolType } from '../pool'
f06e48d8 3import type { IWorker } from '../worker'
10fcfaf4
JB
4import type {
5 IWorkerChoiceStrategy,
da309861
JB
6 RequiredStatistics,
7 WorkerChoiceStrategyOptions
10fcfaf4 8} from './selection-strategies-types'
bdaf31cd
JB
9
10/**
9cd39dd4 11 * Worker choice strategy abstract base class.
bdaf31cd 12 *
38e795c1
JB
13 * @typeParam Worker - Type of worker which manages the strategy.
14 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
02706357 15 * @typeParam Response - Type of execution response. This can only be serializable data.
bdaf31cd
JB
16 */
17export abstract class AbstractWorkerChoiceStrategy<
f06e48d8 18 Worker extends IWorker,
b2b1d84e
JB
19 Data = unknown,
20 Response = unknown
17393ac8 21> implements IWorkerChoiceStrategy {
cb70b19d
JB
22 /**
23 * Toggles finding the last free worker node key.
24 */
25 private toggleFindLastFreeWorkerNodeKey: boolean = false
afc003b2 26 /** @inheritDoc */
8b4d4500 27 protected readonly isDynamicPool: boolean
afc003b2 28 /** @inheritDoc */
da309861 29 public readonly requiredStatistics: RequiredStatistics = {
c6bd2650 30 runTime: false,
78099a15
JB
31 avgRunTime: false,
32 medRunTime: false
10fcfaf4 33 }
bdaf31cd
JB
34
35 /**
6533c3e6 36 * Constructs a worker choice strategy bound to the pool.
bdaf31cd 37 *
38e795c1 38 * @param pool - The pool instance.
da309861 39 * @param opts - The worker choice strategy options.
bdaf31cd
JB
40 */
41 public constructor (
c4855468 42 protected readonly pool: IPool<Worker, Data, Response>,
a20f0ba5 43 protected opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
b8f3418c
JB
44 ) {
45 this.isDynamicPool = this.pool.type === PoolType.DYNAMIC
7254e419 46 this.choose = this.choose.bind(this)
b8f3418c 47 }
bdaf31cd 48
2fc5cae3 49 protected checkOptions (opts: WorkerChoiceStrategyOptions): void {
f9f00b5f 50 if (this.requiredStatistics.avgRunTime && opts.medRunTime === true) {
2fc5cae3
JB
51 this.requiredStatistics.avgRunTime = false
52 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
da309861 53 }
a20f0ba5
JB
54 if (this.requiredStatistics.medRunTime && opts.medRunTime === false) {
55 this.requiredStatistics.avgRunTime = true
56 this.requiredStatistics.medRunTime = opts.medRunTime as boolean
57 }
da309861
JB
58 }
59
afc003b2 60 /** @inheritDoc */
a6f7f1b4 61 public abstract reset (): boolean
ea7a90d3 62
afc003b2 63 /** @inheritDoc */
c923ce56 64 public abstract choose (): number
97a2abc3 65
afc003b2 66 /** @inheritDoc */
f06e48d8 67 public abstract remove (workerNodeKey: number): boolean
a20f0ba5
JB
68
69 /** @inheritDoc */
70 public setOptions (opts: WorkerChoiceStrategyOptions): void {
d01e4d67 71 opts = opts ?? DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
a20f0ba5
JB
72 this.checkOptions(opts)
73 this.opts = opts
74 }
cb70b19d
JB
75
76 /**
77 * Finds a free worker node key.
78 *
79 * @returns The free worker node key or `-1` if there is no free worker node.
80 */
81 protected findFreeWorkerNodeKey (): number {
82 if (this.toggleFindLastFreeWorkerNodeKey) {
83 this.toggleFindLastFreeWorkerNodeKey = false
e0ae6100 84 return this.findLastFreeWorkerNodeKey()
cb70b19d
JB
85 }
86 this.toggleFindLastFreeWorkerNodeKey = true
e0ae6100
JB
87 return this.findFirstFreeWorkerNodeKey()
88 }
89
90 /**
91 * Finds the first free worker node key based on the number of tasks the worker has applied.
92 *
93 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
94 *
95 * If no free worker is found, `-1` is returned.
96 *
97 * @returns A worker node key if there is one, `-1` otherwise.
98 */
99 private findFirstFreeWorkerNodeKey (): number {
100 return this.pool.workerNodes.findIndex(workerNode => {
101 return workerNode.tasksUsage?.running === 0
102 })
103 }
104
105 /**
106 * Finds the last free worker node key based on the number of tasks the worker has applied.
107 *
108 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
109 *
110 * If no free worker is found, `-1` is returned.
111 *
112 * @returns A worker node key if there is one, `-1` otherwise.
113 */
114 private findLastFreeWorkerNodeKey (): number {
0e7c56b0 115 // It requires node >= 18.0.0:
e0ae6100
JB
116 // return this.workerNodes.findLastIndex(workerNode => {
117 // return workerNode.tasksUsage?.running === 0
118 // })
119 for (let i = this.pool.workerNodes.length - 1; i >= 0; i--) {
120 if (this.pool.workerNodes[i].tasksUsage?.running === 0) {
121 return i
122 }
123 }
124 return -1
cb70b19d 125 }
bdaf31cd 126}