1 import { randomUUID
} from
'node:crypto'
2 import { performance
} from
'node:perf_hooks'
3 import { existsSync
} from
'node:fs'
6 PromiseResponseWrapper
,
8 } from
'../utility-types'
11 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
,
17 updateMeasurementStatistics
19 import { KillBehaviors
} from
'../worker/worker-options'
28 type TasksQueueOptions
38 type MeasurementStatisticsRequirements
,
40 WorkerChoiceStrategies
,
41 type WorkerChoiceStrategy
,
42 type WorkerChoiceStrategyOptions
43 } from
'./selection-strategies/selection-strategies-types'
44 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
45 import { version
} from
'./version'
46 import { WorkerNode
} from
'./worker-node'
49 * Base class that implements some shared logic for all poolifier pools.
51 * @typeParam Worker - Type of worker which manages this pool.
52 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
53 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
55 export abstract class AbstractPool
<
56 Worker
extends IWorker
,
59 > implements IPool
<Worker
, Data
, Response
> {
61 public readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>> = []
64 public readonly emitter
?: PoolEmitter
67 * The task execution response promise map.
69 * - `key`: The message id of each submitted task.
70 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
72 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
74 protected promiseResponseMap
: Map
<string, PromiseResponseWrapper
<Response
>> =
75 new Map
<string, PromiseResponseWrapper
<Response
>>()
78 * Worker choice strategy context referencing a worker choice algorithm implementation.
80 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
87 * Whether the pool is starting or not.
89 private readonly starting
: boolean
91 * The start timestamp of the pool.
93 private readonly startTimestamp
96 * Constructs a new poolifier pool.
98 * @param numberOfWorkers - Number of workers that this pool should manage.
99 * @param filePath - Path to the worker file.
100 * @param opts - Options for the pool.
103 protected readonly numberOfWorkers
: number,
104 protected readonly filePath
: string,
105 protected readonly opts
: PoolOptions
<Worker
>
107 if (!this.isMain()) {
108 throw new Error('Cannot start a pool from a worker!')
110 this.checkNumberOfWorkers(this.numberOfWorkers
)
111 this.checkFilePath(this.filePath
)
112 this.checkPoolOptions(this.opts
)
114 this.chooseWorkerNode
= this.chooseWorkerNode
.bind(this)
115 this.executeTask
= this.executeTask
.bind(this)
116 this.enqueueTask
= this.enqueueTask
.bind(this)
117 this.dequeueTask
= this.dequeueTask
.bind(this)
118 this.checkAndEmitEvents
= this.checkAndEmitEvents
.bind(this)
120 if (this.opts
.enableEvents
=== true) {
121 this.emitter
= new PoolEmitter()
123 this.workerChoiceStrategyContext
= new WorkerChoiceStrategyContext
<
129 this.opts
.workerChoiceStrategy
,
130 this.opts
.workerChoiceStrategyOptions
137 this.starting
= false
139 this.startTimestamp
= performance
.now()
142 private checkFilePath (filePath
: string): void {
145 typeof filePath
!== 'string' ||
146 (typeof filePath
=== 'string' && filePath
.trim().length
=== 0)
148 throw new Error('Please specify a file with a worker implementation')
150 if (!existsSync(filePath
)) {
151 throw new Error(`Cannot find the worker file '${filePath}'`)
155 private checkNumberOfWorkers (numberOfWorkers
: number): void {
156 if (numberOfWorkers
== null) {
158 'Cannot instantiate a pool without specifying the number of workers'
160 } else if (!Number.isSafeInteger(numberOfWorkers
)) {
162 'Cannot instantiate a pool with a non safe integer number of workers'
164 } else if (numberOfWorkers
< 0) {
165 throw new RangeError(
166 'Cannot instantiate a pool with a negative number of workers'
168 } else if (this.type === PoolTypes
.fixed
&& numberOfWorkers
=== 0) {
169 throw new RangeError('Cannot instantiate a fixed pool with zero worker')
173 protected checkDynamicPoolSize (min
: number, max
: number): void {
174 if (this.type === PoolTypes
.dynamic
) {
177 'Cannot instantiate a dynamic pool without specifying the maximum pool size'
179 } else if (!Number.isSafeInteger(max
)) {
181 'Cannot instantiate a dynamic pool with a non safe integer maximum pool size'
183 } else if (min
> max
) {
184 throw new RangeError(
185 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
187 } else if (max
=== 0) {
188 throw new RangeError(
189 'Cannot instantiate a dynamic pool with a pool size equal to zero'
191 } else if (min
=== max
) {
192 throw new RangeError(
193 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
199 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
200 if (isPlainObject(opts
)) {
201 this.opts
.workerChoiceStrategy
=
202 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
203 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
204 this.opts
.workerChoiceStrategyOptions
=
205 opts
.workerChoiceStrategyOptions
??
206 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
207 this.checkValidWorkerChoiceStrategyOptions(
208 this.opts
.workerChoiceStrategyOptions
210 this.opts
.restartWorkerOnError
= opts
.restartWorkerOnError
?? true
211 this.opts
.enableEvents
= opts
.enableEvents
?? true
212 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
213 if (this.opts
.enableTasksQueue
) {
214 this.checkValidTasksQueueOptions(
215 opts
.tasksQueueOptions
as TasksQueueOptions
217 this.opts
.tasksQueueOptions
= this.buildTasksQueueOptions(
218 opts
.tasksQueueOptions
as TasksQueueOptions
222 throw new TypeError('Invalid pool options: must be a plain object')
226 private checkValidWorkerChoiceStrategy (
227 workerChoiceStrategy
: WorkerChoiceStrategy
229 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
231 `Invalid worker choice strategy '${workerChoiceStrategy}'`
236 private checkValidWorkerChoiceStrategyOptions (
237 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
239 if (!isPlainObject(workerChoiceStrategyOptions
)) {
241 'Invalid worker choice strategy options: must be a plain object'
245 workerChoiceStrategyOptions
.weights
!= null &&
246 Object.keys(workerChoiceStrategyOptions
.weights
).length
!== this.maxSize
249 'Invalid worker choice strategy options: must have a weight for each worker node'
253 workerChoiceStrategyOptions
.measurement
!= null &&
254 !Object.values(Measurements
).includes(
255 workerChoiceStrategyOptions
.measurement
259 `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
264 private checkValidTasksQueueOptions (
265 tasksQueueOptions
: TasksQueueOptions
267 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
268 throw new TypeError('Invalid tasks queue options: must be a plain object')
271 tasksQueueOptions
?.concurrency
!= null &&
272 !Number.isSafeInteger(tasksQueueOptions
.concurrency
)
275 'Invalid worker tasks concurrency: must be an integer'
279 tasksQueueOptions
?.concurrency
!= null &&
280 tasksQueueOptions
.concurrency
<= 0
283 `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
288 private startPool (): void {
290 this.workerNodes
.reduce(
291 (accumulator
, workerNode
) =>
292 !workerNode
.info
.dynamic
? accumulator
+ 1 : accumulator
,
294 ) < this.numberOfWorkers
296 this.createAndSetupWorkerNode()
301 public get
info (): PoolInfo
{
307 strategy
: this.opts
.workerChoiceStrategy
as WorkerChoiceStrategy
,
308 minSize
: this.minSize
,
309 maxSize
: this.maxSize
,
310 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
311 .runTime
.aggregate
&&
312 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
313 .waitTime
.aggregate
&& { utilization
: round(this.utilization
) }),
314 workerNodes
: this.workerNodes
.length
,
315 idleWorkerNodes
: this.workerNodes
.reduce(
316 (accumulator
, workerNode
) =>
317 workerNode
.usage
.tasks
.executing
=== 0
322 busyWorkerNodes
: this.workerNodes
.reduce(
323 (accumulator
, workerNode
) =>
324 workerNode
.usage
.tasks
.executing
> 0 ? accumulator
+ 1 : accumulator
,
327 executedTasks
: this.workerNodes
.reduce(
328 (accumulator
, workerNode
) =>
329 accumulator
+ workerNode
.usage
.tasks
.executed
,
332 executingTasks
: this.workerNodes
.reduce(
333 (accumulator
, workerNode
) =>
334 accumulator
+ workerNode
.usage
.tasks
.executing
,
337 queuedTasks
: this.workerNodes
.reduce(
338 (accumulator
, workerNode
) =>
339 accumulator
+ workerNode
.usage
.tasks
.queued
,
342 maxQueuedTasks
: this.workerNodes
.reduce(
343 (accumulator
, workerNode
) =>
344 accumulator
+ (workerNode
.usage
.tasks
?.maxQueued
?? 0),
347 failedTasks
: this.workerNodes
.reduce(
348 (accumulator
, workerNode
) =>
349 accumulator
+ workerNode
.usage
.tasks
.failed
,
352 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
353 .runTime
.aggregate
&& {
357 ...this.workerNodes
.map(
358 workerNode
=> workerNode
.usage
.runTime
?.minimum
?? Infinity
364 ...this.workerNodes
.map(
365 workerNode
=> workerNode
.usage
.runTime
?.maximum
?? -Infinity
370 this.workerNodes
.reduce(
371 (accumulator
, workerNode
) =>
372 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
375 this.workerNodes
.reduce(
376 (accumulator
, workerNode
) =>
377 accumulator
+ (workerNode
.usage
.tasks
?.executed
?? 0),
381 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
385 this.workerNodes
.map(
386 workerNode
=> workerNode
.usage
.runTime
?.median
?? 0
393 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
394 .waitTime
.aggregate
&& {
398 ...this.workerNodes
.map(
399 workerNode
=> workerNode
.usage
.waitTime
?.minimum
?? Infinity
405 ...this.workerNodes
.map(
406 workerNode
=> workerNode
.usage
.waitTime
?.maximum
?? -Infinity
411 this.workerNodes
.reduce(
412 (accumulator
, workerNode
) =>
413 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
416 this.workerNodes
.reduce(
417 (accumulator
, workerNode
) =>
418 accumulator
+ (workerNode
.usage
.tasks
?.executed
?? 0),
422 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
423 .waitTime
.median
&& {
426 this.workerNodes
.map(
427 workerNode
=> workerNode
.usage
.waitTime
?.median
?? 0
438 * The pool readiness boolean status.
440 private get
ready (): boolean {
442 this.workerNodes
.reduce(
443 (accumulator
, workerNode
) =>
444 !workerNode
.info
.dynamic
&& workerNode
.info
.ready
453 * The approximate pool utilization.
455 * @returns The pool utilization.
457 private get
utilization (): number {
458 const poolTimeCapacity
=
459 (performance
.now() - this.startTimestamp
) * this.maxSize
460 const totalTasksRunTime
= this.workerNodes
.reduce(
461 (accumulator
, workerNode
) =>
462 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
465 const totalTasksWaitTime
= this.workerNodes
.reduce(
466 (accumulator
, workerNode
) =>
467 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
470 return (totalTasksRunTime
+ totalTasksWaitTime
) / poolTimeCapacity
476 * If it is `'dynamic'`, it provides the `max` property.
478 protected abstract get
type (): PoolType
483 protected abstract get
worker (): WorkerType
486 * The pool minimum size.
488 protected abstract get
minSize (): number
491 * The pool maximum size.
493 protected abstract get
maxSize (): number
496 * Checks if the worker id sent in the received message from a worker is valid.
498 * @param message - The received message.
499 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
501 private checkMessageWorkerId (message
: MessageValue
<Response
>): void {
503 message
.workerId
!= null &&
504 this.getWorkerNodeKeyByWorkerId(message
.workerId
) === -1
507 `Worker message received from unknown worker '${message.workerId}'`
513 * Gets the given worker its worker node key.
515 * @param worker - The worker.
516 * @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
518 private getWorkerNodeKeyByWorker (worker
: Worker
): number {
519 return this.workerNodes
.findIndex(
520 workerNode
=> workerNode
.worker
=== worker
525 * Gets the worker node key given its worker id.
527 * @param workerId - The worker id.
528 * @returns The worker node key if the worker id is found in the pool worker nodes, `-1` otherwise.
530 private getWorkerNodeKeyByWorkerId (workerId
: number): number {
531 return this.workerNodes
.findIndex(
532 workerNode
=> workerNode
.info
.id
=== workerId
537 public setWorkerChoiceStrategy (
538 workerChoiceStrategy
: WorkerChoiceStrategy
,
539 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
541 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
542 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
543 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
544 this.opts
.workerChoiceStrategy
546 if (workerChoiceStrategyOptions
!= null) {
547 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
549 for (const [workerNodeKey
, workerNode
] of this.workerNodes
.entries()) {
550 workerNode
.resetUsage()
551 this.sendWorkerStatisticsMessageToWorker(workerNodeKey
)
556 public setWorkerChoiceStrategyOptions (
557 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
559 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
560 this.opts
.workerChoiceStrategyOptions
= workerChoiceStrategyOptions
561 this.workerChoiceStrategyContext
.setOptions(
562 this.opts
.workerChoiceStrategyOptions
567 public enableTasksQueue (
569 tasksQueueOptions
?: TasksQueueOptions
571 if (this.opts
.enableTasksQueue
=== true && !enable
) {
572 this.flushTasksQueues()
574 this.opts
.enableTasksQueue
= enable
575 this.setTasksQueueOptions(tasksQueueOptions
as TasksQueueOptions
)
579 public setTasksQueueOptions (tasksQueueOptions
: TasksQueueOptions
): void {
580 if (this.opts
.enableTasksQueue
=== true) {
581 this.checkValidTasksQueueOptions(tasksQueueOptions
)
582 this.opts
.tasksQueueOptions
=
583 this.buildTasksQueueOptions(tasksQueueOptions
)
584 } else if (this.opts
.tasksQueueOptions
!= null) {
585 delete this.opts
.tasksQueueOptions
589 private buildTasksQueueOptions (
590 tasksQueueOptions
: TasksQueueOptions
591 ): TasksQueueOptions
{
593 concurrency
: tasksQueueOptions
?.concurrency
?? 1
598 * Whether the pool is full or not.
600 * The pool filling boolean status.
602 protected get
full (): boolean {
603 return this.workerNodes
.length
>= this.maxSize
607 * Whether the pool is busy or not.
609 * The pool busyness boolean status.
611 protected abstract get
busy (): boolean
614 * Whether worker nodes are executing at least one task.
616 * @returns Worker nodes busyness boolean status.
618 protected internalBusy (): boolean {
620 this.workerNodes
.findIndex(
622 workerNode
.info
.ready
&& workerNode
.usage
.tasks
.executing
=== 0
628 public async execute (data
?: Data
, name
?: string): Promise
<Response
> {
629 return await new Promise
<Response
>((resolve
, reject
) => {
630 const timestamp
= performance
.now()
631 const workerNodeKey
= this.chooseWorkerNode()
632 const task
: Task
<Data
> = {
633 name
: name
?? DEFAULT_TASK_NAME
,
634 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
635 data
: data
?? ({} as Data
),
637 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number,
640 this.promiseResponseMap
.set(task
.id
as string, {
646 this.opts
.enableTasksQueue
=== false ||
647 (this.opts
.enableTasksQueue
=== true &&
648 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
649 (this.opts
.tasksQueueOptions
?.concurrency
as number))
651 this.executeTask(workerNodeKey
, task
)
653 this.enqueueTask(workerNodeKey
, task
)
655 this.checkAndEmitEvents()
660 public async destroy (): Promise
<void> {
662 this.workerNodes
.map(async (_
, workerNodeKey
) => {
663 await this.destroyWorkerNode(workerNodeKey
)
669 * Terminates the worker node given its worker node key.
671 * @param workerNodeKey - The worker node key.
673 protected abstract destroyWorkerNode (workerNodeKey
: number): Promise
<void>
676 * Setup hook to execute code before worker nodes are created in the abstract constructor.
681 protected setupHook (): void {
682 // Intentionally empty
686 * Should return whether the worker is the main worker or not.
688 protected abstract isMain (): boolean
691 * Hook executed before the worker task execution.
694 * @param workerNodeKey - The worker node key.
695 * @param task - The task to execute.
697 protected beforeTaskExecutionHook (
698 workerNodeKey
: number,
701 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
702 ++workerUsage
.tasks
.executing
703 this.updateWaitTimeWorkerUsage(workerUsage
, task
)
704 const taskWorkerUsage
= this.workerNodes
[workerNodeKey
].getTaskWorkerUsage(
707 ++taskWorkerUsage
.tasks
.executing
708 this.updateWaitTimeWorkerUsage(taskWorkerUsage
, task
)
712 * Hook executed after the worker task execution.
715 * @param workerNodeKey - The worker node key.
716 * @param message - The received message.
718 protected afterTaskExecutionHook (
719 workerNodeKey
: number,
720 message
: MessageValue
<Response
>
722 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
723 this.updateTaskStatisticsWorkerUsage(workerUsage
, message
)
724 this.updateRunTimeWorkerUsage(workerUsage
, message
)
725 this.updateEluWorkerUsage(workerUsage
, message
)
726 const taskWorkerUsage
= this.workerNodes
[workerNodeKey
].getTaskWorkerUsage(
727 message
.taskPerformance
?.name
?? DEFAULT_TASK_NAME
729 this.updateTaskStatisticsWorkerUsage(taskWorkerUsage
, message
)
730 this.updateRunTimeWorkerUsage(taskWorkerUsage
, message
)
731 this.updateEluWorkerUsage(taskWorkerUsage
, message
)
734 private updateTaskStatisticsWorkerUsage (
735 workerUsage
: WorkerUsage
,
736 message
: MessageValue
<Response
>
738 const workerTaskStatistics
= workerUsage
.tasks
739 --workerTaskStatistics
.executing
740 if (message
.taskError
== null) {
741 ++workerTaskStatistics
.executed
743 ++workerTaskStatistics
.failed
747 private updateRunTimeWorkerUsage (
748 workerUsage
: WorkerUsage
,
749 message
: MessageValue
<Response
>
751 updateMeasurementStatistics(
753 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
,
754 message
.taskPerformance
?.runTime
?? 0,
755 workerUsage
.tasks
.executed
759 private updateWaitTimeWorkerUsage (
760 workerUsage
: WorkerUsage
,
763 const timestamp
= performance
.now()
764 const taskWaitTime
= timestamp
- (task
.timestamp
?? timestamp
)
765 updateMeasurementStatistics(
766 workerUsage
.waitTime
,
767 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().waitTime
,
769 workerUsage
.tasks
.executed
773 private updateEluWorkerUsage (
774 workerUsage
: WorkerUsage
,
775 message
: MessageValue
<Response
>
777 const eluTaskStatisticsRequirements
: MeasurementStatisticsRequirements
=
778 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().elu
779 updateMeasurementStatistics(
780 workerUsage
.elu
.active
,
781 eluTaskStatisticsRequirements
,
782 message
.taskPerformance
?.elu
?.active
?? 0,
783 workerUsage
.tasks
.executed
785 updateMeasurementStatistics(
786 workerUsage
.elu
.idle
,
787 eluTaskStatisticsRequirements
,
788 message
.taskPerformance
?.elu
?.idle
?? 0,
789 workerUsage
.tasks
.executed
791 if (eluTaskStatisticsRequirements
.aggregate
) {
792 if (message
.taskPerformance
?.elu
!= null) {
793 if (workerUsage
.elu
.utilization
!= null) {
794 workerUsage
.elu
.utilization
=
795 (workerUsage
.elu
.utilization
+
796 message
.taskPerformance
.elu
.utilization
) /
799 workerUsage
.elu
.utilization
= message
.taskPerformance
.elu
.utilization
806 * Chooses a worker node for the next task.
808 * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
810 * @returns The chosen worker node key
812 private chooseWorkerNode (): number {
813 if (this.shallCreateDynamicWorker()) {
814 const workerNodeKey
= this.createAndSetupDynamicWorkerNode()
816 this.workerChoiceStrategyContext
.getStrategyPolicy().useDynamicWorker
821 return this.workerChoiceStrategyContext
.execute()
825 * Conditions for dynamic worker creation.
827 * @returns Whether to create a dynamic worker or not.
829 private shallCreateDynamicWorker (): boolean {
830 return this.type === PoolTypes
.dynamic
&& !this.full
&& this.internalBusy()
834 * Sends a message to worker given its worker node key.
836 * @param workerNodeKey - The worker node key.
837 * @param message - The message.
839 protected abstract sendToWorker (
840 workerNodeKey
: number,
841 message
: MessageValue
<Data
>
845 * Creates a new worker.
847 * @returns Newly created worker.
849 protected abstract createWorker (): Worker
852 * Creates a new, completely set up worker node.
854 * @returns New, completely set up worker node key.
856 protected createAndSetupWorkerNode (): number {
857 const worker
= this.createWorker()
859 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
860 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
861 worker
.on('error', error
=> {
862 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
863 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
864 workerInfo
.ready
= false
865 this.workerNodes
[workerNodeKey
].closeChannel()
866 this.emitter
?.emit(PoolEvents
.error
, error
)
867 if (this.opts
.restartWorkerOnError
=== true && !this.starting
) {
868 if (workerInfo
.dynamic
) {
869 this.createAndSetupDynamicWorkerNode()
871 this.createAndSetupWorkerNode()
874 if (this.opts
.enableTasksQueue
=== true) {
875 this.redistributeQueuedTasks(workerNodeKey
)
878 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
879 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
880 worker
.once('exit', () => {
881 this.removeWorkerNode(worker
)
884 const workerNodeKey
= this.addWorkerNode(worker
)
886 this.afterWorkerNodeSetup(workerNodeKey
)
892 * Creates a new, completely set up dynamic worker node.
894 * @returns New, completely set up dynamic worker node key.
896 protected createAndSetupDynamicWorkerNode (): number {
897 const workerNodeKey
= this.createAndSetupWorkerNode()
898 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
899 const localWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(
902 const workerUsage
= this.workerNodes
[localWorkerNodeKey
].usage
903 // Kill message received from worker
905 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
906 (message
.kill
!= null &&
907 ((this.opts
.enableTasksQueue
=== false &&
908 workerUsage
.tasks
.executing
=== 0) ||
909 (this.opts
.enableTasksQueue
=== true &&
910 workerUsage
.tasks
.executing
=== 0 &&
911 this.tasksQueueSize(localWorkerNodeKey
) === 0)))
913 this.destroyWorkerNode(localWorkerNodeKey
).catch(EMPTY_FUNCTION
)
916 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
917 this.sendToWorker(workerNodeKey
, {
919 workerId
: workerInfo
.id
as number
921 workerInfo
.dynamic
= true
922 if (this.workerChoiceStrategyContext
.getStrategyPolicy().useDynamicWorker
) {
923 workerInfo
.ready
= true
929 * Registers a listener callback on the worker given its worker node key.
931 * @param workerNodeKey - The worker node key.
932 * @param listener - The message listener callback.
934 protected abstract registerWorkerMessageListener
<
935 Message
extends Data
| Response
937 workerNodeKey
: number,
938 listener
: (message
: MessageValue
<Message
>) => void
942 * Method hooked up after a worker node has been newly created.
945 * @param workerNodeKey - The newly created worker node key.
947 protected afterWorkerNodeSetup (workerNodeKey
: number): void {
948 // Listen to worker messages.
949 this.registerWorkerMessageListener(workerNodeKey
, this.workerListener())
950 // Send the startup message to worker.
951 this.sendStartupMessageToWorker(workerNodeKey
)
952 // Send the worker statistics message to worker.
953 this.sendWorkerStatisticsMessageToWorker(workerNodeKey
)
957 * Sends the startup message to worker given its worker node key.
959 * @param workerNodeKey - The worker node key.
961 protected abstract sendStartupMessageToWorker (workerNodeKey
: number): void
964 * Sends the worker statistics message to worker given its worker node key.
966 * @param workerNodeKey - The worker node key.
968 private sendWorkerStatisticsMessageToWorker (workerNodeKey
: number): void {
969 this.sendToWorker(workerNodeKey
, {
972 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
974 elu
: this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
977 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number
981 private redistributeQueuedTasks (workerNodeKey
: number): void {
982 while (this.tasksQueueSize(workerNodeKey
) > 0) {
983 let targetWorkerNodeKey
: number = workerNodeKey
984 let minQueuedTasks
= Infinity
985 let executeTask
= false
986 for (const [workerNodeId
, workerNode
] of this.workerNodes
.entries()) {
987 const workerInfo
= this.getWorkerInfo(workerNodeId
)
989 workerNodeId
!== workerNodeKey
&&
991 workerNode
.usage
.tasks
.queued
=== 0
994 this.workerNodes
[workerNodeId
].usage
.tasks
.executing
<
995 (this.opts
.tasksQueueOptions
?.concurrency
as number)
999 targetWorkerNodeKey
= workerNodeId
1003 workerNodeId
!== workerNodeKey
&&
1005 workerNode
.usage
.tasks
.queued
< minQueuedTasks
1007 minQueuedTasks
= workerNode
.usage
.tasks
.queued
1008 targetWorkerNodeKey
= workerNodeId
1013 targetWorkerNodeKey
,
1014 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1018 targetWorkerNodeKey
,
1019 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1026 * This method is the listener registered for each worker message.
1028 * @returns The listener function to execute when a message is received from a worker.
1030 protected workerListener (): (message
: MessageValue
<Response
>) => void {
1032 this.checkMessageWorkerId(message
)
1033 if (message
.ready
!= null) {
1034 // Worker ready response received from worker
1035 this.handleWorkerReadyResponse(message
)
1036 } else if (message
.id
!= null) {
1037 // Task execution response received from worker
1038 this.handleTaskExecutionResponse(message
)
1043 private handleWorkerReadyResponse (message
: MessageValue
<Response
>): void {
1045 this.getWorkerNodeKeyByWorkerId(message
.workerId
)
1046 ).ready
= message
.ready
as boolean
1047 if (this.emitter
!= null && this.ready
) {
1048 this.emitter
.emit(PoolEvents
.ready
, this.info
)
1052 private handleTaskExecutionResponse (message
: MessageValue
<Response
>): void {
1053 const promiseResponse
= this.promiseResponseMap
.get(message
.id
as string)
1054 if (promiseResponse
!= null) {
1055 if (message
.taskError
!= null) {
1056 this.emitter
?.emit(PoolEvents
.taskError
, message
.taskError
)
1057 promiseResponse
.reject(message
.taskError
.message
)
1059 promiseResponse
.resolve(message
.data
as Response
)
1061 const workerNodeKey
= promiseResponse
.workerNodeKey
1062 this.afterTaskExecutionHook(workerNodeKey
, message
)
1063 this.promiseResponseMap
.delete(message
.id
as string)
1065 this.opts
.enableTasksQueue
=== true &&
1066 this.tasksQueueSize(workerNodeKey
) > 0 &&
1067 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
<
1068 (this.opts
.tasksQueueOptions
?.concurrency
as number)
1072 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1075 this.workerChoiceStrategyContext
.update(workerNodeKey
)
1079 private checkAndEmitEvents (): void {
1080 if (this.emitter
!= null) {
1082 this.emitter
.emit(PoolEvents
.busy
, this.info
)
1084 if (this.type === PoolTypes
.dynamic
&& this.full
) {
1085 this.emitter
.emit(PoolEvents
.full
, this.info
)
1091 * Gets the worker information given its worker node key.
1093 * @param workerNodeKey - The worker node key.
1094 * @returns The worker information.
1096 protected getWorkerInfo (workerNodeKey
: number): WorkerInfo
{
1097 return this.workerNodes
[workerNodeKey
].info
1101 * Adds the given worker in the pool worker nodes.
1103 * @param worker - The worker.
1104 * @returns The added worker node key.
1105 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1107 private addWorkerNode (worker
: Worker
): number {
1108 const workerNode
= new WorkerNode
<Worker
, Data
>(worker
, this.worker
)
1109 // Flag the worker node as ready at pool startup.
1110 if (this.starting
) {
1111 workerNode
.info
.ready
= true
1113 this.workerNodes
.push(workerNode
)
1114 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1115 if (workerNodeKey
=== -1) {
1116 throw new Error('Worker node not found')
1118 return workerNodeKey
1122 * Removes the given worker from the pool worker nodes.
1124 * @param worker - The worker.
1126 private removeWorkerNode (worker
: Worker
): void {
1127 const workerNodeKey
= this.getWorkerNodeKeyByWorker(worker
)
1128 if (workerNodeKey
!== -1) {
1129 this.workerNodes
.splice(workerNodeKey
, 1)
1130 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
1135 * Executes the given task on the worker given its worker node key.
1137 * @param workerNodeKey - The worker node key.
1138 * @param task - The task to execute.
1140 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
1141 this.beforeTaskExecutionHook(workerNodeKey
, task
)
1142 this.sendToWorker(workerNodeKey
, task
)
1145 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
1146 return this.workerNodes
[workerNodeKey
].enqueueTask(task
)
1149 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
1150 return this.workerNodes
[workerNodeKey
].dequeueTask()
1153 private tasksQueueSize (workerNodeKey
: number): number {
1154 return this.workerNodes
[workerNodeKey
].tasksQueueSize()
1157 protected flushTasksQueue (workerNodeKey
: number): void {
1158 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1161 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1164 this.workerNodes
[workerNodeKey
].clearTasksQueue()
1167 private flushTasksQueues (): void {
1168 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
1169 this.flushTasksQueue(workerNodeKey
)