3 PromiseWorkerResponseWrapper
4 } from
'../utility-types'
5 import { EMPTY_FUNCTION
} from
'../utils'
6 import { KillBehaviors
, isKillBehavior
} from
'../worker/worker-options'
7 import type { PoolOptions
} from
'./pool'
8 import { PoolEmitter
} from
'./pool'
9 import type { IPoolInternal
, TasksUsage
, WorkerType
} from
'./pool-internal'
10 import { PoolType
} from
'./pool-internal'
11 import type { IPoolWorker
} from
'./pool-worker'
13 WorkerChoiceStrategies
,
14 type WorkerChoiceStrategy
15 } from
'./selection-strategies/selection-strategies-types'
16 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
19 * Base class that implements some shared logic for all poolifier pools.
21 * @typeParam Worker - Type of worker which manages this pool.
22 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
23 * @typeParam Response - Type of response of execution. This can only be serializable data.
25 export abstract class AbstractPool
<
26 Worker
extends IPoolWorker
,
29 > implements IPoolInternal
<Worker
, Data
, Response
> {
31 public readonly workers
: Map
<number, WorkerType
<Worker
>> = new Map
<
37 public readonly emitter
?: PoolEmitter
40 * Id of the next worker.
42 protected nextWorkerId
: number = 0
47 * - `key`: This is the message id of each submitted task.
48 * - `value`: An object that contains the worker, the resolve function and the reject function.
50 * When we receive a message from the worker we get a map entry and resolve/reject the promise based on the message.
52 protected promiseMap
: Map
<
54 PromiseWorkerResponseWrapper
<Worker
, Response
>
55 > = new Map
<string, PromiseWorkerResponseWrapper
<Worker
, Response
>>()
58 * Worker choice strategy instance implementing the worker choice algorithm.
60 * Default to a strategy implementing a round robin algorithm.
62 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
69 * Constructs a new poolifier pool.
71 * @param numberOfWorkers - Number of workers that this pool should manage.
72 * @param filePath - Path to the worker-file.
73 * @param opts - Options for the pool.
76 public readonly numberOfWorkers
: number,
77 public readonly filePath
: string,
78 public readonly opts
: PoolOptions
<Worker
>
81 throw new Error('Cannot start a pool from a worker!')
83 this.checkNumberOfWorkers(this.numberOfWorkers
)
84 this.checkFilePath(this.filePath
)
85 this.checkPoolOptions(this.opts
)
88 for (let i
= 1; i
<= this.numberOfWorkers
; i
++) {
89 this.createAndSetupWorker()
92 if (this.opts
.enableEvents
=== true) {
93 this.emitter
= new PoolEmitter()
95 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext(
98 const workerCreated
= this.createAndSetupWorker()
99 this.registerWorkerMessageListener(workerCreated
, message
=> {
101 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
102 this.getWorkerRunningTasks(workerCreated
) === 0
104 // Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
105 void this.destroyWorker(workerCreated
)
110 this.opts
.workerChoiceStrategy
114 private checkFilePath (filePath
: string): void {
117 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
119 throw new Error('Please specify a file with a worker implementation')
123 private checkNumberOfWorkers (numberOfWorkers
: number): void {
124 if (numberOfWorkers
== null) {
126 'Cannot instantiate a pool without specifying the number of workers'
128 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
130 'Cannot instantiate a pool with a non integer number of workers'
132 } else if (numberOfWorkers
< 0) {
133 throw new RangeError(
134 'Cannot instantiate a pool with a negative number of workers'
136 } else if (this.type === PoolType
.FIXED
&& numberOfWorkers
=== 0) {
137 throw new Error('Cannot instantiate a fixed pool with no worker')
141 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
142 this.opts
.workerChoiceStrategy
=
143 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
144 this.opts
.enableEvents
= opts
.enableEvents
?? true
148 public abstract get
type (): PoolType
151 public get
numberOfRunningTasks (): number {
152 return this.promiseMap
.size
156 * Gets the given worker key.
158 * @param worker - The worker.
159 * @returns The worker key.
161 private getWorkerKey (worker
: Worker
): number | undefined {
162 return [...this.workers
].find(([, value
]) => value
.worker
=== worker
)?.[0]
166 public getWorkerRunningTasks (worker
: Worker
): number | undefined {
167 return this.workers
.get(this.getWorkerKey(worker
) as number)?.tasksUsage
172 public getWorkerAverageTasksRunTime (worker
: Worker
): number | undefined {
173 return this.workers
.get(this.getWorkerKey(worker
) as number)?.tasksUsage
178 public setWorkerChoiceStrategy (
179 workerChoiceStrategy
: WorkerChoiceStrategy
181 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
182 for (const [key
, value
] of this.workers
) {
183 this.setWorker(key
, value
.worker
, {
190 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
196 public abstract get
busy (): boolean
198 protected internalGetBusyStatus (): boolean {
200 this.numberOfRunningTasks
>= this.numberOfWorkers
&&
201 this.findFreeWorker() === false
206 public findFreeWorker (): Worker
| false {
207 for (const value
of this.workers
.values()) {
208 if (value
.tasksUsage
.running
=== 0) {
209 // A worker is free, return the matching worker
217 public async execute (data
: Data
): Promise
<Response
> {
218 const worker
= this.chooseWorker()
219 const messageId
= crypto
.randomUUID()
220 const res
= this.internalExecute(worker
, messageId
)
221 this.checkAndEmitBusy()
222 this.sendToWorker(worker
, {
223 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
224 data
: data
?? ({} as Data
),
227 // eslint-disable-next-line @typescript-eslint/return-await
232 public async destroy (): Promise
<void> {
234 [...this.workers
].map(async ([, value
]) => {
235 await this.destroyWorker(value
.worker
)
241 * Shutdowns given worker.
243 * @param worker - A worker within `workers`.
245 protected abstract destroyWorker (worker
: Worker
): void | Promise
<void>
248 * Setup hook that can be overridden by a Poolifier pool implementation
249 * to run code before workers are created in the abstract constructor.
251 protected setupHook (): void {
256 * Should return whether the worker is the main worker or not.
258 protected abstract isMain (): boolean
261 * Hook executed before the worker task promise resolution.
264 * @param worker - The worker.
266 protected beforePromiseWorkerResponseHook (worker
: Worker
): void {
267 this.increaseWorkerRunningTasks(worker
)
271 * Hook executed after the worker task promise resolution.
274 * @param message - The received message.
275 * @param promise - The Promise response.
277 protected afterPromiseWorkerResponseHook (
278 message
: MessageValue
<Response
>,
279 promise
: PromiseWorkerResponseWrapper
<Worker
, Response
>
281 this.decreaseWorkerRunningTasks(promise
.worker
)
282 this.stepWorkerRunTasks(promise
.worker
, 1)
283 this.updateWorkerTasksRunTime(promise
.worker
, message
.taskRunTime
)
287 * Removes the given worker from the pool.
289 * @param worker - The worker that will be removed.
291 protected removeWorker (worker
: Worker
): void {
292 this.workers
.delete(this.getWorkerKey(worker
) as number)
297 * Chooses a worker for the next task.
299 * The default implementation uses a round robin algorithm to distribute the load.
303 protected chooseWorker (): Worker
{
304 return this.workerChoiceStrategyContext
.execute()
308 * Sends a message to the given worker.
310 * @param worker - The worker which should receive the message.
311 * @param message - The message.
313 protected abstract sendToWorker (
315 message
: MessageValue
<Data
>
319 * Registers a listener callback on a given worker.
321 * @param worker - The worker which should register a listener.
322 * @param listener - The message listener callback.
324 protected abstract registerWorkerMessageListener
<
325 Message
extends Data
| Response
326 >(worker
: Worker
, listener
: (message
: MessageValue
<Message
>) => void): void
329 * Returns a newly created worker.
331 protected abstract createWorker (): Worker
334 * Function that can be hooked up when a worker has been newly created and moved to the workers registry.
336 * Can be used to update the `maxListeners` or binding the `main-worker`\<-\>`worker` connection if not bind by default.
338 * @param worker - The newly created worker.
340 protected abstract afterWorkerSetup (worker
: Worker
): void
343 * Creates a new worker for this pool and sets it up completely.
345 * @returns New, completely set up worker.
347 protected createAndSetupWorker (): Worker
{
348 const worker
= this.createWorker()
350 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
351 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
352 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
353 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
354 worker
.once('exit', () => {
355 this.removeWorker(worker
)
358 this.setWorker(this.nextWorkerId
, worker
, {
366 this.afterWorkerSetup(worker
)
372 * This function is the listener registered for each worker.
374 * @returns The listener function to execute when a message is received from a worker.
376 protected workerListener (): (message
: MessageValue
<Response
>) => void {
378 if (message
.id
!== undefined) {
379 const promise
= this.promiseMap
.get(message
.id
)
380 if (promise
!== undefined) {
381 if (message
.error
!= null) {
382 promise
.reject(message
.error
)
384 promise
.resolve(message
.data
as Response
)
386 this.afterPromiseWorkerResponseHook(message
, promise
)
387 this.promiseMap
.delete(message
.id
)
393 private async internalExecute (
396 ): Promise
<Response
> {
397 this.beforePromiseWorkerResponseHook(worker
)
398 return await new Promise
<Response
>((resolve
, reject
) => {
399 this.promiseMap
.set(messageId
, { resolve
, reject
, worker
})
403 private checkAndEmitBusy (): void {
404 if (this.opts
.enableEvents
=== true && this.busy
) {
405 this.emitter
?.emit('busy')
410 * Increases the number of tasks that the given worker has applied.
412 * @param worker - Worker which running tasks is increased.
414 private increaseWorkerRunningTasks (worker
: Worker
): void {
415 this.stepWorkerRunningTasks(worker
, 1)
419 * Decreases the number of tasks that the given worker has applied.
421 * @param worker - Worker which running tasks is decreased.
423 private decreaseWorkerRunningTasks (worker
: Worker
): void {
424 this.stepWorkerRunningTasks(worker
, -1)
428 * Gets tasks usage of the given worker.
430 * @param worker - Worker which tasks usage is returned.
432 private getWorkerTasksUsage (worker
: Worker
): TasksUsage
| undefined {
433 if (this.checkWorker(worker
)) {
434 const workerKey
= this.getWorkerKey(worker
) as number
435 const workerEntry
= this.workers
.get(workerKey
) as WorkerType
<Worker
>
436 return workerEntry
.tasksUsage
441 * Steps the number of tasks that the given worker has applied.
443 * @param worker - Worker which running tasks are stepped.
444 * @param step - Number of running tasks step.
446 private stepWorkerRunningTasks (worker
: Worker
, step
: number): void {
448 (this.getWorkerTasksUsage(worker
) as TasksUsage
).running
+= step
452 * Steps the number of tasks that the given worker has run.
454 * @param worker - Worker which has run tasks.
455 * @param step - Number of run tasks step.
457 private stepWorkerRunTasks (worker
: Worker
, step
: number): void {
459 (this.getWorkerTasksUsage(worker
) as TasksUsage
).run
+= step
463 * Updates tasks runtime for the given worker.
465 * @param worker - Worker which run the task.
466 * @param taskRunTime - Worker task runtime.
468 private updateWorkerTasksRunTime (
470 taskRunTime
: number | undefined
473 this.workerChoiceStrategyContext
.getWorkerChoiceStrategy()
474 .requiredStatistics
.runTime
476 const workerTasksUsage
= this.getWorkerTasksUsage(worker
) as TasksUsage
477 workerTasksUsage
.runTime
+= taskRunTime
?? 0
478 if (workerTasksUsage
.run
!== 0) {
479 workerTasksUsage
.avgRunTime
=
480 workerTasksUsage
.runTime
/ workerTasksUsage
.run
486 * Sets the given worker.
488 * @param workerKey - The worker key.
489 * @param worker - The worker.
490 * @param tasksUsage - The worker tasks usage.
495 tasksUsage
: TasksUsage
497 this.workers
.set(workerKey
, {
504 * Checks if the given worker is registered in the pool.
506 * @param worker - Worker to check.
507 * @returns `true` if the worker is registered in the pool.
509 private checkWorker (worker
: Worker
): boolean {
510 if (this.getWorkerKey(worker
) == null) {
511 throw new Error('Worker could not be found in the pool')