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({
49 taskError
: 'taskError'
55 export type PoolEvent
= keyof
typeof PoolEvents
60 export interface PoolInfo
{
61 readonly version
: string
62 readonly type: PoolType
63 readonly worker
: WorkerType
64 readonly ready
: boolean
65 readonly strategy
: WorkerChoiceStrategy
66 readonly minSize
: number
67 readonly maxSize
: number
68 /** Pool utilization. */
69 readonly utilization
?: number
70 /** Pool total worker nodes. */
71 readonly workerNodes
: number
72 /** Pool idle worker nodes. */
73 readonly idleWorkerNodes
: number
74 /** Pool busy worker nodes. */
75 readonly busyWorkerNodes
: number
76 readonly executedTasks
: number
77 readonly executingTasks
: number
78 readonly queuedTasks
?: number
79 readonly maxQueuedTasks
?: number
80 readonly failedTasks
: number
82 readonly minimum
: number
83 readonly maximum
: number
84 readonly average
: number
85 readonly median
?: number
88 readonly minimum
: number
89 readonly maximum
: number
90 readonly average
: number
91 readonly median
?: number
96 * Worker tasks queue options.
98 export interface TasksQueueOptions
{
100 * Maximum number of tasks that can be executed concurrently on a worker.
104 readonly concurrency
?: number
108 * Options for a poolifier pool.
110 * @typeParam Worker - Type of worker.
112 export interface PoolOptions
<Worker
extends IWorker
> {
114 * A function that will listen for message event on each worker.
116 messageHandler
?: MessageHandler
<Worker
>
118 * A function that will listen for error event on each worker.
120 errorHandler
?: ErrorHandler
<Worker
>
122 * A function that will listen for online event on each worker.
124 onlineHandler
?: OnlineHandler
<Worker
>
126 * A function that will listen for exit event on each worker.
128 exitHandler
?: ExitHandler
<Worker
>
130 * The worker choice strategy to use in this pool.
132 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
134 workerChoiceStrategy
?: WorkerChoiceStrategy
136 * The worker choice strategy options.
138 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
140 * Restart worker on error.
142 restartWorkerOnError
?: boolean
144 * Pool events emission.
148 enableEvents
?: boolean
150 * Pool worker tasks queue.
152 * @defaultValue false
154 enableTasksQueue
?: boolean
156 * Pool worker tasks queue options.
158 tasksQueueOptions
?: TasksQueueOptions
162 * Contract definition for a poolifier pool.
164 * @typeParam Worker - Type of worker which manages this pool.
165 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
166 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
168 export interface IPool
<
169 Worker
extends IWorker
,
176 readonly info
: PoolInfo
180 readonly workerNodes
: Array<IWorkerNode
<Worker
, Data
>>
182 * Emitter on which events can be listened to.
184 * Events that can currently be listened to:
186 * - `'full'`: Emitted when the pool is dynamic and the number of workers created has reached the maximum size expected.
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 * - `'error'`: Emitted when an uncaught error occurs.
190 * - `'taskError'`: Emitted when an error occurs while executing a task.
192 readonly emitter
?: PoolEmitter
194 * Executes the specified function in the worker constructor with the task data input parameter.
196 * @param data - The optional task input data for the specified task function. This can only be structured-cloneable data.
197 * @param name - The optional name of the task function to execute. If not specified, the default task function will be executed.
198 * @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.
199 * @returns Promise that will be fulfilled when the task is completed.
204 transferList
?: TransferListItem
[]
205 ) => Promise
<Response
>
207 * Terminates all workers in this pool.
209 readonly destroy
: () => Promise
<void>
211 * Lists the names of task function available in this pool.
213 * @returns The names of task function available in this pool.
215 readonly listTaskFunctions
: () => string[]
217 * Sets the worker choice strategy in this pool.
219 * @param workerChoiceStrategy - The worker choice strategy.
220 * @param workerChoiceStrategyOptions - The worker choice strategy options.
222 readonly setWorkerChoiceStrategy
: (
223 workerChoiceStrategy
: WorkerChoiceStrategy
,
224 workerChoiceStrategyOptions
?: WorkerChoiceStrategyOptions
227 * Sets the worker choice strategy options in this pool.
229 * @param workerChoiceStrategyOptions - The worker choice strategy options.
231 readonly setWorkerChoiceStrategyOptions
: (
232 workerChoiceStrategyOptions
: WorkerChoiceStrategyOptions
235 * Enables/disables the worker tasks queue in this pool.
237 * @param enable - Whether to enable or disable the worker tasks queue.
238 * @param tasksQueueOptions - The worker tasks queue options.
240 readonly enableTasksQueue
: (
242 tasksQueueOptions
?: TasksQueueOptions
245 * Sets the worker tasks queue options in this pool.
247 * @param tasksQueueOptions - The worker tasks queue options.
249 readonly setTasksQueueOptions
: (tasksQueueOptions
: TasksQueueOptions
) => void