1 import EventEmitterAsyncResource from
'node:events'
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies/selection-strategies-types'
16 * Enumeration of pool types.
18 export const PoolTypes
= Object.freeze({
32 export type PoolType
= keyof
typeof PoolTypes
35 * Enumeration of worker types.
37 export const WorkerTypes
= Object.freeze({
45 export type WorkerType
= keyof
typeof WorkerTypes
48 * Pool events emitter.
50 export class PoolEmitter
extends EventEmitterAsyncResource
{}
53 * Enumeration of pool events.
55 export const PoolEvents
= Object.freeze({
59 taskError
: 'taskError'
65 export type PoolEvent
= keyof
typeof PoolEvents
70 export interface PoolInfo
{
77 idleWorkerNodes
: number
78 busyWorkerNodes
: number
80 executingTasks
: number
82 maxQueuedTasks
: number
87 * Worker tasks queue options.
89 export interface TasksQueueOptions
{
91 * Maximum number of tasks that can be executed concurrently on a worker.
99 * Options for a poolifier pool.
101 * @typeParam Worker - Type of worker.
103 export interface PoolOptions
<Worker
extends IWorker
> {
105 * A function that will listen for message event on each worker.
107 messageHandler
?: MessageHandler
<Worker
>
109 * A function that will listen for error event on each worker.
111 errorHandler
?: ErrorHandler
<Worker
>
113 * A function that will listen for online event on each worker.
115 onlineHandler
?: OnlineHandler
<Worker
>
117 * A function that will listen for exit event on each worker.
119 exitHandler
?: ExitHandler
<Worker
>
121 * The worker choice strategy to use in this pool.
123 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
125 workerChoiceStrategy
?: WorkerChoiceStrategy
127 * The worker choice strategy options.
129 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
131 * Restart worker on error.
133 restartWorkerOnError
?: boolean
135 * Pool events emission.
139 enableEvents
?: boolean
141 * Pool worker tasks queue.
143 * @defaultValue false
145 enableTasksQueue
?: boolean
147 * Pool worker tasks queue options.
149 tasksQueueOptions
?: TasksQueueOptions
153 * Contract definition for a poolifier pool.
155 * @typeParam Worker - Type of worker which manages this pool.
156 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
157 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
159 export interface IPool
<
160 Worker
extends IWorker
,
167 readonly info
: PoolInfo
171 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
173 * Emitter on which events can be listened to.
175 * Events that can currently be listened to:
177 * - `'full'`: Emitted when the pool is dynamic and full.
178 * - `'busy'`: Emitted when the pool is busy.
179 * - `'error'`: Emitted when an uncaught error occurs.
180 * - `'taskError'`: Emitted when an error occurs while executing a task.
182 readonly emitter
?: PoolEmitter
184 * Executes the specified function in the worker constructor with the task data input parameter.
186 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
187 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
188 * @returns Promise that will be fulfilled when the task is completed.
190 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
192 * Terminate every current worker in this pool.
194 destroy
: () => Promise
<void>
196 * Sets the worker choice strategy in this pool.
198 * @param workerChoiceStrategy - The worker choice strategy.
199 * @param workerChoiceStrategyOptions - The worker choice strategy options.
201 setWorkerChoiceStrategy
: (
202 workerChoiceStrategy
: WorkerChoiceStrategy
,
203 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
206 * Sets the worker choice strategy options in this pool.
208 * @param workerChoiceStrategyOptions - The worker choice strategy options.
210 setWorkerChoiceStrategyOptions
: (
211 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
214 * Enables/disables the worker tasks queue in this pool.
216 * @param enable - Whether to enable or disable the worker tasks queue.
217 * @param tasksQueueOptions - The worker tasks queue options.
221 tasksQueueOptions
?: TasksQueueOptions
224 * Sets the worker tasks queue options in this pool.
226 * @param tasksQueueOptions - The worker tasks queue options.
228 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void