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
,
18 updateMeasurementStatistics
20 import { KillBehaviors
} from
'../worker/worker-options'
29 type TasksQueueOptions
39 type MeasurementStatisticsRequirements
,
41 WorkerChoiceStrategies
,
42 type WorkerChoiceStrategy
,
43 type WorkerChoiceStrategyOptions
44 } from
'./selection-strategies/selection-strategies-types'
45 import { WorkerChoiceStrategyContext
} from
'./selection-strategies/worker-choice-strategy-context'
46 import { version
} from
'./version'
47 import { WorkerNode
} from
'./worker-node'
50 * Base class that implements some shared logic for all poolifier pools.
52 * @typeParam Worker - Type of worker which manages this pool.
53 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
54 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
56 export abstract class AbstractPool
<
57 Worker
extends IWorker
,
60 > implements IPool
<Worker
, Data
, Response
> {
62 public readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>> = []
65 public readonly emitter
?: PoolEmitter
68 * The task execution response promise map.
70 * - `key`: The message id of each submitted task.
71 * - `value`: An object that contains the worker, the execution response promise resolve and reject callbacks.
73 * When we receive a message from the worker, we get a map entry with the promise resolve/reject bound to the message id.
75 protected promiseResponseMap
: Map
<string, PromiseResponseWrapper
<Response
>> =
76 new Map
<string, PromiseResponseWrapper
<Response
>>()
79 * Worker choice strategy context referencing a worker choice algorithm implementation.
81 protected workerChoiceStrategyContext
: WorkerChoiceStrategyContext
<
88 * Whether the pool is starting or not.
90 private readonly starting
: boolean
92 * The start timestamp of the pool.
94 private readonly startTimestamp
97 * Constructs a new poolifier pool.
99 * @param numberOfWorkers - Number of workers that this pool should manage.
100 * @param filePath - Path to the worker file.
101 * @param opts - Options for the pool.
104 protected readonly numberOfWorkers
: number,
105 protected readonly filePath
: string,
106 protected readonly opts
: PoolOptions
<Worker
>
108 if (!this.isMain()) {
109 throw new Error('Cannot start a pool from a worker!')
111 this.checkNumberOfWorkers(this.numberOfWorkers
)
112 this.checkFilePath(this.filePath
)
113 this.checkPoolOptions(this.opts
)
115 this.chooseWorkerNode
= this.chooseWorkerNode
.bind(this)
116 this.executeTask
= this.executeTask
.bind(this)
117 this.enqueueTask
= this.enqueueTask
.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
) {
176 throw new RangeError(
177 'Cannot instantiate a dynamic pool with a maximum pool size inferior to the minimum pool size'
179 } else if (max
=== 0) {
180 throw new RangeError(
181 'Cannot instantiate a dynamic pool with a pool size equal to zero'
183 } else if (min
=== max
) {
184 throw new RangeError(
185 'Cannot instantiate a dynamic pool with a minimum pool size equal to the maximum pool size. Use a fixed pool instead'
191 private checkPoolOptions (opts
: PoolOptions
<Worker
>): void {
192 if (isPlainObject(opts
)) {
193 this.opts
.workerChoiceStrategy
=
194 opts
.workerChoiceStrategy
?? WorkerChoiceStrategies
.ROUND_ROBIN
195 this.checkValidWorkerChoiceStrategy(this.opts
.workerChoiceStrategy
)
196 this.opts
.workerChoiceStrategyOptions
=
197 opts
.workerChoiceStrategyOptions
??
198 DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS
199 this.checkValidWorkerChoiceStrategyOptions(
200 this.opts
.workerChoiceStrategyOptions
202 this.opts
.restartWorkerOnError
= opts
.restartWorkerOnError
?? true
203 this.opts
.enableEvents
= opts
.enableEvents
?? true
204 this.opts
.enableTasksQueue
= opts
.enableTasksQueue
?? false
205 if (this.opts
.enableTasksQueue
) {
206 this.checkValidTasksQueueOptions(
207 opts
.tasksQueueOptions
as TasksQueueOptions
209 this.opts
.tasksQueueOptions
= this.buildTasksQueueOptions(
210 opts
.tasksQueueOptions
as TasksQueueOptions
214 throw new TypeError('Invalid pool options: must be a plain object')
218 private checkValidWorkerChoiceStrategy (
219 workerChoiceStrategy
: WorkerChoiceStrategy
221 if (!Object.values(WorkerChoiceStrategies
).includes(workerChoiceStrategy
)) {
223 `Invalid worker choice strategy '${workerChoiceStrategy}'`
228 private checkValidWorkerChoiceStrategyOptions (
229 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
231 if (!isPlainObject(workerChoiceStrategyOptions
)) {
233 'Invalid worker choice strategy options: must be a plain object'
237 workerChoiceStrategyOptions
.weights
!= null &&
238 Object.keys(workerChoiceStrategyOptions
.weights
).length
!== this.maxSize
241 'Invalid worker choice strategy options: must have a weight for each worker node'
245 workerChoiceStrategyOptions
.measurement
!= null &&
246 !Object.values(Measurements
).includes(
247 workerChoiceStrategyOptions
.measurement
251 `Invalid worker choice strategy options: invalid measurement '${workerChoiceStrategyOptions.measurement}'`
256 private checkValidTasksQueueOptions (
257 tasksQueueOptions
: TasksQueueOptions
259 if (tasksQueueOptions
!= null && !isPlainObject(tasksQueueOptions
)) {
260 throw new TypeError('Invalid tasks queue options: must be a plain object')
263 tasksQueueOptions
?.concurrency
!= null &&
264 !Number.isSafeInteger(tasksQueueOptions
.concurrency
)
267 'Invalid worker tasks concurrency: must be an integer'
271 tasksQueueOptions
?.concurrency
!= null &&
272 tasksQueueOptions
.concurrency
<= 0
275 `Invalid worker tasks concurrency '${tasksQueueOptions.concurrency}'`
280 private startPool (): void {
282 this.workerNodes
.reduce(
283 (accumulator
, workerNode
) =>
284 !workerNode
.info
.dynamic
? accumulator
+ 1 : accumulator
,
286 ) < this.numberOfWorkers
288 this.createAndSetupWorkerNode()
293 public get
info (): PoolInfo
{
299 strategy
: this.opts
.workerChoiceStrategy
as WorkerChoiceStrategy
,
300 minSize
: this.minSize
,
301 maxSize
: this.maxSize
,
302 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
303 .runTime
.aggregate
&&
304 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
305 .waitTime
.aggregate
&& { utilization
: round(this.utilization
) }),
306 workerNodes
: this.workerNodes
.length
,
307 idleWorkerNodes
: this.workerNodes
.reduce(
308 (accumulator
, workerNode
) =>
309 workerNode
.usage
.tasks
.executing
=== 0
314 busyWorkerNodes
: this.workerNodes
.reduce(
315 (accumulator
, workerNode
) =>
316 workerNode
.usage
.tasks
.executing
> 0 ? accumulator
+ 1 : accumulator
,
319 executedTasks
: this.workerNodes
.reduce(
320 (accumulator
, workerNode
) =>
321 accumulator
+ workerNode
.usage
.tasks
.executed
,
324 executingTasks
: this.workerNodes
.reduce(
325 (accumulator
, workerNode
) =>
326 accumulator
+ workerNode
.usage
.tasks
.executing
,
329 queuedTasks
: this.workerNodes
.reduce(
330 (accumulator
, workerNode
) =>
331 accumulator
+ workerNode
.usage
.tasks
.queued
,
334 maxQueuedTasks
: this.workerNodes
.reduce(
335 (accumulator
, workerNode
) =>
336 accumulator
+ (workerNode
.usage
.tasks
?.maxQueued
?? 0),
339 failedTasks
: this.workerNodes
.reduce(
340 (accumulator
, workerNode
) =>
341 accumulator
+ workerNode
.usage
.tasks
.failed
,
344 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
345 .runTime
.aggregate
&& {
349 ...this.workerNodes
.map(
350 workerNode
=> workerNode
.usage
.runTime
?.minimum
?? Infinity
356 ...this.workerNodes
.map(
357 workerNode
=> workerNode
.usage
.runTime
?.maximum
?? -Infinity
362 this.workerNodes
.reduce(
363 (accumulator
, workerNode
) =>
364 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
367 this.workerNodes
.reduce(
368 (accumulator
, workerNode
) =>
369 accumulator
+ (workerNode
.usage
.tasks
?.executed
?? 0),
373 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
377 this.workerNodes
.map(
378 workerNode
=> workerNode
.usage
.runTime
?.median
?? 0
385 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
386 .waitTime
.aggregate
&& {
390 ...this.workerNodes
.map(
391 workerNode
=> workerNode
.usage
.waitTime
?.minimum
?? Infinity
397 ...this.workerNodes
.map(
398 workerNode
=> workerNode
.usage
.waitTime
?.maximum
?? -Infinity
403 this.workerNodes
.reduce(
404 (accumulator
, workerNode
) =>
405 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
408 this.workerNodes
.reduce(
409 (accumulator
, workerNode
) =>
410 accumulator
+ (workerNode
.usage
.tasks
?.executed
?? 0),
414 ...(this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
415 .waitTime
.median
&& {
418 this.workerNodes
.map(
419 workerNode
=> workerNode
.usage
.waitTime
?.median
?? 0
430 * The pool readiness boolean status.
432 private get
ready (): boolean {
434 this.workerNodes
.reduce(
435 (accumulator
, workerNode
) =>
436 !workerNode
.info
.dynamic
&& workerNode
.info
.ready
445 * The approximate pool utilization.
447 * @returns The pool utilization.
449 private get
utilization (): number {
450 const poolTimeCapacity
=
451 (performance
.now() - this.startTimestamp
) * this.maxSize
452 const totalTasksRunTime
= this.workerNodes
.reduce(
453 (accumulator
, workerNode
) =>
454 accumulator
+ (workerNode
.usage
.runTime
?.aggregate
?? 0),
457 const totalTasksWaitTime
= this.workerNodes
.reduce(
458 (accumulator
, workerNode
) =>
459 accumulator
+ (workerNode
.usage
.waitTime
?.aggregate
?? 0),
462 return (totalTasksRunTime
+ totalTasksWaitTime
) / poolTimeCapacity
468 * If it is `'dynamic'`, it provides the `max` property.
470 protected abstract get
type (): PoolType
475 protected abstract get
worker (): WorkerType
478 * The pool minimum size.
480 protected abstract get
minSize (): number
483 * The pool maximum size.
485 protected abstract get
maxSize (): number
488 * Checks if the worker id sent in the received message from a worker is valid.
490 * @param message - The received message.
491 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the worker id is invalid.
493 private checkMessageWorkerId (message
: MessageValue
<Response
>): void {
495 message
.workerId
!= null &&
496 this.getWorkerNodeKeyByWorkerId(message
.workerId
) == null
499 `Worker message received from unknown worker '${message.workerId}'`
505 * Gets the given worker its worker node key.
507 * @param worker - The worker.
508 * @returns The worker node key if found in the pool worker nodes, `-1` otherwise.
510 private getWorkerNodeKey (worker
: Worker
): number {
511 return this.workerNodes
.findIndex(
512 workerNode
=> workerNode
.worker
=== worker
517 * Gets the worker node key given its worker id.
519 * @param workerId - The worker id.
520 * @returns The worker node key if the worker id is found in the pool worker nodes, `undefined` otherwise.
522 private getWorkerNodeKeyByWorkerId (workerId
: number): number | undefined {
523 for (const [workerNodeKey
, workerNode
] of this.workerNodes
.entries()) {
524 if (workerNode
.info
.id
=== workerId
) {
531 public setWorkerChoiceStrategy (
532 workerChoiceStrategy
: WorkerChoiceStrategy
,
533 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
535 this.checkValidWorkerChoiceStrategy(workerChoiceStrategy
)
536 this.opts
.workerChoiceStrategy
= workerChoiceStrategy
537 this.workerChoiceStrategyContext
.setWorkerChoiceStrategy(
538 this.opts
.workerChoiceStrategy
540 if (workerChoiceStrategyOptions
!= null) {
541 this.setWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
543 for (const [workerNodeKey
, workerNode
] of this.workerNodes
.entries()) {
544 workerNode
.resetUsage()
545 this.sendWorkerStatisticsMessageToWorker(workerNodeKey
)
550 public setWorkerChoiceStrategyOptions (
551 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
553 this.checkValidWorkerChoiceStrategyOptions(workerChoiceStrategyOptions
)
554 this.opts
.workerChoiceStrategyOptions
= workerChoiceStrategyOptions
555 this.workerChoiceStrategyContext
.setOptions(
556 this.opts
.workerChoiceStrategyOptions
561 public enableTasksQueue (
563 tasksQueueOptions
?: TasksQueueOptions
565 if (this.opts
.enableTasksQueue
=== true && !enable
) {
566 this.flushTasksQueues()
568 this.opts
.enableTasksQueue
= enable
569 this.setTasksQueueOptions(tasksQueueOptions
as TasksQueueOptions
)
573 public setTasksQueueOptions (tasksQueueOptions
: TasksQueueOptions
): void {
574 if (this.opts
.enableTasksQueue
=== true) {
575 this.checkValidTasksQueueOptions(tasksQueueOptions
)
576 this.opts
.tasksQueueOptions
=
577 this.buildTasksQueueOptions(tasksQueueOptions
)
578 } else if (this.opts
.tasksQueueOptions
!= null) {
579 delete this.opts
.tasksQueueOptions
583 private buildTasksQueueOptions (
584 tasksQueueOptions
: TasksQueueOptions
585 ): TasksQueueOptions
{
587 concurrency
: tasksQueueOptions
?.concurrency
?? 1
592 * Whether the pool is full or not.
594 * The pool filling boolean status.
596 protected get
full (): boolean {
597 return this.workerNodes
.length
>= this.maxSize
601 * Whether the pool is busy or not.
603 * The pool busyness boolean status.
605 protected abstract get
busy (): boolean
608 * Whether worker nodes are executing at least one task.
610 * @returns Worker nodes busyness boolean status.
612 protected internalBusy (): boolean {
614 this.workerNodes
.findIndex(workerNode
=> {
615 return workerNode
.usage
.tasks
.executing
=== 0
621 public async execute (data
?: Data
, name
?: string): Promise
<Response
> {
622 return await new Promise
<Response
>((resolve
, reject
) => {
623 const timestamp
= performance
.now()
624 const workerNodeKey
= this.chooseWorkerNode()
625 const task
: Task
<Data
> = {
626 name
: name
?? DEFAULT_TASK_NAME
,
627 // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
628 data
: data
?? ({} as Data
),
630 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number,
633 this.promiseResponseMap
.set(task
.id
as string, {
639 this.opts
.enableTasksQueue
=== true &&
641 this.workerNodes
[workerNodeKey
].usage
.tasks
.executing
>=
642 ((this.opts
.tasksQueueOptions
as TasksQueueOptions
)
643 .concurrency
as number))
645 this.enqueueTask(workerNodeKey
, task
)
647 this.executeTask(workerNodeKey
, task
)
649 this.checkAndEmitEvents()
654 public async destroy (): Promise
<void> {
656 this.workerNodes
.map(async (workerNode
, workerNodeKey
) => {
657 this.flushTasksQueue(workerNodeKey
)
658 // FIXME: wait for tasks to be finished
659 const workerExitPromise
= new Promise
<void>(resolve
=> {
660 workerNode
.worker
.on('exit', () => {
664 await this.destroyWorkerNode(workerNodeKey
)
665 await workerExitPromise
671 * Terminates the worker node given its worker node key.
673 * @param workerNodeKey - The worker node key.
675 protected abstract destroyWorkerNode (
676 workerNodeKey
: number
677 ): void | Promise
<void>
680 * Setup hook to execute code before worker nodes are created in the abstract constructor.
685 protected setupHook (): void {
686 // Intentionally empty
690 * Should return whether the worker is the main worker or not.
692 protected abstract isMain (): boolean
695 * Hook executed before the worker task execution.
698 * @param workerNodeKey - The worker node key.
699 * @param task - The task to execute.
701 protected beforeTaskExecutionHook (
702 workerNodeKey
: number,
705 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
706 ++workerUsage
.tasks
.executing
707 this.updateWaitTimeWorkerUsage(workerUsage
, task
)
708 const taskWorkerUsage
= this.workerNodes
[workerNodeKey
].getTaskWorkerUsage(
711 ++taskWorkerUsage
.tasks
.executing
712 this.updateWaitTimeWorkerUsage(taskWorkerUsage
, task
)
716 * Hook executed after the worker task execution.
719 * @param workerNodeKey - The worker node key.
720 * @param message - The received message.
722 protected afterTaskExecutionHook (
723 workerNodeKey
: number,
724 message
: MessageValue
<Response
>
726 const workerUsage
= this.workerNodes
[workerNodeKey
].usage
727 this.updateTaskStatisticsWorkerUsage(workerUsage
, message
)
728 this.updateRunTimeWorkerUsage(workerUsage
, message
)
729 this.updateEluWorkerUsage(workerUsage
, message
)
730 const taskWorkerUsage
= this.workerNodes
[workerNodeKey
].getTaskWorkerUsage(
731 message
.taskPerformance
?.name
?? DEFAULT_TASK_NAME
733 this.updateTaskStatisticsWorkerUsage(taskWorkerUsage
, message
)
734 this.updateRunTimeWorkerUsage(taskWorkerUsage
, message
)
735 this.updateEluWorkerUsage(taskWorkerUsage
, message
)
738 private updateTaskStatisticsWorkerUsage (
739 workerUsage
: WorkerUsage
,
740 message
: MessageValue
<Response
>
742 const workerTaskStatistics
= workerUsage
.tasks
743 --workerTaskStatistics
.executing
744 if (message
.taskError
== null) {
745 ++workerTaskStatistics
.executed
747 ++workerTaskStatistics
.failed
751 private updateRunTimeWorkerUsage (
752 workerUsage
: WorkerUsage
,
753 message
: MessageValue
<Response
>
755 updateMeasurementStatistics(
757 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().runTime
,
758 message
.taskPerformance
?.runTime
?? 0,
759 workerUsage
.tasks
.executed
763 private updateWaitTimeWorkerUsage (
764 workerUsage
: WorkerUsage
,
767 const timestamp
= performance
.now()
768 const taskWaitTime
= timestamp
- (task
.timestamp
?? timestamp
)
769 updateMeasurementStatistics(
770 workerUsage
.waitTime
,
771 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().waitTime
,
773 workerUsage
.tasks
.executed
777 private updateEluWorkerUsage (
778 workerUsage
: WorkerUsage
,
779 message
: MessageValue
<Response
>
781 const eluTaskStatisticsRequirements
: MeasurementStatisticsRequirements
=
782 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements().elu
783 updateMeasurementStatistics(
784 workerUsage
.elu
.active
,
785 eluTaskStatisticsRequirements
,
786 message
.taskPerformance
?.elu
?.active
?? 0,
787 workerUsage
.tasks
.executed
789 updateMeasurementStatistics(
790 workerUsage
.elu
.idle
,
791 eluTaskStatisticsRequirements
,
792 message
.taskPerformance
?.elu
?.idle
?? 0,
793 workerUsage
.tasks
.executed
795 if (eluTaskStatisticsRequirements
.aggregate
) {
796 if (message
.taskPerformance
?.elu
!= null) {
797 if (workerUsage
.elu
.utilization
!= null) {
798 workerUsage
.elu
.utilization
=
799 (workerUsage
.elu
.utilization
+
800 message
.taskPerformance
.elu
.utilization
) /
803 workerUsage
.elu
.utilization
= message
.taskPerformance
.elu
.utilization
810 * Chooses a worker node for the next task.
812 * The default worker choice strategy uses a round robin algorithm to distribute the tasks.
814 * @returns The chosen worker node key
816 private chooseWorkerNode (): number {
817 if (this.shallCreateDynamicWorker()) {
818 const workerNodeKey
= this.createAndSetupDynamicWorkerNode()
820 this.workerChoiceStrategyContext
.getStrategyPolicy().useDynamicWorker
825 return this.workerChoiceStrategyContext
.execute()
829 * Conditions for dynamic worker creation.
831 * @returns Whether to create a dynamic worker or not.
833 private shallCreateDynamicWorker (): boolean {
834 return this.type === PoolTypes
.dynamic
&& !this.full
&& this.internalBusy()
838 * Sends a message to worker given its worker node key.
840 * @param workerNodeKey - The worker node key.
841 * @param message - The message.
843 protected abstract sendToWorker (
844 workerNodeKey
: number,
845 message
: MessageValue
<Data
>
849 * Creates a new worker.
851 * @returns Newly created worker.
853 protected abstract createWorker (): Worker
856 * Creates a new, completely set up worker node.
858 * @returns New, completely set up worker node key.
860 protected createAndSetupWorkerNode (): number {
861 const worker
= this.createWorker()
863 worker
.on('message', this.opts
.messageHandler
?? EMPTY_FUNCTION
)
864 worker
.on('error', this.opts
.errorHandler
?? EMPTY_FUNCTION
)
865 worker
.on('error', error
=> {
866 const workerNodeKey
= this.getWorkerNodeKey(worker
)
867 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
868 workerInfo
.ready
= false
869 this.workerNodes
[workerNodeKey
].closeChannel()
870 this.emitter
?.emit(PoolEvents
.error
, error
)
871 if (this.opts
.restartWorkerOnError
=== true && !this.starting
) {
872 if (workerInfo
.dynamic
) {
873 this.createAndSetupDynamicWorkerNode()
875 this.createAndSetupWorkerNode()
878 if (this.opts
.enableTasksQueue
=== true) {
879 this.redistributeQueuedTasks(workerNodeKey
)
882 worker
.on('online', this.opts
.onlineHandler
?? EMPTY_FUNCTION
)
883 worker
.on('exit', this.opts
.exitHandler
?? EMPTY_FUNCTION
)
884 worker
.once('exit', () => {
885 this.removeWorkerNode(worker
)
888 const workerNodeKey
= this.addWorkerNode(worker
)
890 this.afterWorkerNodeSetup(workerNodeKey
)
896 * Creates a new, completely set up dynamic worker node.
898 * @returns New, completely set up dynamic worker node key.
900 protected createAndSetupDynamicWorkerNode (): number {
901 const workerNodeKey
= this.createAndSetupWorkerNode()
902 this.registerWorkerMessageListener(workerNodeKey
, message
=> {
903 const localWorkerNodeKey
= this.getWorkerNodeKeyByWorkerId(
906 const workerUsage
= this.workerNodes
[localWorkerNodeKey
].usage
908 isKillBehavior(KillBehaviors
.HARD
, message
.kill
) ||
909 (message
.kill
!= null &&
910 ((this.opts
.enableTasksQueue
=== false &&
911 workerUsage
.tasks
.executing
=== 0) ||
912 (this.opts
.enableTasksQueue
=== true &&
913 workerUsage
.tasks
.executing
=== 0 &&
914 this.tasksQueueSize(localWorkerNodeKey
) === 0)))
916 // Kill message received from the worker: no new tasks are submitted to that worker for a while ( > maxInactiveTime)
917 const destroyWorkerNodeBounded
= this.destroyWorkerNode
.bind(this)
918 if (isAsyncFunction(destroyWorkerNodeBounded
)) {
920 destroyWorkerNodeBounded
as (workerNodeKey
: number) => Promise
<void>
921 )(localWorkerNodeKey
).catch(EMPTY_FUNCTION
)
923 (destroyWorkerNodeBounded
as (workerNodeKey
: number) => void)(
929 const workerInfo
= this.getWorkerInfo(workerNodeKey
)
930 workerInfo
.dynamic
= true
931 if (this.workerChoiceStrategyContext
.getStrategyPolicy().useDynamicWorker
) {
932 workerInfo
.ready
= true
934 this.sendToWorker(workerNodeKey
, {
936 workerId
: workerInfo
.id
as number
942 * Registers a listener callback on the worker given its worker node key.
944 * @param workerNodeKey - The worker node key.
945 * @param listener - The message listener callback.
947 protected abstract registerWorkerMessageListener
<
948 Message
extends Data
| Response
950 workerNodeKey
: number,
951 listener
: (message
: MessageValue
<Message
>) => void
955 * Method hooked up after a worker node has been newly created.
958 * @param workerNodeKey - The newly created worker node key.
960 protected afterWorkerNodeSetup (workerNodeKey
: number): void {
961 // Listen to worker messages.
962 this.registerWorkerMessageListener(workerNodeKey
, this.workerListener())
963 // Send the startup message to worker.
964 this.sendStartupMessageToWorker(workerNodeKey
)
965 // Send the worker statistics message to worker.
966 this.sendWorkerStatisticsMessageToWorker(workerNodeKey
)
970 * Sends the startup message to worker given its worker node key.
972 * @param workerNodeKey - The worker node key.
974 protected abstract sendStartupMessageToWorker (workerNodeKey
: number): void
977 * Sends the worker statistics message to worker given its worker node key.
979 * @param workerNodeKey - The worker node key.
981 private sendWorkerStatisticsMessageToWorker (workerNodeKey
: number): void {
982 this.sendToWorker(workerNodeKey
, {
985 this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
987 elu
: this.workerChoiceStrategyContext
.getTaskStatisticsRequirements()
990 workerId
: this.getWorkerInfo(workerNodeKey
).id
as number
994 private redistributeQueuedTasks (workerNodeKey
: number): void {
995 while (this.tasksQueueSize(workerNodeKey
) > 0) {
996 let targetWorkerNodeKey
: number = workerNodeKey
997 let minQueuedTasks
= Infinity
998 for (const [workerNodeId
, workerNode
] of this.workerNodes
.entries()) {
999 const workerInfo
= this.getWorkerInfo(workerNodeId
)
1001 workerNodeId
!== workerNodeKey
&&
1003 workerNode
.usage
.tasks
.queued
=== 0
1005 targetWorkerNodeKey
= workerNodeId
1009 workerNodeId
!== workerNodeKey
&&
1011 workerNode
.usage
.tasks
.queued
< minQueuedTasks
1013 minQueuedTasks
= workerNode
.usage
.tasks
.queued
1014 targetWorkerNodeKey
= workerNodeId
1018 targetWorkerNodeKey
,
1019 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1025 * This method is the listener registered for each worker message.
1027 * @returns The listener function to execute when a message is received from a worker.
1029 protected workerListener (): (message
: MessageValue
<Response
>) => void {
1031 this.checkMessageWorkerId(message
)
1032 if (message
.ready
!= null) {
1033 // Worker ready response received
1034 this.handleWorkerReadyResponse(message
)
1035 } else if (message
.id
!= null) {
1036 // Task execution response received
1037 this.handleTaskExecutionResponse(message
)
1042 private handleWorkerReadyResponse (message
: MessageValue
<Response
>): void {
1044 this.getWorkerNodeKeyByWorkerId(message
.workerId
) as number
1045 ).ready
= message
.ready
as boolean
1046 if (this.emitter
!= null && this.ready
) {
1047 this.emitter
.emit(PoolEvents
.ready
, this.info
)
1051 private handleTaskExecutionResponse (message
: MessageValue
<Response
>): void {
1052 const promiseResponse
= this.promiseResponseMap
.get(message
.id
as string)
1053 if (promiseResponse
!= null) {
1054 if (message
.taskError
!= null) {
1055 this.emitter
?.emit(PoolEvents
.taskError
, message
.taskError
)
1056 promiseResponse
.reject(message
.taskError
.message
)
1058 promiseResponse
.resolve(message
.data
as Response
)
1060 const workerNodeKey
= promiseResponse
.workerNodeKey
1061 this.afterTaskExecutionHook(workerNodeKey
, message
)
1062 this.promiseResponseMap
.delete(message
.id
as string)
1064 this.opts
.enableTasksQueue
=== true &&
1065 this.tasksQueueSize(workerNodeKey
) > 0
1069 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1072 this.workerChoiceStrategyContext
.update(workerNodeKey
)
1076 private checkAndEmitEvents (): void {
1077 if (this.emitter
!= null) {
1079 this.emitter
.emit(PoolEvents
.busy
, this.info
)
1081 if (this.type === PoolTypes
.dynamic
&& this.full
) {
1082 this.emitter
.emit(PoolEvents
.full
, this.info
)
1088 * Gets the worker information given its worker node key.
1090 * @param workerNodeKey - The worker node key.
1091 * @returns The worker information.
1093 protected getWorkerInfo (workerNodeKey
: number): WorkerInfo
{
1094 return this.workerNodes
[workerNodeKey
].info
1098 * Adds the given worker in the pool worker nodes.
1100 * @param worker - The worker.
1101 * @returns The added worker node key.
1102 * @throws {@link https://nodejs.org/api/errors.html#class-error} If the added worker node is not found.
1104 private addWorkerNode (worker
: Worker
): number {
1105 const workerNode
= new WorkerNode
<Worker
, Data
>(worker
, this.worker
)
1106 // Flag the worker node as ready at pool startup.
1107 if (this.starting
) {
1108 workerNode
.info
.ready
= true
1110 this.workerNodes
.push(workerNode
)
1111 const workerNodeKey
= this.getWorkerNodeKey(worker
)
1112 if (workerNodeKey
=== -1) {
1113 throw new Error('Worker node not found')
1115 return workerNodeKey
1119 * Removes the given worker from the pool worker nodes.
1121 * @param worker - The worker.
1123 private removeWorkerNode (worker
: Worker
): void {
1124 const workerNodeKey
= this.getWorkerNodeKey(worker
)
1125 if (workerNodeKey
!== -1) {
1126 this.workerNodes
.splice(workerNodeKey
, 1)
1127 this.workerChoiceStrategyContext
.remove(workerNodeKey
)
1132 * Executes the given task on the worker given its worker node key.
1134 * @param workerNodeKey - The worker node key.
1135 * @param task - The task to execute.
1137 private executeTask (workerNodeKey
: number, task
: Task
<Data
>): void {
1138 this.beforeTaskExecutionHook(workerNodeKey
, task
)
1139 this.sendToWorker(workerNodeKey
, task
)
1142 private enqueueTask (workerNodeKey
: number, task
: Task
<Data
>): number {
1143 return this.workerNodes
[workerNodeKey
].enqueueTask(task
)
1146 private dequeueTask (workerNodeKey
: number): Task
<Data
> | undefined {
1147 return this.workerNodes
[workerNodeKey
].dequeueTask()
1150 private tasksQueueSize (workerNodeKey
: number): number {
1151 return this.workerNodes
[workerNodeKey
].tasksQueueSize()
1154 private flushTasksQueue (workerNodeKey
: number): void {
1155 while (this.tasksQueueSize(workerNodeKey
) > 0) {
1158 this.dequeueTask(workerNodeKey
) as Task
<Data
>
1161 this.workerNodes
[workerNodeKey
].clearTasksQueue()
1164 private flushTasksQueues (): void {
1165 for (const [workerNodeKey
] of this.workerNodes
.entries()) {
1166 this.flushTasksQueue(workerNodeKey
)