Commit | Line | Data |
---|---|---|
2845f2a5 | 1 | import { EventEmitter } from 'node:events' |
bdaf31cd JB |
2 | import type { |
3 | ErrorHandler, | |
4 | ExitHandler, | |
50e66724 | 5 | IWorker, |
bdaf31cd | 6 | MessageHandler, |
c4855468 JB |
7 | OnlineHandler, |
8 | WorkerNode | |
f06e48d8 | 9 | } from './worker' |
da309861 JB |
10 | import 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 | 18 | export 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 | */ | |
32 | export type PoolType = keyof typeof PoolTypes | |
c4855468 | 33 | |
184855e6 JB |
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 | ||
b4904890 JB |
47 | /** |
48 | * Pool events emitter. | |
49 | */ | |
2845f2a5 | 50 | export class PoolEmitter extends EventEmitter {} |
b4904890 | 51 | |
aee46736 JB |
52 | /** |
53 | * Enumeration of pool events. | |
54 | */ | |
55 | export 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 | */ | |
65 | export type PoolEvent = keyof typeof PoolEvents | |
66 | ||
6b27d407 JB |
67 | /** |
68 | * Pool information. | |
69 | */ | |
70 | export interface PoolInfo { | |
23ccf9d7 | 71 | version: string |
6b27d407 | 72 | type: PoolType |
184855e6 | 73 | worker: WorkerType |
6b27d407 JB |
74 | minSize: number |
75 | maxSize: number | |
64383951 | 76 | /** Pool utilization ratio. */ |
c05f0d50 | 77 | utilization?: number |
64383951 | 78 | /** Pool total worker nodes */ |
6b27d407 | 79 | workerNodes: number |
64383951 | 80 | /** Pool idle worker nodes */ |
6b27d407 | 81 | idleWorkerNodes: number |
64383951 | 82 | /** Pool busy worker nodes */ |
6b27d407 | 83 | busyWorkerNodes: number |
a4e07f72 JB |
84 | executedTasks: number |
85 | executingTasks: number | |
6b27d407 JB |
86 | queuedTasks: number |
87 | maxQueuedTasks: number | |
a4e07f72 | 88 | failedTasks: number |
1dcf8b7b JB |
89 | runTime?: { |
90 | minimum: number | |
91 | maximum: number | |
92 | } | |
93 | waitTime?: { | |
94 | minimum: number | |
95 | maximum: number | |
96 | } | |
6b27d407 JB |
97 | } |
98 | ||
7171d33f JB |
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 | */ | |
eb7bf744 | 108 | readonly concurrency?: number |
7171d33f JB |
109 | } |
110 | ||
bdaf31cd JB |
111 | /** |
112 | * Options for a poolifier pool. | |
c319c66b | 113 | * |
d480d708 | 114 | * @typeParam Worker - Type of worker. |
bdaf31cd | 115 | */ |
50e66724 | 116 | export interface PoolOptions<Worker extends IWorker> { |
bdaf31cd JB |
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 | /** | |
46e857ca | 134 | * The worker choice strategy to use in this pool. |
d29bce7c | 135 | * |
95ec6006 | 136 | * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd JB |
137 | */ |
138 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
139 | /** |
140 | * The worker choice strategy options. | |
141 | */ | |
142 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
1f68cede JB |
143 | /** |
144 | * Restart worker on error. | |
145 | */ | |
146 | restartWorkerOnError?: boolean | |
bdaf31cd JB |
147 | /** |
148 | * Pool events emission. | |
149 | * | |
38e795c1 | 150 | * @defaultValue true |
bdaf31cd JB |
151 | */ |
152 | enableEvents?: boolean | |
ff733df7 JB |
153 | /** |
154 | * Pool worker tasks queue. | |
155 | * | |
ff733df7 JB |
156 | * @defaultValue false |
157 | */ | |
158 | enableTasksQueue?: boolean | |
7171d33f JB |
159 | /** |
160 | * Pool worker tasks queue options. | |
7171d33f JB |
161 | */ |
162 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 163 | } |
a35560ba | 164 | |
729c563d S |
165 | /** |
166 | * Contract definition for a poolifier pool. | |
167 | * | |
c4855468 | 168 | * @typeParam Worker - Type of worker which manages this pool. |
e102732c JB |
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. | |
729c563d | 171 | */ |
c4855468 JB |
172 | export interface IPool< |
173 | Worker extends IWorker, | |
174 | Data = unknown, | |
175 | Response = unknown | |
176 | > { | |
08f3f44c | 177 | /** |
6b27d407 | 178 | * Pool information. |
08f3f44c | 179 | */ |
6b27d407 | 180 | readonly info: PoolInfo |
c4855468 JB |
181 | /** |
182 | * Pool worker nodes. | |
183 | */ | |
184 | readonly workerNodes: Array<WorkerNode<Worker, Data>> | |
b4904890 JB |
185 | /** |
186 | * Emitter on which events can be listened to. | |
187 | * | |
188 | * Events that can currently be listened to: | |
189 | * | |
164d950a JB |
190 | * - `'full'`: Emitted when the pool is dynamic and full. |
191 | * - `'busy'`: Emitted when the pool is busy. | |
91ee39ed JB |
192 | * - `'error'`: Emitted when an uncaught error occurs. |
193 | * - `'taskError'`: Emitted when an error occurs while executing a task. | |
b4904890 JB |
194 | */ |
195 | readonly emitter?: PoolEmitter | |
729c563d | 196 | /** |
61aa11a6 | 197 | * Executes the specified function in the worker constructor with the task data input parameter. |
729c563d | 198 | * |
e102732c | 199 | * @param data - The task input data for the specified worker function. This can only be structured-cloneable data. |
a86b6df1 | 200 | * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed. |
ef41a6e6 | 201 | * @returns Promise that will be fulfilled when the task is completed. |
729c563d | 202 | */ |
a86b6df1 | 203 | execute: (data?: Data, name?: string) => Promise<Response> |
280c2a77 | 204 | /** |
64383951 | 205 | * Terminates every current worker in this pool. |
280c2a77 | 206 | */ |
78cea37e | 207 | destroy: () => Promise<void> |
a35560ba | 208 | /** |
bdede008 | 209 | * Sets the worker choice strategy in this pool. |
a35560ba | 210 | * |
38e795c1 | 211 | * @param workerChoiceStrategy - The worker choice strategy. |
59219cbb | 212 | * @param workerChoiceStrategyOptions - The worker choice strategy options. |
a35560ba | 213 | */ |
59219cbb JB |
214 | setWorkerChoiceStrategy: ( |
215 | workerChoiceStrategy: WorkerChoiceStrategy, | |
216 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
217 | ) => void | |
a20f0ba5 JB |
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 | */ | |
8f52842f JB |
232 | enableTasksQueue: ( |
233 | enable: boolean, | |
234 | tasksQueueOptions?: TasksQueueOptions | |
235 | ) => void | |
a20f0ba5 JB |
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 | |
c97c7edb | 242 | } |