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
{
75 /** Pool utilization ratio. */
77 /** Pool total worker nodes */
79 /** Pool idle worker nodes */
80 idleWorkerNodes
: number
81 /** Pool busy worker nodes */
82 busyWorkerNodes
: number
84 executingTasks
: number
86 maxQueuedTasks
: number
91 * Worker tasks queue options.
93 export interface TasksQueueOptions
{
95 * Maximum number of tasks that can be executed concurrently on a worker.
103 * Options for a poolifier pool.
105 * @typeParam Worker - Type of worker.
107 export interface PoolOptions
<Worker
extends IWorker
> {
109 * A function that will listen for message event on each worker.
111 messageHandler
?: MessageHandler
<Worker
>
113 * A function that will listen for error event on each worker.
115 errorHandler
?: ErrorHandler
<Worker
>
117 * A function that will listen for online event on each worker.
119 onlineHandler
?: OnlineHandler
<Worker
>
121 * A function that will listen for exit event on each worker.
123 exitHandler
?: ExitHandler
<Worker
>
125 * The worker choice strategy to use in this pool.
127 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
129 workerChoiceStrategy
?: WorkerChoiceStrategy
131 * The worker choice strategy options.
133 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
135 * Restart worker on error.
137 restartWorkerOnError
?: boolean
139 * Pool events emission.
143 enableEvents
?: boolean
145 * Pool worker tasks queue.
147 * @defaultValue false
149 enableTasksQueue
?: boolean
151 * Pool worker tasks queue options.
153 tasksQueueOptions
?: TasksQueueOptions
157 * Contract definition for a poolifier pool.
159 * @typeParam Worker - Type of worker which manages this pool.
160 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
161 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
163 export interface IPool
<
164 Worker
extends IWorker
,
171 readonly info
: PoolInfo
175 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
177 * Emitter on which events can be listened to.
179 * Events that can currently be listened to:
181 * - `'full'`: Emitted when the pool is dynamic and full.
182 * - `'busy'`: Emitted when the pool is busy.
183 * - `'error'`: Emitted when an uncaught error occurs.
184 * - `'taskError'`: Emitted when an error occurs while executing a task.
186 readonly emitter
?: PoolEmitter
188 * Executes the specified function in the worker constructor with the task data input parameter.
190 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
191 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
192 * @returns Promise that will be fulfilled when the task is completed.
194 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
196 * Terminates every current worker in this pool.
198 destroy
: () => Promise
<void>
200 * Sets the worker choice strategy in this pool.
202 * @param workerChoiceStrategy - The worker choice strategy.
203 * @param workerChoiceStrategyOptions - The worker choice strategy options.
205 setWorkerChoiceStrategy
: (
206 workerChoiceStrategy
: WorkerChoiceStrategy
,
207 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
210 * Sets the worker choice strategy options in this pool.
212 * @param workerChoiceStrategyOptions - The worker choice strategy options.
214 setWorkerChoiceStrategyOptions
: (
215 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
218 * Enables/disables the worker tasks queue in this pool.
220 * @param enable - Whether to enable or disable the worker tasks queue.
221 * @param tasksQueueOptions - The worker tasks queue options.
225 tasksQueueOptions
?: TasksQueueOptions
228 * Sets the worker tasks queue options in this pool.
230 * @param tasksQueueOptions - The worker tasks queue options.
232 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void