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