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({
47 taskError
: 'taskError'
53 export type PoolEvent
= keyof
typeof PoolEvents
58 export interface PoolInfo
{
59 readonly version
: string
60 readonly type: PoolType
61 readonly worker
: WorkerType
62 readonly minSize
: number
63 readonly maxSize
: number
64 /** Pool utilization ratio. */
65 readonly utilization
?: number
66 /** Pool total worker nodes */
67 readonly workerNodes
: number
68 /** Pool idle worker nodes */
69 readonly idleWorkerNodes
: number
70 /** Pool busy worker nodes */
71 readonly busyWorkerNodes
: number
72 readonly executedTasks
: number
73 readonly executingTasks
: number
74 readonly queuedTasks
: number
75 readonly maxQueuedTasks
: number
76 readonly failedTasks
: number
78 readonly minimum
: number
79 readonly maximum
: number
80 readonly average
: number
81 readonly median
?: number
84 readonly minimum
: number
85 readonly maximum
: number
86 readonly average
: number
87 readonly median
?: number
92 * Worker tasks queue options.
94 export interface TasksQueueOptions
{
96 * Maximum number of tasks that can be executed concurrently on a worker.
100 readonly concurrency
?: number
104 * Options for a poolifier pool.
106 * @typeParam Worker - Type of worker.
108 export interface PoolOptions
<Worker
extends IWorker
> {
110 * A function that will listen for message event on each worker.
112 messageHandler
?: MessageHandler
<Worker
>
114 * A function that will listen for error event on each worker.
116 errorHandler
?: ErrorHandler
<Worker
>
118 * A function that will listen for online event on each worker.
120 onlineHandler
?: OnlineHandler
<Worker
>
122 * A function that will listen for exit event on each worker.
124 exitHandler
?: ExitHandler
<Worker
>
126 * The worker choice strategy to use in this pool.
128 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
130 workerChoiceStrategy
?: WorkerChoiceStrategy
132 * The worker choice strategy options.
134 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
136 * Restart worker on error.
138 restartWorkerOnError
?: boolean
140 * Pool events emission.
144 enableEvents
?: boolean
146 * Pool worker tasks queue.
148 * @defaultValue false
150 enableTasksQueue
?: boolean
152 * Pool worker tasks queue options.
154 tasksQueueOptions
?: TasksQueueOptions
158 * Contract definition for a poolifier pool.
160 * @typeParam Worker - Type of worker which manages this pool.
161 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
162 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
164 export interface IPool
<
165 Worker
extends IWorker
,
172 readonly info
: PoolInfo
176 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
178 * Emitter on which events can be listened to.
180 * Events that can currently be listened to:
182 * - `'full'`: Emitted when the pool is dynamic and full.
183 * - `'busy'`: Emitted when the pool is busy.
184 * - `'error'`: Emitted when an uncaught error occurs.
185 * - `'taskError'`: Emitted when an error occurs while executing a task.
187 readonly emitter
?: PoolEmitter
189 * Executes the specified function in the worker constructor with the task data input parameter.
191 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
192 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
193 * @returns Promise that will be fulfilled when the task is completed.
195 readonly execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
197 * Terminates every current worker in this pool.
199 readonly destroy
: () => Promise
<void>
201 * Sets the worker choice strategy in this pool.
203 * @param workerChoiceStrategy - The worker choice strategy.
204 * @param workerChoiceStrategyOptions - The worker choice strategy options.
206 readonly setWorkerChoiceStrategy
: (
207 workerChoiceStrategy
: WorkerChoiceStrategy
,
208 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
211 * Sets the worker choice strategy options in this pool.
213 * @param workerChoiceStrategyOptions - The worker choice strategy options.
215 readonly setWorkerChoiceStrategyOptions
: (
216 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
219 * Enables/disables the worker tasks queue in this pool.
221 * @param enable - Whether to enable or disable the worker tasks queue.
222 * @param tasksQueueOptions - The worker tasks queue options.
224 readonly enableTasksQueue
: (
226 tasksQueueOptions
?: TasksQueueOptions
229 * Sets the worker tasks queue options in this pool.
231 * @param tasksQueueOptions - The worker tasks queue options.
233 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void