1 import { EventEmitter
} from
'node:events'
12 WorkerChoiceStrategyOptions
13 } from
'./selection-strategies/selection-strategies-types'
16 * Enumeration of pool types.
18 export const PoolTypes
= Object.freeze({
32 export type PoolType
= keyof
typeof PoolTypes
35 * Enumeration of worker types.
37 export const WorkerTypes
= Object.freeze({
45 export type WorkerType
= keyof
typeof WorkerTypes
48 * Pool events emitter.
50 export class PoolEmitter
extends EventEmitter
{}
53 * Enumeration of pool events.
55 export const PoolEvents
= Object.freeze({
59 taskError
: 'taskError'
65 export type PoolEvent
= keyof
typeof PoolEvents
70 export interface PoolInfo
{
76 /** Pool utilization ratio. */
78 /** Pool total worker nodes */
80 /** Pool idle worker nodes */
81 idleWorkerNodes
: number
82 /** Pool busy worker nodes */
83 busyWorkerNodes
: number
85 executingTasks
: number
87 maxQueuedTasks
: number
100 * Worker tasks queue options.
102 export interface TasksQueueOptions
{
104 * Maximum number of tasks that can be executed concurrently on a worker.
108 readonly concurrency
?: number
112 * Options for a poolifier pool.
114 * @typeParam Worker - Type of worker.
116 export interface PoolOptions
<Worker
extends IWorker
> {
118 * A function that will listen for message event on each worker.
120 messageHandler
?: MessageHandler
<Worker
>
122 * A function that will listen for error event on each worker.
124 errorHandler
?: ErrorHandler
<Worker
>
126 * A function that will listen for online event on each worker.
128 onlineHandler
?: OnlineHandler
<Worker
>
130 * A function that will listen for exit event on each worker.
132 exitHandler
?: ExitHandler
<Worker
>
134 * The worker choice strategy to use in this pool.
136 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
138 workerChoiceStrategy
?: WorkerChoiceStrategy
140 * The worker choice strategy options.
142 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
144 * Restart worker on error.
146 restartWorkerOnError
?: boolean
148 * Pool events emission.
152 enableEvents
?: boolean
154 * Pool worker tasks queue.
156 * @defaultValue false
158 enableTasksQueue
?: boolean
160 * Pool worker tasks queue options.
162 tasksQueueOptions
?: TasksQueueOptions
166 * Contract definition for a poolifier pool.
168 * @typeParam Worker - Type of worker which manages this pool.
169 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
170 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
172 export interface IPool
<
173 Worker
extends IWorker
,
180 readonly info
: PoolInfo
184 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
186 * Emitter on which events can be listened to.
188 * Events that can currently be listened to:
190 * - `'full'`: Emitted when the pool is dynamic and full.
191 * - `'busy'`: Emitted when the pool is busy.
192 * - `'error'`: Emitted when an uncaught error occurs.
193 * - `'taskError'`: Emitted when an error occurs while executing a task.
195 readonly emitter
?: PoolEmitter
197 * Executes the specified function in the worker constructor with the task data input parameter.
199 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
200 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
201 * @returns Promise that will be fulfilled when the task is completed.
203 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
205 * Terminates every current worker in this pool.
207 destroy
: () => Promise
<void>
209 * Sets the worker choice strategy in this pool.
211 * @param workerChoiceStrategy - The worker choice strategy.
212 * @param workerChoiceStrategyOptions - The worker choice strategy options.
214 setWorkerChoiceStrategy
: (
215 workerChoiceStrategy
: WorkerChoiceStrategy
,
216 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
219 * Sets the worker choice strategy options in this pool.
221 * @param workerChoiceStrategyOptions - The worker choice strategy options.
223 setWorkerChoiceStrategyOptions
: (
224 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
227 * Enables/disables the worker tasks queue in this pool.
229 * @param enable - Whether to enable or disable the worker tasks queue.
230 * @param tasksQueueOptions - The worker tasks queue options.
234 tasksQueueOptions
?: TasksQueueOptions
237 * Sets the worker tasks queue options in this pool.
239 * @param tasksQueueOptions - The worker tasks queue options.
241 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void