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 size
: number
137 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
139 * Emitter on which events can be listened to.
141 * Events that can currently be listened to:
143 * - `'full'`: Emitted when the pool is dynamic and full.
144 * - `'busy'`: Emitted when the pool is busy.
146 readonly emitter
?: PoolEmitter
148 * Executes the function specified in the worker constructor with the task data input parameter.
150 * @param data - The task input data for the specified worker function. This can only be serializable data.
151 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
152 * @returns Promise that will be fulfilled when the task is completed.
154 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
156 * Shutdowns every current worker in this pool.
158 destroy
: () => Promise
<void>
160 * Sets the worker choice strategy in this pool.
162 * @param workerChoiceStrategy - The worker choice strategy.
163 * @param workerChoiceStrategyOptions - The worker choice strategy options.
165 setWorkerChoiceStrategy
: (
166 workerChoiceStrategy
: WorkerChoiceStrategy
,
167 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
170 * Sets the worker choice strategy options in this pool.
172 * @param workerChoiceStrategyOptions - The worker choice strategy options.
174 setWorkerChoiceStrategyOptions
: (
175 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
178 * Enables/disables the worker tasks queue in this pool.
180 * @param enable - Whether to enable or disable the worker tasks queue.
181 * @param tasksQueueOptions - The worker tasks queue options.
185 tasksQueueOptions
?: TasksQueueOptions
188 * Sets the worker tasks queue options in this pool.
190 * @param tasksQueueOptions - The worker tasks queue options.
192 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void