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({
49 export type PoolEvent
= keyof
typeof PoolEvents
52 * Worker tasks queue options.
54 export interface TasksQueueOptions
{
56 * Maximum number of tasks that can be executed concurrently on a worker.
64 * Options for a poolifier pool.
66 * @typeParam Worker - Type of worker.
68 export interface PoolOptions
<Worker
extends IWorker
> {
70 * A function that will listen for message event on each worker.
72 messageHandler
?: MessageHandler
<Worker
>
74 * A function that will listen for error event on each worker.
76 errorHandler
?: ErrorHandler
<Worker
>
78 * A function that will listen for online event on each worker.
80 onlineHandler
?: OnlineHandler
<Worker
>
82 * A function that will listen for exit event on each worker.
84 exitHandler
?: ExitHandler
<Worker
>
86 * The worker choice strategy to use in this pool.
88 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
90 workerChoiceStrategy
?: WorkerChoiceStrategy
92 * The worker choice strategy options.
94 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
96 * Restart worker on error.
98 restartWorkerOnError
?: boolean
100 * Pool events emission.
104 enableEvents
?: boolean
106 * Pool worker tasks queue.
108 * @defaultValue false
110 enableTasksQueue
?: boolean
112 * Pool worker tasks queue options.
114 tasksQueueOptions
?: TasksQueueOptions
118 * Contract definition for a poolifier pool.
120 * @typeParam Worker - Type of worker which manages this pool.
121 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
122 * @typeParam Response - Type of execution response. This can only be serializable data.
124 export interface IPool
<
125 Worker
extends IWorker
,
132 * If it is `'dynamic'`, it provides the `max` property.
134 readonly type: PoolType
138 readonly size
: number
142 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
144 * Emitter on which events can be listened to.
146 * Events that can currently be listened to:
148 * - `'full'`: Emitted when the pool is dynamic and full.
149 * - `'busy'`: Emitted when the pool is busy.
150 * - `'error'`: Emitted when an error occurs.
152 readonly emitter
?: PoolEmitter
154 * Executes the specified function in the worker constructor with the task data input parameter.
156 * @param data - The task input data for the specified worker function. This can only be serializable data.
157 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
158 * @returns Promise that will be fulfilled when the task is completed.
160 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
162 * Shutdowns every current worker in this pool.
164 destroy
: () => Promise
<void>
166 * Sets the worker choice strategy in this pool.
168 * @param workerChoiceStrategy - The worker choice strategy.
169 * @param workerChoiceStrategyOptions - The worker choice strategy options.
171 setWorkerChoiceStrategy
: (
172 workerChoiceStrategy
: WorkerChoiceStrategy
,
173 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
176 * Sets the worker choice strategy options in this pool.
178 * @param workerChoiceStrategyOptions - The worker choice strategy options.
180 setWorkerChoiceStrategyOptions
: (
181 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
184 * Enables/disables the worker tasks queue in this pool.
186 * @param enable - Whether to enable or disable the worker tasks queue.
187 * @param tasksQueueOptions - The worker tasks queue options.
191 tasksQueueOptions
?: TasksQueueOptions
194 * Sets the worker tasks queue options in this pool.
196 * @param tasksQueueOptions - The worker tasks queue options.
198 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void