1 import EventEmitter from
'node:events'
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies/selection-strategies-types'
20 export enum PoolType
{
32 * Pool events emitter.
34 export class PoolEmitter
extends EventEmitter
{}
37 * Enumeration of pool events.
39 export const PoolEvents
= Object.freeze({
47 export type PoolEvent
= keyof
typeof PoolEvents
50 * Worker tasks queue options.
52 export interface TasksQueueOptions
{
54 * Maximum number of tasks that can be executed concurrently on a worker.
62 * Options for a poolifier pool.
64 * @typeParam Worker - The worker type.
66 export interface PoolOptions
<Worker
extends IWorker
> {
68 * A function that will listen for message event on each worker.
70 messageHandler
?: MessageHandler
<Worker
>
72 * A function that will listen for error event on each worker.
74 errorHandler
?: ErrorHandler
<Worker
>
76 * A function that will listen for online event on each worker.
78 onlineHandler
?: OnlineHandler
<Worker
>
80 * A function that will listen for exit event on each worker.
82 exitHandler
?: ExitHandler
<Worker
>
84 * The worker choice strategy to use in this pool.
86 workerChoiceStrategy
?: WorkerChoiceStrategy
88 * The worker choice strategy options.
90 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
92 * Pool events emission.
96 enableEvents
?: boolean
98 * Pool worker tasks queue.
101 * @defaultValue false
103 enableTasksQueue
?: boolean
105 * 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 response of execution. 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 * Finds a free worker node key based on the number of tasks the worker has applied.
146 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
148 * If no free worker is found, `-1` is returned.
150 * @returns A worker node key if there is one, `-1` otherwise.
152 findFreeWorkerNodeKey
: () => number
154 * Performs the task specified in the constructor with the data parameter.
156 * @param data - The input for the specified task. This can only be serializable data.
157 * @returns Promise that will be resolved when the task is successfully completed.
159 execute
: (data
: Data
) => Promise
<Response
>
161 * Shutdowns every current worker in this pool.
163 destroy
: () => Promise
<void>
165 * Sets the worker choice strategy in this pool.
167 * @param workerChoiceStrategy - The worker choice strategy.
169 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void