Commit | Line | Data |
---|---|---|
9e45c2c4 | 1 | import EventEmitterAsyncResource 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 | |
b4904890 JB |
34 | /** |
35 | * Pool events emitter. | |
36 | */ | |
9e45c2c4 | 37 | export class PoolEmitter extends EventEmitterAsyncResource {} |
b4904890 | 38 | |
aee46736 JB |
39 | /** |
40 | * Enumeration of pool events. | |
41 | */ | |
42 | export const PoolEvents = Object.freeze({ | |
43 | full: 'full', | |
1f68cede | 44 | busy: 'busy', |
91ee39ed JB |
45 | error: 'error', |
46 | taskError: 'taskError' | |
aee46736 JB |
47 | } as const) |
48 | ||
49 | /** | |
50 | * Pool event. | |
51 | */ | |
52 | export type PoolEvent = keyof typeof PoolEvents | |
53 | ||
6b27d407 JB |
54 | /** |
55 | * Pool information. | |
56 | */ | |
57 | export interface PoolInfo { | |
58 | type: PoolType | |
59 | minSize: number | |
60 | maxSize: number | |
61 | workerNodes: number | |
62 | idleWorkerNodes: number | |
63 | busyWorkerNodes: number | |
64 | runningTasks: number | |
65 | queuedTasks: number | |
66 | maxQueuedTasks: number | |
67 | } | |
68 | ||
7171d33f JB |
69 | /** |
70 | * Worker tasks queue options. | |
71 | */ | |
72 | export interface TasksQueueOptions { | |
73 | /** | |
74 | * Maximum number of tasks that can be executed concurrently on a worker. | |
75 | * | |
76 | * @defaultValue 1 | |
77 | */ | |
78 | concurrency?: number | |
79 | } | |
80 | ||
bdaf31cd JB |
81 | /** |
82 | * Options for a poolifier pool. | |
c319c66b | 83 | * |
d480d708 | 84 | * @typeParam Worker - Type of worker. |
bdaf31cd | 85 | */ |
50e66724 | 86 | export interface PoolOptions<Worker extends IWorker> { |
bdaf31cd JB |
87 | /** |
88 | * A function that will listen for message event on each worker. | |
89 | */ | |
90 | messageHandler?: MessageHandler<Worker> | |
91 | /** | |
92 | * A function that will listen for error event on each worker. | |
93 | */ | |
94 | errorHandler?: ErrorHandler<Worker> | |
95 | /** | |
96 | * A function that will listen for online event on each worker. | |
97 | */ | |
98 | onlineHandler?: OnlineHandler<Worker> | |
99 | /** | |
100 | * A function that will listen for exit event on each worker. | |
101 | */ | |
102 | exitHandler?: ExitHandler<Worker> | |
103 | /** | |
46e857ca | 104 | * The worker choice strategy to use in this pool. |
d29bce7c | 105 | * |
95ec6006 | 106 | * @defaultValue WorkerChoiceStrategies.ROUND_ROBIN |
bdaf31cd JB |
107 | */ |
108 | workerChoiceStrategy?: WorkerChoiceStrategy | |
da309861 JB |
109 | /** |
110 | * The worker choice strategy options. | |
111 | */ | |
112 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
1f68cede JB |
113 | /** |
114 | * Restart worker on error. | |
115 | */ | |
116 | restartWorkerOnError?: boolean | |
bdaf31cd JB |
117 | /** |
118 | * Pool events emission. | |
119 | * | |
38e795c1 | 120 | * @defaultValue true |
bdaf31cd JB |
121 | */ |
122 | enableEvents?: boolean | |
ff733df7 JB |
123 | /** |
124 | * Pool worker tasks queue. | |
125 | * | |
ff733df7 JB |
126 | * @defaultValue false |
127 | */ | |
128 | enableTasksQueue?: boolean | |
7171d33f JB |
129 | /** |
130 | * Pool worker tasks queue options. | |
7171d33f JB |
131 | */ |
132 | tasksQueueOptions?: TasksQueueOptions | |
bdaf31cd | 133 | } |
a35560ba | 134 | |
729c563d S |
135 | /** |
136 | * Contract definition for a poolifier pool. | |
137 | * | |
c4855468 | 138 | * @typeParam Worker - Type of worker which manages this pool. |
38e795c1 | 139 | * @typeParam Data - Type of data sent to the worker. This can only be serializable data. |
02706357 | 140 | * @typeParam Response - Type of execution response. This can only be serializable data. |
729c563d | 141 | */ |
c4855468 JB |
142 | export interface IPool< |
143 | Worker extends IWorker, | |
144 | Data = unknown, | |
145 | Response = unknown | |
146 | > { | |
147 | /** | |
148 | * Pool type. | |
149 | * | |
150 | * If it is `'dynamic'`, it provides the `max` property. | |
151 | */ | |
152 | readonly type: PoolType | |
08f3f44c | 153 | /** |
6b27d407 | 154 | * Pool information. |
08f3f44c | 155 | */ |
6b27d407 | 156 | readonly info: PoolInfo |
c4855468 JB |
157 | /** |
158 | * Pool worker nodes. | |
159 | */ | |
160 | readonly workerNodes: Array<WorkerNode<Worker, Data>> | |
b4904890 JB |
161 | /** |
162 | * Emitter on which events can be listened to. | |
163 | * | |
164 | * Events that can currently be listened to: | |
165 | * | |
164d950a JB |
166 | * - `'full'`: Emitted when the pool is dynamic and full. |
167 | * - `'busy'`: Emitted when the pool is busy. | |
91ee39ed JB |
168 | * - `'error'`: Emitted when an uncaught error occurs. |
169 | * - `'taskError'`: Emitted when an error occurs while executing a task. | |
b4904890 JB |
170 | */ |
171 | readonly emitter?: PoolEmitter | |
729c563d | 172 | /** |
61aa11a6 | 173 | * Executes the specified function in the worker constructor with the task data input parameter. |
729c563d | 174 | * |
ef41a6e6 | 175 | * @param data - The task input data for the specified worker function. This can only be serializable data. |
a86b6df1 | 176 | * @param name - The name of the worker function to execute. If not specified, the default worker function will be executed. |
ef41a6e6 | 177 | * @returns Promise that will be fulfilled when the task is completed. |
729c563d | 178 | */ |
a86b6df1 | 179 | execute: (data?: Data, name?: string) => Promise<Response> |
280c2a77 | 180 | /** |
675bb809 | 181 | * Shutdowns every current worker in this pool. |
280c2a77 | 182 | */ |
78cea37e | 183 | destroy: () => Promise<void> |
a35560ba | 184 | /** |
bdede008 | 185 | * Sets the worker choice strategy in this pool. |
a35560ba | 186 | * |
38e795c1 | 187 | * @param workerChoiceStrategy - The worker choice strategy. |
59219cbb | 188 | * @param workerChoiceStrategyOptions - The worker choice strategy options. |
a35560ba | 189 | */ |
59219cbb JB |
190 | setWorkerChoiceStrategy: ( |
191 | workerChoiceStrategy: WorkerChoiceStrategy, | |
192 | workerChoiceStrategyOptions?: WorkerChoiceStrategyOptions | |
193 | ) => void | |
a20f0ba5 JB |
194 | /** |
195 | * Sets the worker choice strategy options in this pool. | |
196 | * | |
197 | * @param workerChoiceStrategyOptions - The worker choice strategy options. | |
198 | */ | |
199 | setWorkerChoiceStrategyOptions: ( | |
200 | workerChoiceStrategyOptions: WorkerChoiceStrategyOptions | |
201 | ) => void | |
202 | /** | |
203 | * Enables/disables the worker tasks queue in this pool. | |
204 | * | |
205 | * @param enable - Whether to enable or disable the worker tasks queue. | |
206 | * @param tasksQueueOptions - The worker tasks queue options. | |
207 | */ | |
8f52842f JB |
208 | enableTasksQueue: ( |
209 | enable: boolean, | |
210 | tasksQueueOptions?: TasksQueueOptions | |
211 | ) => void | |
a20f0ba5 JB |
212 | /** |
213 | * Sets the worker tasks queue options in this pool. | |
214 | * | |
215 | * @param tasksQueueOptions - The worker tasks queue options. | |
216 | */ | |
217 | setTasksQueueOptions: (tasksQueueOptions: TasksQueueOptions) => void | |
c97c7edb | 218 | } |