refactor: align findFreeWorkerKey() return type with findIndex()
[poolifier.git] / src / pools / abstract-pool.ts
1 import crypto from 'node:crypto'
2 import type { MessageValue, PromiseResponseWrapper } from '../utility-types'
3 import { EMPTY_FUNCTION } from '../utils'
4 import { KillBehaviors, isKillBehavior } from '../worker/worker-options'
5 import type { PoolOptions } from './pool'
6 import { PoolEmitter } from './pool'
7 import type { IPoolInternal, TasksUsage, WorkerType } from './pool-internal'
8 import { PoolType } from './pool-internal'
9 import type { IPoolWorker } from './pool-worker'
10 import {
11 WorkerChoiceStrategies,
12 type WorkerChoiceStrategy
13 } from './selection-strategies/selection-strategies-types'
14 import { WorkerChoiceStrategyContext } from './selection-strategies/worker-choice-strategy-context'
15
16 /**
17 * Base class that implements some shared logic for all poolifier pools.
18 *
19 * @typeParam Worker - Type of worker which manages this pool.
20 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
21 * @typeParam Response - Type of response of execution. This can only be serializable data.
22 */
23 export abstract class AbstractPool<
24 Worker extends IPoolWorker,
25 Data = unknown,
26 Response = unknown
27 > implements IPoolInternal<Worker, Data, Response> {
28 /** {@inheritDoc} */
29 public readonly workers: Array<WorkerType<Worker>> = []
30
31 /** {@inheritDoc} */
32 public readonly emitter?: PoolEmitter
33
34 /**
35 * The promise response map.
36 *
37 * - `key`: The message id of each submitted task.
38 * - `value`: An object that contains the worker, the promise resolve and reject callbacks.
39 *
40 * When we receive a message from the worker we get a map entry with the promise resolve/reject bound to the message.
41 */
42 protected promiseResponseMap: Map<
43 string,
44 PromiseResponseWrapper<Worker, Response>
45 > = new Map<string, PromiseResponseWrapper<Worker, Response>>()
46
47 /**
48 * Worker choice strategy instance implementing the worker choice algorithm.
49 *
50 * Default to a strategy implementing a round robin algorithm.
51 */
52 protected workerChoiceStrategyContext: WorkerChoiceStrategyContext<
53 Worker,
54 Data,
55 Response
56 >
57
58 /**
59 * Constructs a new poolifier pool.
60 *
61 * @param numberOfWorkers - Number of workers that this pool should manage.
62 * @param filePath - Path to the worker-file.
63 * @param opts - Options for the pool.
64 */
65 public constructor (
66 public readonly numberOfWorkers: number,
67 public readonly filePath: string,
68 public readonly opts: PoolOptions<Worker>
69 ) {
70 if (!this.isMain()) {
71 throw new Error('Cannot start a pool from a worker!')
72 }
73 this.checkNumberOfWorkers(this.numberOfWorkers)
74 this.checkFilePath(this.filePath)
75 this.checkPoolOptions(this.opts)
76 this.setupHook()
77
78 for (let i = 1; i <= this.numberOfWorkers; i++) {
79 this.createAndSetupWorker()
80 }
81
82 if (this.opts.enableEvents === true) {
83 this.emitter = new PoolEmitter()
84 }
85 this.workerChoiceStrategyContext = new WorkerChoiceStrategyContext(
86 this,
87 () => {
88 const createdWorker = this.createAndSetupWorker()
89 this.registerWorkerMessageListener(createdWorker, message => {
90 if (
91 isKillBehavior(KillBehaviors.HARD, message.kill) ||
92 this.getWorkerTasksUsage(createdWorker)?.running === 0
93 ) {
94 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
95 void this.destroyWorker(createdWorker)
96 }
97 })
98 return this.getWorkerKey(createdWorker)
99 },
100 this.opts.workerChoiceStrategy
101 )
102 }
103
104 private checkFilePath (filePath: string): void {
105 if (
106 filePath == null ||
107 (typeof filePath === 'string' && filePath.trim().length === 0)
108 ) {
109 throw new Error('Please specify a file with a worker implementation')
110 }
111 }
112
113 private checkNumberOfWorkers (numberOfWorkers: number): void {
114 if (numberOfWorkers == null) {
115 throw new Error(
116 'Cannot instantiate a pool without specifying the number of workers'
117 )
118 } else if (!Number.isSafeInteger(numberOfWorkers)) {
119 throw new TypeError(
120 'Cannot instantiate a pool with a non integer number of workers'
121 )
122 } else if (numberOfWorkers < 0) {
123 throw new RangeError(
124 'Cannot instantiate a pool with a negative number of workers'
125 )
126 } else if (this.type === PoolType.FIXED && numberOfWorkers === 0) {
127 throw new Error('Cannot instantiate a fixed pool with no worker')
128 }
129 }
130
131 private checkPoolOptions (opts: PoolOptions<Worker>): void {
132 this.opts.workerChoiceStrategy =
133 opts.workerChoiceStrategy ?? WorkerChoiceStrategies.ROUND_ROBIN
134 this.opts.enableEvents = opts.enableEvents ?? true
135 }
136
137 /** {@inheritDoc} */
138 public abstract get type (): PoolType
139
140 /** {@inheritDoc} */
141 public get numberOfRunningTasks (): number {
142 return this.promiseResponseMap.size
143 }
144
145 /**
146 * Gets the given worker key.
147 *
148 * @param worker - The worker.
149 * @returns The worker key if the worker is found in the pool, `-1` otherwise.
150 */
151 private getWorkerKey (worker: Worker): number {
152 return this.workers.findIndex(workerItem => workerItem.worker === worker)
153 }
154
155 /** {@inheritDoc} */
156 public setWorkerChoiceStrategy (
157 workerChoiceStrategy: WorkerChoiceStrategy
158 ): void {
159 this.opts.workerChoiceStrategy = workerChoiceStrategy
160 for (const [index, workerItem] of this.workers.entries()) {
161 this.setWorker(index, workerItem.worker, {
162 run: 0,
163 running: 0,
164 runTime: 0,
165 avgRunTime: 0,
166 error: 0
167 })
168 }
169 this.workerChoiceStrategyContext.setWorkerChoiceStrategy(
170 workerChoiceStrategy
171 )
172 }
173
174 /** {@inheritDoc} */
175 public abstract get busy (): boolean
176
177 protected internalGetBusyStatus (): boolean {
178 return (
179 this.numberOfRunningTasks >= this.numberOfWorkers &&
180 this.findFreeWorkerKey() === -1
181 )
182 }
183
184 /** {@inheritDoc} */
185 public findFreeWorkerKey (): number {
186 return this.workers.findIndex(workerItem => {
187 return workerItem.tasksUsage.running === 0
188 })
189 }
190
191 /** {@inheritDoc} */
192 public async execute (data: Data): Promise<Response> {
193 const [workerKey, worker] = this.chooseWorker()
194 const messageId = crypto.randomUUID()
195 const res = this.internalExecute(workerKey, worker, messageId)
196 this.checkAndEmitBusy()
197 this.sendToWorker(worker, {
198 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
199 data: data ?? ({} as Data),
200 id: messageId
201 })
202 // eslint-disable-next-line @typescript-eslint/return-await
203 return res
204 }
205
206 /** {@inheritDoc} */
207 public async destroy (): Promise<void> {
208 await Promise.all(
209 this.workers.map(async workerItem => {
210 await this.destroyWorker(workerItem.worker)
211 })
212 )
213 }
214
215 /**
216 * Shutdowns given worker.
217 *
218 * @param worker - A worker within `workers`.
219 */
220 protected abstract destroyWorker (worker: Worker): void | Promise<void>
221
222 /**
223 * Setup hook that can be overridden by a Poolifier pool implementation
224 * to run code before workers are created in the abstract constructor.
225 */
226 protected setupHook (): void {
227 // Can be overridden
228 }
229
230 /**
231 * Should return whether the worker is the main worker or not.
232 */
233 protected abstract isMain (): boolean
234
235 /**
236 * Hook executed before the worker task promise resolution.
237 * Can be overridden.
238 *
239 * @param workerKey - The worker key.
240 */
241 protected beforePromiseResponseHook (workerKey: number): void {
242 ++this.workers[workerKey].tasksUsage.running
243 }
244
245 /**
246 * Hook executed after the worker task promise resolution.
247 * Can be overridden.
248 *
249 * @param worker - The worker.
250 * @param message - The received message.
251 */
252 protected afterPromiseResponseHook (
253 worker: Worker,
254 message: MessageValue<Response>
255 ): void {
256 const workerTasksUsage = this.getWorkerTasksUsage(worker) as TasksUsage
257 --workerTasksUsage.running
258 ++workerTasksUsage.run
259 if (message.error != null) {
260 ++workerTasksUsage.error
261 }
262 if (this.workerChoiceStrategyContext.getRequiredStatistics().runTime) {
263 workerTasksUsage.runTime += message.taskRunTime ?? 0
264 if (workerTasksUsage.run !== 0) {
265 workerTasksUsage.avgRunTime =
266 workerTasksUsage.runTime / workerTasksUsage.run
267 }
268 }
269 }
270
271 /**
272 * Removes the given worker from the pool.
273 *
274 * @param worker - The worker that will be removed.
275 */
276 protected removeWorker (worker: Worker): void {
277 const workerKey = this.getWorkerKey(worker)
278 this.workers.splice(workerKey, 1)
279 this.workerChoiceStrategyContext.remove(workerKey)
280 }
281
282 /**
283 * Chooses a worker for the next task.
284 *
285 * The default implementation uses a round robin algorithm to distribute the load.
286 *
287 * @returns [worker key, worker].
288 */
289 protected chooseWorker (): [number, Worker] {
290 const workerKey = this.workerChoiceStrategyContext.execute()
291 return [workerKey, this.workers[workerKey].worker]
292 }
293
294 /**
295 * Sends a message to the given worker.
296 *
297 * @param worker - The worker which should receive the message.
298 * @param message - The message.
299 */
300 protected abstract sendToWorker (
301 worker: Worker,
302 message: MessageValue<Data>
303 ): void
304
305 /**
306 * Registers a listener callback on a given worker.
307 *
308 * @param worker - The worker which should register a listener.
309 * @param listener - The message listener callback.
310 */
311 protected abstract registerWorkerMessageListener<
312 Message extends Data | Response
313 >(worker: Worker, listener: (message: MessageValue<Message>) => void): void
314
315 /**
316 * Returns a newly created worker.
317 */
318 protected abstract createWorker (): Worker
319
320 /**
321 * Function that can be hooked up when a worker has been newly created and moved to the workers registry.
322 *
323 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
324 *
325 * @param worker - The newly created worker.
326 */
327 protected abstract afterWorkerSetup (worker: Worker): void
328
329 /**
330 * Creates a new worker for this pool and sets it up completely.
331 *
332 * @returns New, completely set up worker.
333 */
334 protected createAndSetupWorker (): Worker {
335 const worker = this.createWorker()
336
337 worker.on('message', this.opts.messageHandler ?? EMPTY_FUNCTION)
338 worker.on('error', this.opts.errorHandler ?? EMPTY_FUNCTION)
339 worker.on('online', this.opts.onlineHandler ?? EMPTY_FUNCTION)
340 worker.on('exit', this.opts.exitHandler ?? EMPTY_FUNCTION)
341 worker.once('exit', () => {
342 this.removeWorker(worker)
343 })
344
345 this.pushWorker(worker, {
346 run: 0,
347 running: 0,
348 runTime: 0,
349 avgRunTime: 0,
350 error: 0
351 })
352
353 this.afterWorkerSetup(worker)
354
355 return worker
356 }
357
358 /**
359 * This function is the listener registered for each worker.
360 *
361 * @returns The listener function to execute when a message is received from a worker.
362 */
363 protected workerListener (): (message: MessageValue<Response>) => void {
364 return message => {
365 if (message.id !== undefined) {
366 const promiseResponse = this.promiseResponseMap.get(message.id)
367 if (promiseResponse !== undefined) {
368 if (message.error != null) {
369 promiseResponse.reject(message.error)
370 } else {
371 promiseResponse.resolve(message.data as Response)
372 }
373 this.afterPromiseResponseHook(promiseResponse.worker, message)
374 this.promiseResponseMap.delete(message.id)
375 }
376 }
377 }
378 }
379
380 private async internalExecute (
381 workerKey: number,
382 worker: Worker,
383 messageId: string
384 ): Promise<Response> {
385 this.beforePromiseResponseHook(workerKey)
386 return await new Promise<Response>((resolve, reject) => {
387 this.promiseResponseMap.set(messageId, { resolve, reject, worker })
388 })
389 }
390
391 private checkAndEmitBusy (): void {
392 if (this.opts.enableEvents === true && this.busy) {
393 this.emitter?.emit('busy')
394 }
395 }
396
397 /**
398 * Gets worker tasks usage.
399 *
400 * @param worker - The worker.
401 * @returns The worker tasks usage.
402 */
403 private getWorkerTasksUsage (worker: Worker): TasksUsage | undefined {
404 const workerKey = this.getWorkerKey(worker)
405 if (workerKey !== -1) {
406 return this.workers[workerKey].tasksUsage
407 }
408 throw new Error('Worker could not be found in the pool')
409 }
410
411 /**
412 * Pushes the given worker.
413 *
414 * @param worker - The worker.
415 * @param tasksUsage - The worker tasks usage.
416 */
417 private pushWorker (worker: Worker, tasksUsage: TasksUsage): void {
418 this.workers.push({
419 worker,
420 tasksUsage
421 })
422 }
423
424 /**
425 * Sets the given worker.
426 *
427 * @param workerKey - The worker key.
428 * @param worker - The worker.
429 * @param tasksUsage - The worker tasks usage.
430 */
431 private setWorker (
432 workerKey: number,
433 worker: Worker,
434 tasksUsage: TasksUsage
435 ): void {
436 this.workers[workerKey] = {
437 worker,
438 tasksUsage
439 }
440 }
441 }