refactor: improve error messages
[poolifier.git] / src / pools / pool.ts
CommitLineData
9e45c2c4 1import EventEmitterAsyncResource from 'node:events'
bdaf31cd
JB
2import type {
3 ErrorHandler,
4 ExitHandler,
50e66724 5 IWorker,
bdaf31cd 6 MessageHandler,
c4855468
JB
7 OnlineHandler,
8 WorkerNode
f06e48d8 9} from './worker'
da309861
JB
10import type {
11 WorkerChoiceStrategy,
12 WorkerChoiceStrategyOptions
13} from './selection-strategies/selection-strategies-types'
bdaf31cd 14
c4855468 15/**
6b27d407 16 * Enumeration of pool types.
c4855468 17 */
6b27d407 18export const PoolTypes = Object.freeze({
c4855468
JB
19 /**
20 * Fixed pool type.
21 */
6b27d407 22 fixed: 'fixed',
c4855468
JB
23 /**
24 * Dynamic pool type.
25 */
6b27d407
JB
26 dynamic: 'dynamic'
27} as const)
28
29/**
30 * Pool type.
31 */
32export type PoolType = keyof typeof PoolTypes
c4855468 33
184855e6
JB
34/**
35 * Enumeration of worker types.
36 */
37export const WorkerTypes = Object.freeze({
38 cluster: 'cluster',
39 thread: 'thread'
40} as const)
41
42/**
43 * Worker type.
44 */
45export type WorkerType = keyof typeof WorkerTypes
46
b4904890
JB
47/**
48 * Pool events emitter.
49 */
9e45c2c4 50export class PoolEmitter extends EventEmitterAsyncResource {}
b4904890 51
aee46736
JB
52/**
53 * Enumeration of pool events.
54 */
55export const PoolEvents = Object.freeze({
56 full: 'full',
1f68cede 57 busy: 'busy',
91ee39ed
JB
58 error: 'error',
59 taskError: 'taskError'
aee46736
JB
60} as const)
61
62/**
63 * Pool event.
64 */
65export type PoolEvent = keyof typeof PoolEvents
66
6b27d407
JB
67/**
68 * Pool information.
69 */
70export interface PoolInfo {
71 type: PoolType
184855e6 72 worker: WorkerType
6b27d407
JB
73 minSize: number
74 maxSize: number
64383951 75 /** Pool utilization ratio. */
afe0d5bf 76 utilization: number
64383951 77 /** Pool total worker nodes */
6b27d407 78 workerNodes: number
64383951 79 /** Pool idle worker nodes */
6b27d407 80 idleWorkerNodes: number
64383951 81 /** Pool busy worker nodes */
6b27d407 82 busyWorkerNodes: number
a4e07f72
JB
83 executedTasks: number
84 executingTasks: number
6b27d407
JB
85 queuedTasks: number
86 maxQueuedTasks: number
a4e07f72 87 failedTasks: number
6b27d407
JB
88}
89
7171d33f
JB
90/**
91 * Worker tasks queue options.
92 */
93export interface TasksQueueOptions {
94 /**
95 * Maximum number of tasks that can be executed concurrently on a worker.
96 *
97 * @defaultValue 1
98 */
99 concurrency?: number
100}
101
bdaf31cd
JB
102/**
103 * Options for a poolifier pool.
c319c66b 104 *
d480d708 105 * @typeParam Worker - Type of worker.
bdaf31cd 106 */
50e66724 107export interface PoolOptions<Worker extends IWorker> {
bdaf31cd
JB
108 /**
109 * A function that will listen for message event on each worker.
110 */
111 messageHandler?: MessageHandler<Worker>
112 /**
113 * A function that will listen for error event on each worker.
114 */
115 errorHandler?: ErrorHandler<Worker>
116 /**
117 * A function that will listen for online event on each worker.
118 */
119 onlineHandler?: OnlineHandler<Worker>
120 /**
121 * A function that will listen for exit event on each worker.
122 */
123 exitHandler?: ExitHandler<Worker>
124 /**
46e857ca 125 * The worker choice strategy to use in this pool.
d29bce7c 126 *
95ec6006 127 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd
JB
128 */
129 workerChoiceStrategy?: WorkerChoiceStrategy
da309861
JB
130 /**
131 * The worker choice strategy options.
132 */
133 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
1f68cede
JB
134 /**
135 * Restart worker on error.
136 */
137 restartWorkerOnError?: boolean
bdaf31cd
JB
138 /**
139 * Pool events emission.
140 *
38e795c1 141 * @defaultValue true
bdaf31cd
JB
142 */
143 enableEvents?: boolean
ff733df7
JB
144 /**
145 * Pool worker tasks queue.
146 *
ff733df7
JB
147 * @defaultValue false
148 */
149 enableTasksQueue?: boolean
7171d33f
JB
150 /**
151 * Pool worker tasks queue options.
7171d33f
JB
152 */
153 tasksQueueOptions?: TasksQueueOptions
bdaf31cd 154}
a35560ba 155
729c563d
S
156/**
157 * Contract definition for a poolifier pool.
158 *
c4855468 159 * @typeParam Worker - Type of worker which manages this pool.
e102732c
JB
160 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
161 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
729c563d 162 */
c4855468
JB
163export interface IPool<
164 Worker extends IWorker,
165 Data = unknown,
166 Response = unknown
167> {
08f3f44c 168 /**
6b27d407 169 * Pool information.
08f3f44c 170 */
6b27d407 171 readonly info: PoolInfo
c4855468
JB
172 /**
173 * Pool worker nodes.
174 */
175 readonly workerNodes: Array<WorkerNode<Worker, Data>>
b4904890
JB
176 /**
177 * Emitter on which events can be listened to.
178 *
179 * Events that can currently be listened to:
180 *
164d950a
JB
181 * - `'full'`: Emitted when the pool is dynamic and full.
182 * - `'busy'`: Emitted when the pool is busy.
91ee39ed
JB
183 * - `'error'`: Emitted when an uncaught error occurs.
184 * - `'taskError'`: Emitted when an error occurs while executing a task.
b4904890
JB
185 */
186 readonly emitter?: PoolEmitter
729c563d 187 /**
61aa11a6 188 * Executes the specified function in the worker constructor with the task data input parameter.
729c563d 189 *
e102732c 190 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
a86b6df1 191 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
ef41a6e6 192 * @returns Promise that will be fulfilled when the task is completed.
729c563d 193 */
a86b6df1 194 execute: (data?: Data, name?: string) => Promise<Response>
280c2a77 195 /**
64383951 196 * Terminates every current worker in this pool.
280c2a77 197 */
78cea37e 198 destroy: () => Promise<void>
a35560ba 199 /**
bdede008 200 * Sets the worker choice strategy in this pool.
a35560ba 201 *
38e795c1 202 * @param workerChoiceStrategy - The worker choice strategy.
59219cbb 203 * @param workerChoiceStrategyOptions - The worker choice strategy options.
a35560ba 204 */
59219cbb
JB
205 setWorkerChoiceStrategy: (
206 workerChoiceStrategy: WorkerChoiceStrategy,
207 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
208 ) => void
a20f0ba5
JB
209 /**
210 * Sets the worker choice strategy options in this pool.
211 *
212 * @param workerChoiceStrategyOptions - The worker choice strategy options.
213 */
214 setWorkerChoiceStrategyOptions: (
215 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
216 ) => void
217 /**
218 * Enables/disables the worker tasks queue in this pool.
219 *
220 * @param enable - Whether to enable or disable the worker tasks queue.
221 * @param tasksQueueOptions - The worker tasks queue options.
222 */
8f52842f
JB
223 enableTasksQueue: (
224 enable: boolean,
225 tasksQueueOptions?: TasksQueueOptions
226 ) => void
a20f0ba5
JB
227 /**
228 * Sets the worker tasks queue options in this pool.
229 *
230 * @param tasksQueueOptions - The worker tasks queue options.
231 */
232 setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void
c97c7edb 233}