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