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
{
76 /** Pool utilization ratio. */
78 /** Pool total worker nodes */
80 /** Pool idle worker nodes */
81 idleWorkerNodes
: number
82 /** Pool busy worker nodes */
83 busyWorkerNodes
: number
85 executingTasks
: number
87 maxQueuedTasks
: number
92 * Worker tasks queue options.
94 export interface TasksQueueOptions
{
96 * Maximum number of tasks that can be executed concurrently on a worker.
100 readonly concurrency
?: number
104 * Options for a poolifier pool.
106 * @typeParam Worker - Type of worker.
108 export interface PoolOptions
<Worker
extends IWorker
> {
110 * A function that will listen for message event on each worker.
112 messageHandler
?: MessageHandler
<Worker
>
114 * A function that will listen for error event on each worker.
116 errorHandler
?: ErrorHandler
<Worker
>
118 * A function that will listen for online event on each worker.
120 onlineHandler
?: OnlineHandler
<Worker
>
122 * A function that will listen for exit event on each worker.
124 exitHandler
?: ExitHandler
<Worker
>
126 * The worker choice strategy to use in this pool.
128 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
130 workerChoiceStrategy
?: WorkerChoiceStrategy
132 * The worker choice strategy options.
134 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
136 * Restart worker on error.
138 restartWorkerOnError
?: boolean
140 * Pool events emission.
144 enableEvents
?: boolean
146 * Pool worker tasks queue.
148 * @defaultValue false
150 enableTasksQueue
?: boolean
152 * Pool worker tasks queue options.
154 tasksQueueOptions
?: TasksQueueOptions
158 * Contract definition for a poolifier pool.
160 * @typeParam Worker - Type of worker which manages this pool.
161 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
162 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
164 export interface IPool
<
165 Worker
extends IWorker
,
172 readonly info
: PoolInfo
176 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
178 * Emitter on which events can be listened to.
180 * Events that can currently be listened to:
182 * - `'full'`: Emitted when the pool is dynamic and full.
183 * - `'busy'`: Emitted when the pool is busy.
184 * - `'error'`: Emitted when an uncaught error occurs.
185 * - `'taskError'`: Emitted when an error occurs while executing a task.
187 readonly emitter
?: PoolEmitter
189 * Executes the specified function in the worker constructor with the task data input parameter.
191 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
192 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
193 * @returns Promise that will be fulfilled when the task is completed.
195 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
197 * Terminates every current worker in this pool.
199 destroy
: () => Promise
<void>
201 * Sets the worker choice strategy in this pool.
203 * @param workerChoiceStrategy - The worker choice strategy.
204 * @param workerChoiceStrategyOptions - The worker choice strategy options.
206 setWorkerChoiceStrategy
: (
207 workerChoiceStrategy
: WorkerChoiceStrategy
,
208 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
211 * Sets the worker choice strategy options in this pool.
213 * @param workerChoiceStrategyOptions - The worker choice strategy options.
215 setWorkerChoiceStrategyOptions
: (
216 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
219 * Enables/disables the worker tasks queue in this pool.
221 * @param enable - Whether to enable or disable the worker tasks queue.
222 * @param tasksQueueOptions - The worker tasks queue options.
226 tasksQueueOptions
?: TasksQueueOptions
229 * Sets the worker tasks queue options in this pool.
231 * @param tasksQueueOptions - The worker tasks queue options.
233 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void