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 * Pool events emitter.
37 export class PoolEmitter
extends EventEmitterAsyncResource
{}
40 * Enumeration of pool events.
42 export const PoolEvents
= Object.freeze({
46 taskError
: 'taskError'
52 export type PoolEvent
= keyof
typeof PoolEvents
57 export interface PoolInfo
{
62 idleWorkerNodes
: number
63 busyWorkerNodes
: number
66 maxQueuedTasks
: number
70 * Worker tasks queue options.
72 export interface TasksQueueOptions
{
74 * Maximum number of tasks that can be executed concurrently on a worker.
82 * Options for a poolifier pool.
84 * @typeParam Worker - Type of worker.
86 export interface PoolOptions
<Worker
extends IWorker
> {
88 * A function that will listen for message event on each worker.
90 messageHandler
?: MessageHandler
<Worker
>
92 * A function that will listen for error event on each worker.
94 errorHandler
?: ErrorHandler
<Worker
>
96 * A function that will listen for online event on each worker.
98 onlineHandler
?: OnlineHandler
<Worker
>
100 * A function that will listen for exit event on each worker.
102 exitHandler
?: ExitHandler
<Worker
>
104 * The worker choice strategy to use in this pool.
106 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
108 workerChoiceStrategy
?: WorkerChoiceStrategy
110 * The worker choice strategy options.
112 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
114 * Restart worker on error.
116 restartWorkerOnError
?: boolean
118 * Pool events emission.
122 enableEvents
?: boolean
124 * Pool worker tasks queue.
126 * @defaultValue false
128 enableTasksQueue
?: boolean
130 * Pool worker tasks queue options.
132 tasksQueueOptions
?: TasksQueueOptions
136 * Contract definition for a poolifier pool.
138 * @typeParam Worker - Type of worker which manages this pool.
139 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
140 * @typeParam Response - Type of execution response. This can only be serializable data.
142 export interface IPool
<
143 Worker
extends IWorker
,
150 * If it is `'dynamic'`, it provides the `max` property.
152 readonly type: PoolType
156 readonly info
: PoolInfo
160 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
162 * Emitter on which events can be listened to.
164 * Events that can currently be listened to:
166 * - `'full'`: Emitted when the pool is dynamic and full.
167 * - `'busy'`: Emitted when the pool is busy.
168 * - `'error'`: Emitted when an uncaught error occurs.
169 * - `'taskError'`: Emitted when an error occurs while executing a task.
171 readonly emitter
?: PoolEmitter
173 * Executes the specified function in the worker constructor with the task data input parameter.
175 * @param data - The task input data for the specified worker function. This can only be serializable data.
176 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
177 * @returns Promise that will be fulfilled when the task is completed.
179 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
181 * Shutdowns every current worker in this pool.
183 destroy
: () => Promise
<void>
185 * Sets the worker choice strategy in this pool.
187 * @param workerChoiceStrategy - The worker choice strategy.
188 * @param workerChoiceStrategyOptions - The worker choice strategy options.
190 setWorkerChoiceStrategy
: (
191 workerChoiceStrategy
: WorkerChoiceStrategy
,
192 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
195 * Sets the worker choice strategy options in this pool.
197 * @param workerChoiceStrategyOptions - The worker choice strategy options.
199 setWorkerChoiceStrategyOptions
: (
200 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
203 * Enables/disables the worker tasks queue in this pool.
205 * @param enable - Whether to enable or disable the worker tasks queue.
206 * @param tasksQueueOptions - The worker tasks queue options.
210 tasksQueueOptions
?: TasksQueueOptions
213 * Sets the worker tasks queue options in this pool.
215 * @param tasksQueueOptions - The worker tasks queue options.
217 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void