feat: add utilization to pool information
[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
afe0d5bf 75 utilization: number
6b27d407
JB
76 workerNodes: number
77 idleWorkerNodes: number
78 busyWorkerNodes: number
a4e07f72
JB
79 executedTasks: number
80 executingTasks: number
6b27d407
JB
81 queuedTasks: number
82 maxQueuedTasks: number
a4e07f72 83 failedTasks: number
6b27d407
JB
84}
85
7171d33f
JB
86/**
87 * Worker tasks queue options.
88 */
89export interface TasksQueueOptions {
90 /**
91 * Maximum number of tasks that can be executed concurrently on a worker.
92 *
93 * @defaultValue 1
94 */
95 concurrency?: number
96}
97
bdaf31cd
JB
98/**
99 * Options for a poolifier pool.
c319c66b 100 *
d480d708 101 * @typeParam Worker - Type of worker.
bdaf31cd 102 */
50e66724 103export interface PoolOptions<Worker extends IWorker> {
bdaf31cd
JB
104 /**
105 * A function that will listen for message event on each worker.
106 */
107 messageHandler?: MessageHandler<Worker>
108 /**
109 * A function that will listen for error event on each worker.
110 */
111 errorHandler?: ErrorHandler<Worker>
112 /**
113 * A function that will listen for online event on each worker.
114 */
115 onlineHandler?: OnlineHandler<Worker>
116 /**
117 * A function that will listen for exit event on each worker.
118 */
119 exitHandler?: ExitHandler<Worker>
120 /**
46e857ca 121 * The worker choice strategy to use in this pool.
d29bce7c 122 *
95ec6006 123 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
bdaf31cd
JB
124 */
125 workerChoiceStrategy?: WorkerChoiceStrategy
da309861
JB
126 /**
127 * The worker choice strategy options.
128 */
129 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
1f68cede
JB
130 /**
131 * Restart worker on error.
132 */
133 restartWorkerOnError?: boolean
bdaf31cd
JB
134 /**
135 * Pool events emission.
136 *
38e795c1 137 * @defaultValue true
bdaf31cd
JB
138 */
139 enableEvents?: boolean
ff733df7
JB
140 /**
141 * Pool worker tasks queue.
142 *
ff733df7
JB
143 * @defaultValue false
144 */
145 enableTasksQueue?: boolean
7171d33f
JB
146 /**
147 * Pool worker tasks queue options.
7171d33f
JB
148 */
149 tasksQueueOptions?: TasksQueueOptions
bdaf31cd 150}
a35560ba 151
729c563d
S
152/**
153 * Contract definition for a poolifier pool.
154 *
c4855468 155 * @typeParam Worker - Type of worker which manages this pool.
e102732c
JB
156 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
157 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
729c563d 158 */
c4855468
JB
159export interface IPool<
160 Worker extends IWorker,
161 Data = unknown,
162 Response = unknown
163> {
08f3f44c 164 /**
6b27d407 165 * Pool information.
08f3f44c 166 */
6b27d407 167 readonly info: PoolInfo
c4855468
JB
168 /**
169 * Pool worker nodes.
170 */
171 readonly workerNodes: Array<WorkerNode<Worker, Data>>
b4904890
JB
172 /**
173 * Emitter on which events can be listened to.
174 *
175 * Events that can currently be listened to:
176 *
164d950a
JB
177 * - `'full'`: Emitted when the pool is dynamic and full.
178 * - `'busy'`: Emitted when the pool is busy.
91ee39ed
JB
179 * - `'error'`: Emitted when an uncaught error occurs.
180 * - `'taskError'`: Emitted when an error occurs while executing a task.
b4904890
JB
181 */
182 readonly emitter?: PoolEmitter
729c563d 183 /**
61aa11a6 184 * Executes the specified function in the worker constructor with the task data input parameter.
729c563d 185 *
e102732c 186 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
a86b6df1 187 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
ef41a6e6 188 * @returns Promise that will be fulfilled when the task is completed.
729c563d 189 */
a86b6df1 190 execute: (data?: Data, name?: string) => Promise<Response>
280c2a77 191 /**
6c6afb84 192 * Terminate every current worker in this pool.
280c2a77 193 */
78cea37e 194 destroy: () => Promise<void>
a35560ba 195 /**
bdede008 196 * Sets the worker choice strategy in this pool.
a35560ba 197 *
38e795c1 198 * @param workerChoiceStrategy - The worker choice strategy.
59219cbb 199 * @param workerChoiceStrategyOptions - The worker choice strategy options.
a35560ba 200 */
59219cbb
JB
201 setWorkerChoiceStrategy: (
202 workerChoiceStrategy: WorkerChoiceStrategy,
203 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
204 ) => void
a20f0ba5
JB
205 /**
206 * Sets the worker choice strategy options in this pool.
207 *
208 * @param workerChoiceStrategyOptions - The worker choice strategy options.
209 */
210 setWorkerChoiceStrategyOptions: (
211 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
212 ) => void
213 /**
214 * Enables/disables the worker tasks queue in this pool.
215 *
216 * @param enable - Whether to enable or disable the worker tasks queue.
217 * @param tasksQueueOptions - The worker tasks queue options.
218 */
8f52842f
JB
219 enableTasksQueue: (
220 enable: boolean,
221 tasksQueueOptions?: TasksQueueOptions
222 ) => void
a20f0ba5
JB
223 /**
224 * Sets the worker tasks queue options in this pool.
225 *
226 * @param tasksQueueOptions - The worker tasks queue options.
227 */
228 setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void
c97c7edb 229}