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 workerChoiceStrategy
?: WorkerChoiceStrategy
89 * The worker choice strategy options.
91 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
93 * Pool events emission.
97 enableEvents
?: boolean
99 * Pool worker tasks queue.
101 * @defaultValue false
103 enableTasksQueue
?: boolean
105 * Pool worker tasks queue options.
107 tasksQueueOptions
?: TasksQueueOptions
111 * Contract definition for a poolifier pool.
113 * @typeParam Worker - Type of worker which manages this pool.
114 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
115 * @typeParam Response - Type of execution response. This can only be serializable data.
117 export interface IPool
<
118 Worker
extends IWorker
,
125 * If it is `'dynamic'`, it provides the `max` property.
127 readonly type: PoolType
131 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
133 * Emitter on which events can be listened to.
135 * Events that can currently be listened to:
137 * - `'full'`: Emitted when the pool is dynamic and full.
138 * - `'busy'`: Emitted when the pool is busy.
140 readonly emitter
?: PoolEmitter
142 * Finds a free worker node key based on the number of tasks the worker has applied.
144 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
146 * If no free worker is found, `-1` is returned.
148 * @returns A worker node key if there is one, `-1` otherwise.
150 findFreeWorkerNodeKey
: () => number
152 * Executes the function specified in the constructor with the task data parameter.
154 * @param data - The input for the specified task. This can only be serializable data.
155 * @returns Promise that will be resolved when the task is successfully completed.
157 execute
: (data
: Data
) => Promise
<Response
>
159 * Shutdowns every current worker in this pool.
161 destroy
: () => Promise
<void>
163 * Sets the worker choice strategy in this pool.
165 * @param workerChoiceStrategy - The worker choice strategy.
166 * @param workerChoiceStrategyOptions - The worker choice strategy options.
168 setWorkerChoiceStrategy
: (
169 workerChoiceStrategy
: WorkerChoiceStrategy
,
170 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
173 * Sets the worker choice strategy options in this pool.
175 * @param workerChoiceStrategyOptions - The worker choice strategy options.
177 setWorkerChoiceStrategyOptions
: (
178 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
181 * Enables/disables the worker tasks queue in this pool.
183 * @param enable - Whether to enable or disable the worker tasks queue.
184 * @param tasksQueueOptions - The worker tasks queue options.
188 tasksQueueOptions
?: TasksQueueOptions
191 * Sets the worker tasks queue options in this pool.
193 * @param tasksQueueOptions - The worker tasks queue options.
195 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void