1 import EventEmitterAsyncResource from
'node:events'
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies/selection-strategies-types'
21 export enum PoolType
{
33 * Pool events emitter.
35 export class PoolEmitter
extends EventEmitterAsyncResource
{}
38 * Enumeration of pool events.
40 export const PoolEvents
= Object.freeze({
44 taskError
: 'taskError'
50 export type PoolEvent
= keyof
typeof PoolEvents
53 * Worker tasks queue options.
55 export interface TasksQueueOptions
{
57 * Maximum number of tasks that can be executed concurrently on a worker.
65 * Options for a poolifier pool.
67 * @typeParam Worker - Type of worker.
69 export interface PoolOptions
<Worker
extends IWorker
> {
71 * A function that will listen for message event on each worker.
73 messageHandler
?: MessageHandler
<Worker
>
75 * A function that will listen for error event on each worker.
77 errorHandler
?: ErrorHandler
<Worker
>
79 * A function that will listen for online event on each worker.
81 onlineHandler
?: OnlineHandler
<Worker
>
83 * A function that will listen for exit event on each worker.
85 exitHandler
?: ExitHandler
<Worker
>
87 * The worker choice strategy to use in this pool.
89 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
91 workerChoiceStrategy
?: WorkerChoiceStrategy
93 * The worker choice strategy options.
95 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
97 * Restart worker on error.
99 restartWorkerOnError
?: boolean
101 * Pool events emission.
105 enableEvents
?: boolean
107 * Pool worker tasks queue.
109 * @defaultValue false
111 enableTasksQueue
?: boolean
113 * Pool worker tasks queue options.
115 tasksQueueOptions
?: TasksQueueOptions
119 * Contract definition for a poolifier pool.
121 * @typeParam Worker - Type of worker which manages this pool.
122 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
123 * @typeParam Response - Type of execution response. This can only be serializable data.
125 export interface IPool
<
126 Worker
extends IWorker
,
133 * If it is `'dynamic'`, it provides the `max` property.
135 readonly type: PoolType
139 readonly size
: number
143 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
145 * Emitter on which events can be listened to.
147 * Events that can currently be listened to:
149 * - `'full'`: Emitted when the pool is dynamic and full.
150 * - `'busy'`: Emitted when the pool is busy.
151 * - `'error'`: Emitted when an uncaught error occurs.
152 * - `'taskError'`: Emitted when an error occurs while executing a task.
154 readonly emitter
?: PoolEmitter
156 * Executes the specified function in the worker constructor with the task data input parameter.
158 * @param data - The task input data for the specified worker function. This can only be serializable data.
159 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
160 * @returns Promise that will be fulfilled when the task is completed.
162 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
164 * Shutdowns every current worker in this pool.
166 destroy
: () => Promise
<void>
168 * Sets the worker choice strategy in this pool.
170 * @param workerChoiceStrategy - The worker choice strategy.
171 * @param workerChoiceStrategyOptions - The worker choice strategy options.
173 setWorkerChoiceStrategy
: (
174 workerChoiceStrategy
: WorkerChoiceStrategy
,
175 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
178 * Sets the worker choice strategy options in this pool.
180 * @param workerChoiceStrategyOptions - The worker choice strategy options.
182 setWorkerChoiceStrategyOptions
: (
183 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
186 * Enables/disables the worker tasks queue in this pool.
188 * @param enable - Whether to enable or disable the worker tasks queue.
189 * @param tasksQueueOptions - The worker tasks queue options.
193 tasksQueueOptions
?: TasksQueueOptions
196 * Sets the worker tasks queue options in this pool.
198 * @param tasksQueueOptions - The worker tasks queue options.
200 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void