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.
102 * @defaultValue false
104 enableTasksQueue
?: boolean
106 * Pool worker tasks queue options.
110 tasksQueueOptions
?: TasksQueueOptions
114 * Contract definition for a poolifier pool.
116 * @typeParam Worker - Type of worker which manages this pool.
117 * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
118 * @typeParam Response - Type of execution response. This can only be serializable data.
120 export interface IPool
<
121 Worker
extends IWorker
,
128 * If it is `'dynamic'`, it provides the `max` property.
130 readonly type: PoolType
134 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
136 * Emitter on which events can be listened to.
138 * Events that can currently be listened to:
140 * - `'full'`: Emitted when the pool is dynamic and full.
141 * - `'busy'`: Emitted when the pool is busy.
143 readonly emitter
?: PoolEmitter
145 * Finds a free worker node key based on the number of tasks the worker has applied.
147 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
149 * If no free worker is found, `-1` is returned.
151 * @returns A worker node key if there is one, `-1` otherwise.
153 findFreeWorkerNodeKey
: () => number
155 * Performs the task specified in the constructor with the data parameter.
157 * @param data - The input for the specified task. This can only be serializable data.
158 * @returns Promise that will be resolved when the task is successfully completed.
160 execute
: (data
: Data
) => Promise
<Response
>
162 * Shutdowns every current worker in this pool.
164 destroy
: () => Promise
<void>
166 * Sets the worker choice strategy in this pool.
168 * @param workerChoiceStrategy - The worker choice strategy.
170 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void