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 export interface PoolOptions
<Worker
extends IWorker
> {
66 * A function that will listen for message event on each worker.
68 messageHandler
?: MessageHandler
<Worker
>
70 * A function that will listen for error event on each worker.
72 errorHandler
?: ErrorHandler
<Worker
>
74 * A function that will listen for online event on each worker.
76 onlineHandler
?: OnlineHandler
<Worker
>
78 * A function that will listen for exit event on each worker.
80 exitHandler
?: ExitHandler
<Worker
>
82 * The worker choice strategy to use in this pool.
84 workerChoiceStrategy
?: WorkerChoiceStrategy
86 * The worker choice strategy options.
88 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
90 * Pool events emission.
94 enableEvents
?: boolean
96 * Pool worker tasks queue.
101 enableTasksQueue
?: boolean
103 * 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 response of execution. 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 * Whether the pool is full or not.
144 * The pool filling boolean status.
146 readonly full
: boolean
148 * Whether the pool is busy or not.
150 * The pool busyness boolean status.
152 readonly busy
: boolean
154 * Finds a free worker node key based on the number of tasks the worker has applied.
156 * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned.
158 * If no free worker is found, `-1` is returned.
160 * @returns A worker node key if there is one, `-1` otherwise.
162 findFreeWorkerNodeKey
: () => number
164 * Performs the task specified in the constructor with the data parameter.
166 * @param data - The input for the specified task. This can only be serializable data.
167 * @returns Promise that will be resolved when the task is successfully completed.
169 execute
: (data
: Data
) => Promise
<Response
>
171 * Shutdowns every current worker in this pool.
173 destroy
: () => Promise
<void>
175 * Sets the worker choice strategy in this pool.
177 * @param workerChoiceStrategy - The worker choice strategy.
179 setWorkerChoiceStrategy
: (workerChoiceStrategy
: WorkerChoiceStrategy
) => void