feat: add public methods to manipulate task functions
[poolifier.git] / src / pools / selection-strategies / worker-choice-strategy-context.ts
1 import { DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS } from '../../utils'
2 import type { IPool } from '../pool'
3 import type { IWorker } from '../worker'
4 import { FairShareWorkerChoiceStrategy } from './fair-share-worker-choice-strategy'
5 import { InterleavedWeightedRoundRobinWorkerChoiceStrategy } from './interleaved-weighted-round-robin-worker-choice-strategy'
6 import { LeastBusyWorkerChoiceStrategy } from './least-busy-worker-choice-strategy'
7 import { LeastUsedWorkerChoiceStrategy } from './least-used-worker-choice-strategy'
8 import { LeastEluWorkerChoiceStrategy } from './least-elu-worker-choice-strategy'
9 import { RoundRobinWorkerChoiceStrategy } from './round-robin-worker-choice-strategy'
10 import type {
11 IWorkerChoiceStrategy,
12 StrategyPolicy,
13 TaskStatisticsRequirements,
14 WorkerChoiceStrategy,
15 WorkerChoiceStrategyOptions
16 } from './selection-strategies-types'
17 import { WorkerChoiceStrategies } from './selection-strategies-types'
18 import { WeightedRoundRobinWorkerChoiceStrategy } from './weighted-round-robin-worker-choice-strategy'
19
20 /**
21 * The worker choice strategy context.
22 *
23 * @typeParam Worker - Type of worker.
24 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
25 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
26 */
27 export class WorkerChoiceStrategyContext<
28 Worker extends IWorker,
29 Data = unknown,
30 Response = unknown
31 > {
32 private readonly workerChoiceStrategies: Map<
33 WorkerChoiceStrategy,
34 IWorkerChoiceStrategy
35 >
36
37 /**
38 * Worker choice strategy context constructor.
39 *
40 * @param pool - The pool instance.
41 * @param workerChoiceStrategy - The worker choice strategy.
42 * @param opts - The worker choice strategy options.
43 */
44 public constructor (
45 pool: IPool<Worker, Data, Response>,
46 private workerChoiceStrategy: WorkerChoiceStrategy = WorkerChoiceStrategies.ROUND_ROBIN,
47 opts: WorkerChoiceStrategyOptions = DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
48 ) {
49 this.execute = this.execute.bind(this)
50 this.workerChoiceStrategies = new Map<
51 WorkerChoiceStrategy,
52 IWorkerChoiceStrategy
53 >([
54 [
55 WorkerChoiceStrategies.ROUND_ROBIN,
56 new (RoundRobinWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
57 pool,
58 opts
59 )
60 ],
61 [
62 WorkerChoiceStrategies.LEAST_USED,
63 new (LeastUsedWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
64 pool,
65 opts
66 )
67 ],
68 [
69 WorkerChoiceStrategies.LEAST_BUSY,
70 new (LeastBusyWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
71 pool,
72 opts
73 )
74 ],
75 [
76 WorkerChoiceStrategies.LEAST_ELU,
77 new (LeastEluWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
78 pool,
79 opts
80 )
81 ],
82 [
83 WorkerChoiceStrategies.FAIR_SHARE,
84 new (FairShareWorkerChoiceStrategy.bind(this))<Worker, Data, Response>(
85 pool,
86 opts
87 )
88 ],
89 [
90 WorkerChoiceStrategies.WEIGHTED_ROUND_ROBIN,
91 new (WeightedRoundRobinWorkerChoiceStrategy.bind(this))<
92 Worker,
93 Data,
94 Response
95 >(pool, opts)
96 ],
97 [
98 WorkerChoiceStrategies.INTERLEAVED_WEIGHTED_ROUND_ROBIN,
99 new (InterleavedWeightedRoundRobinWorkerChoiceStrategy.bind(this))<
100 Worker,
101 Data,
102 Response
103 >(pool, opts)
104 ]
105 ])
106 }
107
108 /**
109 * Gets the strategy policy in the context.
110 *
111 * @returns The strategy policy.
112 */
113 public getStrategyPolicy (): StrategyPolicy {
114 return (
115 this.workerChoiceStrategies.get(
116 this.workerChoiceStrategy
117 ) as IWorkerChoiceStrategy
118 ).strategyPolicy
119 }
120
121 /**
122 * Gets the worker choice strategy task statistics requirements in the context.
123 *
124 * @returns The task statistics requirements.
125 */
126 public getTaskStatisticsRequirements (): TaskStatisticsRequirements {
127 return (
128 this.workerChoiceStrategies.get(
129 this.workerChoiceStrategy
130 ) as IWorkerChoiceStrategy
131 ).taskStatisticsRequirements
132 }
133
134 /**
135 * Sets the worker choice strategy to use in the context.
136 *
137 * @param workerChoiceStrategy - The worker choice strategy to set.
138 */
139 public setWorkerChoiceStrategy (
140 workerChoiceStrategy: WorkerChoiceStrategy
141 ): void {
142 if (this.workerChoiceStrategy !== workerChoiceStrategy) {
143 this.workerChoiceStrategy = workerChoiceStrategy
144 }
145 this.workerChoiceStrategies.get(this.workerChoiceStrategy)?.reset()
146 }
147
148 /**
149 * Updates the worker node key in the worker choice strategy internals in the context.
150 *
151 * @returns `true` if the update is successful, `false` otherwise.
152 */
153 public update (workerNodeKey: number): boolean {
154 return (
155 this.workerChoiceStrategies.get(
156 this.workerChoiceStrategy
157 ) as IWorkerChoiceStrategy
158 ).update(workerNodeKey)
159 }
160
161 /**
162 * Executes the worker choice strategy algorithm in the context.
163 *
164 * @returns The key of the worker node.
165 * @throws {@link Error} If the worker node key is null or undefined.
166 */
167 public execute (): number {
168 const workerNodeKey = (
169 this.workerChoiceStrategies.get(
170 this.workerChoiceStrategy
171 ) as IWorkerChoiceStrategy
172 ).choose()
173 if (workerNodeKey == null) {
174 throw new Error('Worker node key chosen is null or undefined')
175 }
176 return workerNodeKey
177 }
178
179 /**
180 * Removes the worker node key from the worker choice strategy in the context.
181 *
182 * @param workerNodeKey - The key of the worker node.
183 * @returns `true` if the removal is successful, `false` otherwise.
184 */
185 public remove (workerNodeKey: number): boolean {
186 return (
187 this.workerChoiceStrategies.get(
188 this.workerChoiceStrategy
189 ) as IWorkerChoiceStrategy
190 ).remove(workerNodeKey)
191 }
192
193 /**
194 * Sets the worker choice strategies in the context options.
195 *
196 * @param opts - The worker choice strategy options.
197 */
198 public setOptions (opts: WorkerChoiceStrategyOptions): void {
199 for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
200 workerChoiceStrategy.setOptions(opts)
201 }
202 }
203 }