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
104 * Worker tasks queue options.
106 export interface TasksQueueOptions
{
108 * Maximum number of tasks that can be executed concurrently on a worker.
112 readonly concurrency
?: number
116 * Options for a poolifier pool.
118 * @typeParam Worker - Type of worker.
120 export interface PoolOptions
<Worker
extends IWorker
> {
122 * A function that will listen for message event on each worker.
124 messageHandler
?: MessageHandler
<Worker
>
126 * A function that will listen for error event on each worker.
128 errorHandler
?: ErrorHandler
<Worker
>
130 * A function that will listen for online event on each worker.
132 onlineHandler
?: OnlineHandler
<Worker
>
134 * A function that will listen for exit event on each worker.
136 exitHandler
?: ExitHandler
<Worker
>
138 * The worker choice strategy to use in this pool.
140 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
142 workerChoiceStrategy
?: WorkerChoiceStrategy
144 * The worker choice strategy options.
146 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
148 * Restart worker on error.
150 restartWorkerOnError
?: boolean
152 * Pool events emission.
156 enableEvents
?: boolean
158 * Pool worker tasks queue.
160 * @defaultValue false
162 enableTasksQueue
?: boolean
164 * Pool worker tasks queue options.
166 tasksQueueOptions
?: TasksQueueOptions
170 * Contract definition for a poolifier pool.
172 * @typeParam Worker - Type of worker which manages this pool.
173 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
174 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
176 export interface IPool
<
177 Worker
extends IWorker
,
184 readonly info
: PoolInfo
188 readonly workerNodes
: Array<WorkerNode
<Worker
, Data
>>
190 * Emitter on which events can be listened to.
192 * Events that can currently be listened to:
194 * - `'full'`: Emitted when the pool is dynamic and full.
195 * - `'busy'`: Emitted when the pool is busy.
196 * - `'error'`: Emitted when an uncaught error occurs.
197 * - `'taskError'`: Emitted when an error occurs while executing a task.
199 readonly emitter
?: PoolEmitter
201 * Executes the specified function in the worker constructor with the task data input parameter.
203 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
204 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
205 * @returns Promise that will be fulfilled when the task is completed.
207 execute
: (data
?: Data
, name
?: string) => Promise
<Response
>
209 * Terminates every current worker in this pool.
211 destroy
: () => Promise
<void>
213 * Sets the worker choice strategy in this pool.
215 * @param workerChoiceStrategy - The worker choice strategy.
216 * @param workerChoiceStrategyOptions - The worker choice strategy options.
218 setWorkerChoiceStrategy
: (
219 workerChoiceStrategy
: WorkerChoiceStrategy
,
220 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
223 * Sets the worker choice strategy options in this pool.
225 * @param workerChoiceStrategyOptions - The worker choice strategy options.
227 setWorkerChoiceStrategyOptions
: (
228 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
231 * Enables/disables the worker tasks queue in this pool.
233 * @param enable - Whether to enable or disable the worker tasks queue.
234 * @param tasksQueueOptions - The worker tasks queue options.
238 tasksQueueOptions
?: TasksQueueOptions
241 * Sets the worker tasks queue options in this pool.
243 * @param tasksQueueOptions - The worker tasks queue options.
245 setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void