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