1 import { EventEmitter
} from
'node:events'
2 import { type TransferListItem
} from
'node:worker_threads'
14 WorkerChoiceStrategyOptions
15 } from
'./selection-strategies/selection-strategies-types'
18 * Enumeration of pool types.
20 export const PoolTypes
= Object.freeze({
34 export type PoolType
= keyof
typeof PoolTypes
37 * Pool events emitter.
39 export class PoolEmitter
extends EventEmitter
{}
42 * Enumeration of pool events.
44 export const PoolEvents
= Object.freeze({
50 taskError
: 'taskError'
56 export type PoolEvent
= keyof
typeof PoolEvents
61 export interface PoolInfo
{
62 readonly version
: string
63 readonly type: PoolType
64 readonly worker
: WorkerType
65 readonly ready
: boolean
66 readonly strategy
: WorkerChoiceStrategy
67 readonly minSize
: number
68 readonly maxSize
: number
69 /** Pool utilization. */
70 readonly utilization
?: number
71 /** Pool total worker nodes. */
72 readonly workerNodes
: number
73 /** Pool idle worker nodes. */
74 readonly idleWorkerNodes
: number
75 /** Pool busy worker nodes. */
76 readonly busyWorkerNodes
: number
77 readonly executedTasks
: number
78 readonly executingTasks
: number
79 readonly queuedTasks
?: number
80 readonly maxQueuedTasks
?: number
81 readonly failedTasks
: number
83 readonly minimum
: number
84 readonly maximum
: number
85 readonly average
: number
86 readonly median
?: number
89 readonly minimum
: number
90 readonly maximum
: number
91 readonly average
: number
92 readonly median
?: number
97 * Worker tasks queue options.
99 export interface TasksQueueOptions
{
101 * Maximum number of tasks that can be executed concurrently on a worker.
105 readonly concurrency
?: number
109 * Options for a poolifier pool.
111 * @typeParam Worker - Type of worker.
113 export interface PoolOptions
<Worker
extends IWorker
> {
115 * A function that will listen for message event on each worker.
117 messageHandler
?: MessageHandler
<Worker
>
119 * A function that will listen for error event on each worker.
121 errorHandler
?: ErrorHandler
<Worker
>
123 * A function that will listen for online event on each worker.
125 onlineHandler
?: OnlineHandler
<Worker
>
127 * A function that will listen for exit event on each worker.
129 exitHandler
?: ExitHandler
<Worker
>
131 * The worker choice strategy to use in this pool.
133 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
135 workerChoiceStrategy
?: WorkerChoiceStrategy
137 * The worker choice strategy options.
139 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
141 * Restart worker on error.
143 restartWorkerOnError
?: boolean
145 * Pool events emission.
149 enableEvents
?: boolean
151 * Pool worker tasks queue.
153 * @defaultValue false
155 enableTasksQueue
?: boolean
157 * Pool worker tasks queue options.
159 tasksQueueOptions
?: TasksQueueOptions
163 * Contract definition for a poolifier pool.
165 * @typeParam Worker - Type of worker which manages this pool.
166 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
167 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
169 export interface IPool
<
170 Worker
extends IWorker
,
177 readonly info
: PoolInfo
181 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
183 * Emitter on which events can be listened to.
185 * Events that can currently be listened to:
187 * - `'ready'`: Emitted when the number of workers created in the pool has reached the minimum size expected and are ready.
188 * - `'busy'`: Emitted when the number of workers created in the pool has reached the maximum size expected and are executing at least one task.
189 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
190 * - '`destroy`': Emitted when the pool is destroyed.
191 * - `'error'`: Emitted when an uncaught error occurs.
192 * - `'taskError'`: Emitted when an error occurs while executing a task.
194 readonly emitter
?: PoolEmitter
196 * Executes the specified function in the worker constructor with the task data input parameter.
198 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
199 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
200 * @param transferList - An optional array of transferable objects to transfer ownership of. Ownership of the transferred objects is given to the pool's worker_threads worker and they should not be used in the main thread afterwards.
201 * @returns Promise that will be fulfilled when the task is completed.
206 transferList
?: TransferListItem
[]
207 ) => Promise
<Response
>
209 * Terminates all workers in this pool.
211 readonly destroy
: () => Promise
<void>
213 * Lists the names of task function available in this pool.
215 * @returns The names of task function available in this pool.
217 readonly listTaskFunctions
: () => string[]
219 * Sets the worker choice strategy in this pool.
221 * @param workerChoiceStrategy - The worker choice strategy.
222 * @param workerChoiceStrategyOptions - The worker choice strategy options.
224 readonly setWorkerChoiceStrategy
: (
225 workerChoiceStrategy
: WorkerChoiceStrategy
,
226 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
229 * Sets the worker choice strategy options in this pool.
231 * @param workerChoiceStrategyOptions - The worker choice strategy options.
233 readonly setWorkerChoiceStrategyOptions
: (
234 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
237 * Enables/disables the worker tasks queue in this pool.
239 * @param enable - Whether to enable or disable the worker tasks queue.
240 * @param tasksQueueOptions - The worker tasks queue options.
242 readonly enableTasksQueue
: (
244 tasksQueueOptions
?: TasksQueueOptions
247 * Sets the worker tasks queue options in this pool.
249 * @param tasksQueueOptions - The worker tasks queue options.
251 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void