1 import { EventEmitter
} from
'node:events'
13 WorkerChoiceStrategyOptions
14 } from
'./selection-strategies/selection-strategies-types'
17 * Enumeration of pool types.
19 export const PoolTypes
= Object.freeze({
33 export type PoolType
= keyof
typeof PoolTypes
36 * Pool events emitter.
38 export class PoolEmitter
extends EventEmitter
{}
41 * Enumeration of pool events.
43 export const PoolEvents
= Object.freeze({
48 taskError
: 'taskError'
54 export type PoolEvent
= keyof
typeof PoolEvents
59 export interface PoolInfo
{
60 readonly version
: string
61 readonly type: PoolType
62 readonly worker
: WorkerType
63 readonly ready
: boolean
64 readonly strategy
: WorkerChoiceStrategy
65 readonly minSize
: number
66 readonly maxSize
: number
67 /** Pool utilization ratio. */
68 readonly utilization
?: number
69 /** Pool total worker nodes. */
70 readonly workerNodes
: number
71 /** Pool idle worker nodes. */
72 readonly idleWorkerNodes
: number
73 /** Pool busy worker nodes. */
74 readonly busyWorkerNodes
: number
75 readonly executedTasks
: number
76 readonly executingTasks
: number
77 readonly queuedTasks
: number
78 readonly maxQueuedTasks
: number
79 readonly failedTasks
: number
81 readonly minimum
: number
82 readonly maximum
: number
83 readonly average
: number
84 readonly median
?: number
87 readonly minimum
: number
88 readonly maximum
: number
89 readonly average
: number
90 readonly median
?: number
95 * Worker tasks queue options.
97 export interface TasksQueueOptions
{
99 * Maximum number of tasks that can be executed concurrently on a worker.
103 readonly concurrency
?: number
107 * Options for a poolifier pool.
109 * @typeParam Worker - Type of worker.
111 export interface PoolOptions
<Worker
extends IWorker
> {
113 * A function that will listen for message event on each worker.
115 messageHandler
?: MessageHandler
<Worker
>
117 * A function that will listen for error event on each worker.
119 errorHandler
?: ErrorHandler
<Worker
>
121 * A function that will listen for online event on each worker.
123 onlineHandler
?: OnlineHandler
<Worker
>
125 * A function that will listen for exit event on each worker.
127 exitHandler
?: ExitHandler
<Worker
>
129 * The worker choice strategy to use in this pool.
131 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
133 workerChoiceStrategy
?: WorkerChoiceStrategy
135 * The worker choice strategy options.
137 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
139 * Restart worker on error.
141 restartWorkerOnError
?: boolean
143 * Pool events emission.
147 enableEvents
?: boolean
149 * Pool worker tasks queue.
151 * @defaultValue false
153 enableTasksQueue
?: boolean
155 * Pool worker tasks queue options.
157 tasksQueueOptions
?: TasksQueueOptions
161 * Contract definition for a poolifier pool.
163 * @typeParam Worker - Type of worker which manages this pool.
164 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
165 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
167 export interface IPool
<
168 Worker
extends IWorker
,
175 readonly info
: PoolInfo
179 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
181 * Emitter on which events can be listened to.
183 * Events that can currently be listened to:
185 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
186 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
187 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing at least one task.
188 * - `'error'`: Emitted when an uncaught error occurs.
189 * - `'taskError'`: Emitted when an error occurs while executing a task.
191 readonly emitter
?: PoolEmitter
193 * Executes the specified function in the worker constructor with the task data input parameter.
195 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
196 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
197 * @returns Promise that will be fulfilled when the task is completed.
199 readonly execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
201 * Terminates every current worker in this pool.
203 readonly destroy
: () => Promise
<void>
205 * Sets the worker choice strategy in this pool.
207 * @param workerChoiceStrategy - The worker choice strategy.
208 * @param workerChoiceStrategyOptions - The worker choice strategy options.
210 readonly setWorkerChoiceStrategy
: (
211 workerChoiceStrategy
: WorkerChoiceStrategy
,
212 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
215 * Sets the worker choice strategy options in this pool.
217 * @param workerChoiceStrategyOptions - The worker choice strategy options.
219 readonly setWorkerChoiceStrategyOptions
: (
220 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
223 * Enables/disables the worker tasks queue in this pool.
225 * @param enable - Whether to enable or disable the worker tasks queue.
226 * @param tasksQueueOptions - The worker tasks queue options.
228 readonly enableTasksQueue
: (
230 tasksQueueOptions
?: TasksQueueOptions
233 * Sets the worker tasks queue options in this pool.
235 * @param tasksQueueOptions - The worker tasks queue options.
237 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void