]>
Commit | Line | Data |
---|---|---|
e1c2dba7 | 1 | import type { EventEmitter } from 'node:events' |
ded253e2 JB |
2 | import type { MessageChannel, WorkerOptions } from 'node:worker_threads' |
3 | ||
f12182ad | 4 | import type { CircularBuffer } from '../circular-buffer.js' |
f4289ecb | 5 | import type { PriorityQueue } from '../queues/priority-queue.js' |
31847469 | 6 | import type { Task, TaskFunctionProperties } from '../utility-types.js' |
f06e48d8 | 7 | |
671d5154 | 8 | /** |
5de88a2a | 9 | * Callback invoked if the worker raised an error. |
09b75fef | 10 | * @typeParam Worker - Type of worker. |
671d5154 | 11 | */ |
5de88a2a JB |
12 | export type ErrorHandler<Worker extends IWorker> = ( |
13 | this: Worker, | |
14 | error: Error | |
15 | ) => void | |
671d5154 | 16 | |
bdaf31cd | 17 | /** |
5de88a2a | 18 | * Worker event handler. |
09b75fef | 19 | * @typeParam Worker - Type of worker. |
bdaf31cd | 20 | */ |
5de88a2a JB |
21 | export type EventHandler<Worker extends IWorker> = |
22 | | ErrorHandler<Worker> | |
23 | | ExitHandler<Worker> | |
24 | | MessageHandler<Worker> | |
25 | | OnlineHandler<Worker> | |
bdaf31cd JB |
26 | |
27 | /** | |
5de88a2a | 28 | * Callback invoked when the worker exits successfully. |
09b75fef | 29 | * @typeParam Worker - Type of worker. |
bdaf31cd | 30 | */ |
5de88a2a | 31 | export type ExitHandler<Worker extends IWorker> = ( |
50e66724 | 32 | this: Worker, |
5de88a2a | 33 | exitCode: number |
50e66724 | 34 | ) => void |
bdaf31cd | 35 | |
bdaf31cd | 36 | /** |
5de88a2a | 37 | * Callback invoked if the worker has received a message. |
09b75fef | 38 | * @typeParam Worker - Type of worker. |
bdaf31cd | 39 | */ |
5de88a2a | 40 | export type MessageHandler<Worker extends IWorker> = ( |
50e66724 | 41 | this: Worker, |
5de88a2a | 42 | message: unknown |
50e66724 | 43 | ) => void |
bdaf31cd | 44 | |
3bcbd4c5 | 45 | /** |
5de88a2a | 46 | * Callback invoked when the worker has started successfully. |
3bcbd4c5 JB |
47 | * @typeParam Worker - Type of worker. |
48 | */ | |
5de88a2a | 49 | export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void |
3bcbd4c5 | 50 | |
f12182ad JB |
51 | /** |
52 | * Measurement history size. | |
53 | */ | |
54 | export const MeasurementHistorySize = 386 | |
55 | ||
5de88a2a JB |
56 | /** |
57 | * Event loop utilization measurement statistics. | |
58 | * @internal | |
59 | */ | |
60 | export interface EventLoopUtilizationMeasurementStatistics { | |
61 | readonly active: MeasurementStatistics | |
62 | readonly idle: MeasurementStatistics | |
63 | utilization?: number | |
64 | } | |
65 | ||
f06e48d8 | 66 | /** |
cd4d348a | 67 | * Measurement statistics. |
f9b4bbf8 | 68 | * @internal |
f06e48d8 | 69 | */ |
cd4d348a | 70 | export interface MeasurementStatistics { |
02706357 | 71 | /** |
932fc8be | 72 | * Measurement aggregate. |
02706357 | 73 | */ |
71514351 | 74 | aggregate?: number |
f7510105 | 75 | /** |
97231086 | 76 | * Measurement average. |
f7510105 | 77 | */ |
97231086 | 78 | average?: number |
f7510105 | 79 | /** |
97231086 | 80 | * Measurement history. |
f7510105 | 81 | */ |
97231086 | 82 | readonly history: CircularBuffer |
02706357 | 83 | /** |
97231086 | 84 | * Measurement maximum. |
02706357 | 85 | */ |
97231086 | 86 | maximum?: number |
02706357 | 87 | /** |
cd4d348a | 88 | * Measurement median. |
02706357 | 89 | */ |
71514351 | 90 | median?: number |
02706357 | 91 | /** |
97231086 | 92 | * Measurement minimum. |
02706357 | 93 | */ |
97231086 | 94 | minimum?: number |
a4e07f72 JB |
95 | } |
96 | ||
97 | /** | |
98 | * Task statistics. | |
a4e07f72 JB |
99 | * @internal |
100 | */ | |
a4e07f72 | 101 | export interface TaskStatistics { |
02706357 | 102 | /** |
9a0613e9 | 103 | * Number of executed tasks. |
02706357 | 104 | */ |
a4e07f72 | 105 | executed: number |
02706357 | 106 | /** |
9a0613e9 | 107 | * Number of executing tasks. |
02706357 | 108 | */ |
a4e07f72 | 109 | executing: number |
0567595a | 110 | /** |
97231086 | 111 | * Number of failed tasks. |
0567595a | 112 | */ |
97231086 | 113 | failed: number |
df593701 JB |
114 | /** |
115 | * Maximum number of queued tasks. | |
116 | */ | |
b25a42cd | 117 | readonly maxQueued?: number |
97231086 JB |
118 | /** |
119 | * Number of queued tasks. | |
120 | */ | |
121 | readonly queued: number | |
463226a4 JB |
122 | /** |
123 | * Number of sequentially stolen tasks. | |
124 | */ | |
125 | sequentiallyStolen: number | |
68cbdc84 JB |
126 | /** |
127 | * Number of stolen tasks. | |
128 | */ | |
129 | stolen: number | |
a4e07f72 JB |
130 | } |
131 | ||
4b628b48 JB |
132 | /** |
133 | * Enumeration of worker types. | |
134 | */ | |
97231086 | 135 | export const WorkerTypes: Readonly<{ cluster: 'cluster'; thread: 'thread' }> = |
59776ec5 | 136 | Object.freeze({ |
3a502712 | 137 | cluster: 'cluster', |
97231086 | 138 | thread: 'thread', |
59776ec5 | 139 | } as const) |
4b628b48 | 140 | |
f06e48d8 JB |
141 | /** |
142 | * Worker interface. | |
143 | */ | |
a4791fb1 | 144 | export interface IWorker extends EventEmitter { |
97231086 JB |
145 | /** |
146 | * Cluster worker disconnect. | |
147 | */ | |
148 | readonly disconnect?: () => void | |
f59e1027 | 149 | /** |
07e0c9e5 | 150 | * Cluster worker id. |
f59e1027 | 151 | */ |
aecc6e48 | 152 | readonly id?: number |
07e0c9e5 | 153 | /** |
97231086 | 154 | * Cluster worker kill. |
07e0c9e5 | 155 | */ |
97231086 | 156 | readonly kill?: (signal?: string) => void |
bdaf31cd | 157 | /** |
88af9bf1 | 158 | * Registers an event handler. |
38e795c1 | 159 | * @param event - The event. |
48ef9107 | 160 | * @param handler - The event handler. |
bdaf31cd | 161 | */ |
a4791fb1 | 162 | readonly on: (event: string, handler: EventHandler<this>) => this |
c3719753 | 163 | /** |
88af9bf1 | 164 | * Registers once an event handler. |
c3719753 JB |
165 | * @param event - The event. |
166 | * @param handler - The event handler. | |
bdaf31cd | 167 | */ |
a4791fb1 | 168 | readonly once: (event: string, handler: EventHandler<this>) => this |
07e0c9e5 JB |
169 | /** |
170 | * Stop all JavaScript execution in the worker thread as soon as possible. | |
171 | * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted. | |
172 | */ | |
173 | readonly terminate?: () => Promise<number> | |
174 | /** | |
97231086 | 175 | * Worker thread worker id. |
07e0c9e5 | 176 | */ |
97231086 | 177 | readonly threadId?: number |
07e0c9e5 | 178 | /** |
97231086 JB |
179 | * Calling `unref()` on a worker allows the thread to exit if this is the only |
180 | * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect. | |
181 | * @since v10.5.0 | |
07e0c9e5 | 182 | */ |
97231086 | 183 | readonly unref?: () => void |
bdaf31cd | 184 | } |
f06e48d8 JB |
185 | |
186 | /** | |
187 | * Worker node interface. | |
c319c66b | 188 | * @typeParam Worker - Type of worker. |
e102732c | 189 | * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. |
c319c66b | 190 | * @internal |
f06e48d8 | 191 | */ |
9f95d5eb | 192 | export interface IWorkerNode<Worker extends IWorker, Data = unknown> |
e1c2dba7 | 193 | extends EventEmitter { |
c319c66b | 194 | /** |
97231086 | 195 | * Clears tasks queue. |
fcfc3353 | 196 | */ |
97231086 | 197 | readonly clearTasksQueue: () => void |
f4289ecb JB |
198 | /** |
199 | * Deletes a task from the tasks queue. | |
200 | * @param task - The task to delete. | |
201 | * @returns `true` if the task was deleted, `false` otherwise. | |
202 | */ | |
203 | readonly deleteTask: (task: Task<Data>) => boolean | |
c319c66b | 204 | /** |
97231086 JB |
205 | * Deletes task function worker usage statistics. |
206 | * @param name - The task function name. | |
207 | * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise. | |
4b628b48 | 208 | */ |
97231086 | 209 | readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean |
4b628b48 | 210 | /** |
97231086 JB |
211 | * Dequeue last prioritized task. |
212 | * @returns The dequeued task. | |
4b628b48 | 213 | */ |
97231086 | 214 | readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined |
4b628b48 | 215 | /** |
ccd73e07 | 216 | * Dequeue task. |
95d1a734 | 217 | * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0 |
4b628b48 JB |
218 | * @returns The dequeued task. |
219 | */ | |
95d1a734 | 220 | readonly dequeueTask: (bucket?: number) => Task<Data> | undefined |
0d4e88b3 | 221 | /** |
97231086 JB |
222 | * Enqueue task. |
223 | * @param task - The task to queue. | |
224 | * @returns The tasks queue size. | |
0d4e88b3 | 225 | */ |
97231086 | 226 | readonly enqueueTask: (task: Task<Data>) => number |
4b628b48 | 227 | /** |
97231086 JB |
228 | * Gets task function worker usage statistics. |
229 | * @param name - The task function name. | |
230 | * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise. | |
4b628b48 | 231 | */ |
97231086 | 232 | readonly getTaskFunctionWorkerUsage: (name: string) => undefined | WorkerUsage |
3f09ed9f | 233 | /** |
97231086 | 234 | * Worker info. |
3f09ed9f | 235 | */ |
97231086 | 236 | readonly info: WorkerInfo |
c3719753 | 237 | /** |
97231086 JB |
238 | * Message channel (worker thread only). |
239 | */ | |
240 | readonly messageChannel?: MessageChannel | |
241 | /** | |
242 | * Registers once a worker event handler. | |
c3719753 | 243 | * @param event - The event. |
88af9bf1 | 244 | * @param handler - The event handler. |
c3719753 | 245 | */ |
97231086 | 246 | readonly registerOnceWorkerEventHandler: ( |
c3719753 | 247 | event: string, |
3bcbd4c5 | 248 | handler: EventHandler<Worker> |
c3719753 JB |
249 | ) => void |
250 | /** | |
97231086 | 251 | * Registers a worker event handler. |
c3719753 | 252 | * @param event - The event. |
88af9bf1 | 253 | * @param handler - The event handler. |
c3719753 | 254 | */ |
97231086 | 255 | readonly registerWorkerEventHandler: ( |
c3719753 | 256 | event: string, |
3bcbd4c5 | 257 | handler: EventHandler<Worker> |
c3719753 | 258 | ) => void |
ff128cc9 | 259 | /** |
97231086 JB |
260 | * Sets tasks queue priority. |
261 | * @param enablePriority - Whether to enable tasks queue priority. | |
ff128cc9 | 262 | */ |
97231086 | 263 | readonly setTasksQueuePriority: (enablePriority: boolean) => void |
adee6053 | 264 | /** |
97231086 JB |
265 | * Worker choice strategy data. |
266 | * This is used to store data that are specific to the worker choice strategy. | |
adee6053 | 267 | */ |
97231086 | 268 | strategyData?: StrategyData |
f4289ecb JB |
269 | /** |
270 | * Tasks queue. | |
271 | */ | |
272 | readonly tasksQueue: PriorityQueue<Task<Data>> | |
97231086 JB |
273 | /** |
274 | * Tasks queue back pressure size. | |
275 | * This is the number of tasks that can be enqueued before the worker node has back pressure. | |
276 | */ | |
277 | tasksQueueBackPressureSize: number | |
278 | /** | |
279 | * Tasks queue size. | |
280 | * @returns The tasks queue size. | |
281 | */ | |
282 | readonly tasksQueueSize: () => number | |
283 | /** | |
284 | * Terminates the worker node. | |
285 | */ | |
286 | readonly terminate: () => Promise<void> | |
287 | /** | |
288 | * Worker usage statistics. | |
289 | */ | |
290 | readonly usage: WorkerUsage | |
291 | /** | |
292 | * Worker. | |
293 | */ | |
294 | readonly worker: Worker | |
f06e48d8 | 295 | } |
c3719753 | 296 | |
5de88a2a JB |
297 | /** |
298 | * Worker choice strategy data. | |
299 | * @internal | |
300 | */ | |
301 | export interface StrategyData { | |
302 | virtualTaskEndTimestamp?: number | |
303 | } | |
304 | ||
305 | /** | |
306 | * Worker information. | |
307 | * @internal | |
308 | */ | |
309 | export interface WorkerInfo { | |
310 | /** | |
311 | * Back pressure flag. | |
312 | * This flag is set to `true` when worker node tasks queue is back pressured. | |
313 | */ | |
314 | backPressure: boolean | |
315 | /** | |
316 | * Back pressure stealing flag. | |
317 | * This flag is set to `true` when worker node is stealing one task from another back pressured worker node. | |
318 | */ | |
319 | backPressureStealing: boolean | |
320 | /** | |
321 | * Continuous stealing flag. | |
322 | * This flag is set to `true` when worker node is continuously stealing tasks from other worker nodes. | |
323 | */ | |
324 | continuousStealing: boolean | |
325 | /** | |
326 | * Dynamic flag. | |
327 | */ | |
328 | dynamic: boolean | |
329 | /** | |
330 | * Worker id. | |
331 | */ | |
332 | readonly id: number | undefined | |
f4289ecb JB |
333 | /** |
334 | * Queued task abortion flag. | |
335 | * This flag is set to `true` when worker node is aborting a queued task. | |
336 | */ | |
337 | queuedTaskAbortion: boolean | |
5de88a2a JB |
338 | /** |
339 | * Ready flag. | |
340 | */ | |
341 | ready: boolean | |
342 | /** | |
343 | * Stealing flag. | |
344 | * This flag is set to `true` when worker node is stealing one task from another worker node. | |
345 | */ | |
346 | stealing: boolean | |
347 | /** | |
348 | * Stolen flag. | |
349 | * This flag is set to `true` when worker node has one task stolen from another worker node. | |
350 | */ | |
351 | stolen: boolean | |
352 | /** | |
353 | * Task functions properties. | |
354 | */ | |
355 | taskFunctionsProperties?: TaskFunctionProperties[] | |
356 | /** | |
357 | * Worker type. | |
358 | */ | |
359 | readonly type: WorkerType | |
360 | } | |
361 | ||
c3719753 JB |
362 | /** |
363 | * Worker node event detail. | |
c3719753 JB |
364 | * @internal |
365 | */ | |
366 | export interface WorkerNodeEventDetail { | |
f4289ecb | 367 | taskId?: `${string}-${string}-${string}-${string}-${string}` |
3a20a1a1 | 368 | workerId?: number |
c3719753 JB |
369 | workerNodeKey?: number |
370 | } | |
5de88a2a JB |
371 | |
372 | /** | |
373 | * Worker node options. | |
374 | * @internal | |
375 | */ | |
376 | export interface WorkerNodeOptions { | |
377 | env?: Record<string, unknown> | |
378 | tasksQueueBackPressureSize: number | undefined | |
379 | tasksQueueBucketSize: number | undefined | |
380 | tasksQueuePriority: boolean | undefined | |
381 | workerOptions?: WorkerOptions | |
382 | } | |
383 | ||
384 | /** | |
385 | * Worker type. | |
386 | */ | |
387 | export type WorkerType = keyof typeof WorkerTypes | |
388 | ||
389 | /** | |
390 | * Worker usage statistics. | |
391 | * @internal | |
392 | */ | |
393 | export interface WorkerUsage { | |
394 | /** | |
395 | * Tasks event loop utilization statistics. | |
396 | */ | |
397 | readonly elu: EventLoopUtilizationMeasurementStatistics | |
398 | /** | |
399 | * Tasks runtime statistics. | |
400 | */ | |
401 | readonly runTime: MeasurementStatistics | |
402 | /** | |
403 | * Tasks statistics. | |
404 | */ | |
405 | readonly tasks: TaskStatistics | |
406 | /** | |
407 | * Tasks wait time statistics. | |
408 | */ | |
409 | readonly waitTime: MeasurementStatistics | |
410 | } |