1 import EventEmitter 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 EventEmitter
{}
38 * Enumeration of pool events.
40 export const PoolEvents
= Object.freeze({
48 export type PoolEvent
= keyof
typeof PoolEvents
51 * Worker tasks queue options.
53 export interface TasksQueueOptions
{
55 * Maximum number of tasks that can be executed concurrently on a worker.
63 * Options for a poolifier pool.
65 * @typeParam Worker - Type of worker.
67 export interface PoolOptions
<Worker
extends IWorker
> {
69 * A function that will listen for message event on each worker.
71 messageHandler
?: MessageHandler
<Worker
>
73 * A function that will listen for error event on each worker.
75 errorHandler
?: ErrorHandler
<Worker
>
77 * A function that will listen for online event on each worker.
79 onlineHandler
?: OnlineHandler
<Worker
>
81 * A function that will listen for exit event on each worker.
83 exitHandler
?: ExitHandler
<Worker
>
85 * The worker choice strategy to use in this pool.
87 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
89 workerChoiceStrategy
?: WorkerChoiceStrategy
91 * The worker choice strategy options.
93 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
95 * Pool events emission.
99 enableEvents
?: boolean
101 * Pool worker tasks queue.
103 * @defaultValue false
105 enableTasksQueue
?: boolean
107 * Pool worker tasks queue options.
109 tasksQueueOptions
?: TasksQueueOptions
113 * Contract definition for a poolifier pool.
115 * @typeParam Worker - Type of worker which manages this pool.
116 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
117 * @typeParam Response - Type of execution response. This can only be serializable data.
119 export interface IPool
<
120 Worker
extends IWorker
,
127 * If it is `'dynamic'`, it provides the `max` property.
129 readonly type: PoolType
133 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
135 * Emitter on which events can be listened to.
137 * Events that can currently be listened to:
139 * - `'full'`: Emitted when the pool is dynamic and full.
140 * - `'busy'`: Emitted when the pool is busy.
142 readonly emitter
?: PoolEmitter
144 * Executes the function specified in the worker constructor with the task data input parameter.
146 * @param data - The task input data for the specified worker function. This can only be serializable data.
147 * @returns Promise that will be fulfilled when the task is completed.
149 execute
: (data
?: Data
) => Promise
<Response
>
151 * Shutdowns every current worker in this pool.
153 destroy
: () => Promise
<void>
155 * Sets the worker choice strategy in this pool.
157 * @param workerChoiceStrategy - The worker choice strategy.
158 * @param workerChoiceStrategyOptions - The worker choice strategy options.
160 setWorkerChoiceStrategy
: (
161 workerChoiceStrategy
: WorkerChoiceStrategy
,
162 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
165 * Sets the worker choice strategy options in this pool.
167 * @param workerChoiceStrategyOptions - The worker choice strategy options.
169 setWorkerChoiceStrategyOptions
: (
170 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
173 * Enables/disables the worker tasks queue in this pool.
175 * @param enable - Whether to enable or disable the worker tasks queue.
176 * @param tasksQueueOptions - The worker tasks queue options.
180 tasksQueueOptions
?: TasksQueueOptions
183 * Sets the worker tasks queue options in this pool.
185 * @param tasksQueueOptions - The worker tasks queue options.
187 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void