build: disable esModuleInterop from TS configuration
[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 }
90
91 /**
92 * Worker tasks queue options.
93 */
94 export interface TasksQueueOptions {
95 /**
96 * Maximum number of tasks that can be executed concurrently on a worker.
97 *
98 * @defaultValue 1
99 */
100 readonly concurrency?: number
101 }
102
103 /**
104 * Options for a poolifier pool.
105 *
106 * @typeParam Worker - Type of worker.
107 */
108 export interface PoolOptions<Worker extends IWorker> {
109 /**
110 * A function that will listen for message event on each worker.
111 */
112 messageHandler?: MessageHandler<Worker>
113 /**
114 * A function that will listen for error event on each worker.
115 */
116 errorHandler?: ErrorHandler<Worker>
117 /**
118 * A function that will listen for online event on each worker.
119 */
120 onlineHandler?: OnlineHandler<Worker>
121 /**
122 * A function that will listen for exit event on each worker.
123 */
124 exitHandler?: ExitHandler<Worker>
125 /**
126 * The worker choice strategy to use in this pool.
127 *
128 * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN
129 */
130 workerChoiceStrategy?: WorkerChoiceStrategy
131 /**
132 * The worker choice strategy options.
133 */
134 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
135 /**
136 * Restart worker on error.
137 */
138 restartWorkerOnError?: boolean
139 /**
140 * Pool events emission.
141 *
142 * @defaultValue true
143 */
144 enableEvents?: boolean
145 /**
146 * Pool worker tasks queue.
147 *
148 * @defaultValue false
149 */
150 enableTasksQueue?: boolean
151 /**
152 * Pool worker tasks queue options.
153 */
154 tasksQueueOptions?: TasksQueueOptions
155 }
156
157 /**
158 * Contract definition for a poolifier pool.
159 *
160 * @typeParam Worker - Type of worker which manages this pool.
161 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
162 * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
163 */
164 export interface IPool<
165 Worker extends IWorker,
166 Data = unknown,
167 Response = unknown
168 > {
169 /**
170 * Pool information.
171 */
172 readonly info: PoolInfo
173 /**
174 * Pool worker nodes.
175 */
176 readonly workerNodes: Array<WorkerNode<Worker, Data>>
177 /**
178 * Emitter on which events can be listened to.
179 *
180 * Events that can currently be listened to:
181 *
182 * - `'full'`: Emitted when the pool is dynamic and full.
183 * - `'busy'`: Emitted when the pool is busy.
184 * - `'error'`: Emitted when an uncaught error occurs.
185 * - `'taskError'`: Emitted when an error occurs while executing a task.
186 */
187 readonly emitter?: PoolEmitter
188 /**
189 * Executes the specified function in the worker constructor with the task data input parameter.
190 *
191 * @param data - The task input data for the specified worker function. This can only be structured-cloneable data.
192 * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed.
193 * @returns Promise that will be fulfilled when the task is completed.
194 */
195 execute: (data?: Data, name?: string) => Promise<Response>
196 /**
197 * Terminates every current worker in this pool.
198 */
199 destroy: () => Promise<void>
200 /**
201 * Sets the worker choice strategy in this pool.
202 *
203 * @param workerChoiceStrategy - The worker choice strategy.
204 * @param workerChoiceStrategyOptions - The worker choice strategy options.
205 */
206 setWorkerChoiceStrategy: (
207 workerChoiceStrategy: WorkerChoiceStrategy,
208 workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions
209 ) => void
210 /**
211 * Sets the worker choice strategy options in this pool.
212 *
213 * @param workerChoiceStrategyOptions - The worker choice strategy options.
214 */
215 setWorkerChoiceStrategyOptions: (
216 workerChoiceStrategyOptions: WorkerChoiceStrategyOptions
217 ) => void
218 /**
219 * Enables/disables the worker tasks queue in this pool.
220 *
221 * @param enable - Whether to enable or disable the worker tasks queue.
222 * @param tasksQueueOptions - The worker tasks queue options.
223 */
224 enableTasksQueue: (
225 enable: boolean,
226 tasksQueueOptions?: TasksQueueOptions
227 ) => void
228 /**
229 * Sets the worker tasks queue options in this pool.
230 *
231 * @param tasksQueueOptions - The worker tasks queue options.
232 */
233 setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void
234 }