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 idleWorkerNodes
: number
77 busyWorkerNodes
: number
79 executingTasks
: number
81 maxQueuedTasks
: number
86 * Worker tasks queue options.
88 export interface TasksQueueOptions
{
90 * Maximum number of tasks that can be executed concurrently on a worker.
98 * Options for a poolifier pool.
100 * @typeParam Worker - Type of worker.
102 export interface PoolOptions
<Worker
extends IWorker
> {
104 * A function that will listen for message event on each worker.
106 messageHandler
?: MessageHandler
<Worker
>
108 * A function that will listen for error event on each worker.
110 errorHandler
?: ErrorHandler
<Worker
>
112 * A function that will listen for online event on each worker.
114 onlineHandler
?: OnlineHandler
<Worker
>
116 * A function that will listen for exit event on each worker.
118 exitHandler
?: ExitHandler
<Worker
>
120 * The worker choice strategy to use in this pool.
122 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
124 workerChoiceStrategy
?: WorkerChoiceStrategy
126 * The worker choice strategy options.
128 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
130 * Restart worker on error.
132 restartWorkerOnError
?: boolean
134 * Pool events emission.
138 enableEvents
?: boolean
140 * Pool worker tasks queue.
142 * @defaultValue false
144 enableTasksQueue
?: boolean
146 * Pool worker tasks queue options.
148 tasksQueueOptions
?: TasksQueueOptions
152 * Contract definition for a poolifier pool.
154 * @typeParam Worker - Type of worker which manages this pool.
155 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
156 * @typeParam Response - Type of execution response. This can only be serializable data.
158 export interface IPool
<
159 Worker
extends IWorker
,
166 readonly info
: PoolInfo
170 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
172 * Emitter on which events can be listened to.
174 * Events that can currently be listened to:
176 * - `'full'`: Emitted when the pool is dynamic and full.
177 * - `'busy'`: Emitted when the pool is busy.
178 * - `'error'`: Emitted when an uncaught error occurs.
179 * - `'taskError'`: Emitted when an error occurs while executing a task.
181 readonly emitter
?: PoolEmitter
183 * Executes the specified function in the worker constructor with the task data input parameter.
185 * @param data - The task input data for the specified worker function. This can only be serializable data.
186 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
187 * @returns Promise that will be fulfilled when the task is completed.
189 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
191 * Terminate every current worker in this pool.
193 destroy
: () => Promise
<void>
195 * Sets the worker choice strategy in this pool.
197 * @param workerChoiceStrategy - The worker choice strategy.
198 * @param workerChoiceStrategyOptions - The worker choice strategy options.
200 setWorkerChoiceStrategy
: (
201 workerChoiceStrategy
: WorkerChoiceStrategy
,
202 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
205 * Sets the worker choice strategy options in this pool.
207 * @param workerChoiceStrategyOptions - The worker choice strategy options.
209 setWorkerChoiceStrategyOptions
: (
210 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
213 * Enables/disables the worker tasks queue in this pool.
215 * @param enable - Whether to enable or disable the worker tasks queue.
216 * @param tasksQueueOptions - The worker tasks queue options.
220 tasksQueueOptions
?: TasksQueueOptions
223 * Sets the worker tasks queue options in this pool.
225 * @param tasksQueueOptions - The worker tasks queue options.
227 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void