1 import type { EventEmitter
} from
'node:events'
2 import type { MessageChannel
, WorkerOptions
} from
'node:worker_threads'
4 import type { CircularBuffer
} from
'../circular-buffer.js'
5 import type { Task
, TaskFunctionProperties
} from
'../utility-types.js'
8 * Callback invoked when the worker has started successfully.
9 * @typeParam Worker - Type of worker.
11 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
14 * Callback invoked if the worker has received a message.
15 * @typeParam Worker - Type of worker.
17 export type MessageHandler
<Worker
extends IWorker
> = (
23 * Callback invoked if the worker raised an error.
24 * @typeParam Worker - Type of worker.
26 export type ErrorHandler
<Worker
extends IWorker
> = (
32 * Callback invoked when the worker exits successfully.
33 * @typeParam Worker - Type of worker.
35 export type ExitHandler
<Worker
extends IWorker
> = (
41 * Worker event handler.
42 * @typeParam Worker - Type of worker.
44 export type EventHandler
<Worker
extends IWorker
> =
45 | OnlineHandler
<Worker
>
46 | MessageHandler
<Worker
>
47 | ErrorHandler
<Worker
>
51 * Measurement history size.
53 export const MeasurementHistorySize
= 386
56 * Measurement statistics.
59 export interface MeasurementStatistics
{
61 * Measurement aggregate.
65 * Measurement minimum.
69 * Measurement maximum.
73 * Measurement average.
81 * Measurement history.
83 readonly history
: CircularBuffer
87 * Event loop utilization measurement statistics.
90 export interface EventLoopUtilizationMeasurementStatistics
{
91 readonly idle
: MeasurementStatistics
92 readonly active
: MeasurementStatistics
100 export interface TaskStatistics
{
102 * Number of executed tasks.
106 * Number of executing tasks.
110 * Number of queued tasks.
112 readonly queued
: number
114 * Maximum number of queued tasks.
116 readonly maxQueued
?: number
118 * Number of sequentially stolen tasks.
120 sequentiallyStolen
: number
122 * Number of stolen tasks.
126 * Number of failed tasks.
132 * Enumeration of worker types.
134 export const WorkerTypes
: Readonly
<{ thread
: 'thread'; cluster
: 'cluster' }> =
143 export type WorkerType
= keyof
typeof WorkerTypes
146 * Worker information.
149 export interface WorkerInfo
{
153 readonly id
: number | undefined
157 readonly type: WorkerType
168 * This flag is set to `true` when worker node is stealing tasks from another worker node.
172 * Back pressure flag.
173 * This flag is set to `true` when worker node tasks queue has back pressure.
175 backPressure
: boolean
177 * Task functions properties.
179 taskFunctionsProperties
?: TaskFunctionProperties
[]
183 * Worker usage statistics.
186 export interface WorkerUsage
{
190 readonly tasks
: TaskStatistics
192 * Tasks runtime statistics.
194 readonly runTime
: MeasurementStatistics
196 * Tasks wait time statistics.
198 readonly waitTime
: MeasurementStatistics
200 * Tasks event loop utilization statistics.
202 readonly elu
: EventLoopUtilizationMeasurementStatistics
206 * Worker choice strategy data.
209 export interface StrategyData
{
210 virtualTaskEndTimestamp
?: number
216 export interface IWorker
extends EventEmitter
{
222 * Worker thread worker id.
224 readonly threadId
?: number
226 * Registers an event handler.
227 * @param event - The event.
228 * @param handler - The event handler.
230 readonly on
: (event
: string, handler
: EventHandler
<this>) => this
232 * Registers once an event handler.
233 * @param event - The event.
234 * @param handler - The event handler.
236 readonly once
: (event
: string, handler
: EventHandler
<this>) => this
238 * Calling `unref()` on a worker allows the thread to exit if this is the only
239 * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
242 readonly unref
?: () => void
244 * Stop all JavaScript execution in the worker thread as soon as possible.
245 * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
247 readonly terminate
?: () => Promise
<number>
249 * Cluster worker disconnect.
251 readonly disconnect
?: () => void
253 * Cluster worker kill.
255 readonly kill
?: (signal
?: string) => void
259 * Worker node options.
262 export interface WorkerNodeOptions
{
263 workerOptions
?: WorkerOptions
264 env
?: Record
<string, unknown
>
265 tasksQueueBackPressureSize
: number | undefined
266 tasksQueueBucketSize
: number | undefined
267 tasksQueuePriority
: boolean | undefined
271 * Worker node interface.
272 * @typeParam Worker - Type of worker.
273 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
276 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
277 extends EventEmitter
{
281 readonly worker
: Worker
285 readonly info
: WorkerInfo
287 * Worker usage statistics.
289 readonly usage
: WorkerUsage
291 * Worker choice strategy data.
292 * This is used to store data that are specific to the worker choice strategy.
294 strategyData
?: StrategyData
296 * Message channel (worker thread only).
298 readonly messageChannel
?: MessageChannel
300 * Tasks queue back pressure size.
301 * This is the number of tasks that can be enqueued before the worker node has back pressure.
303 tasksQueueBackPressureSize
: number
305 * Sets tasks queue priority.
306 * @param enablePriority - Whether to enable tasks queue priority.
308 readonly setTasksQueuePriority
: (enablePriority
: boolean) => void
311 * @returns The tasks queue size.
313 readonly tasksQueueSize
: () => number
316 * @param task - The task to queue.
317 * @returns The tasks queue size.
319 readonly enqueueTask
: (task
: Task
<Data
>) => number
322 * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
323 * @returns The dequeued task.
325 readonly dequeueTask
: (bucket
?: number) => Task
<Data
> | undefined
327 * Dequeue last prioritized task.
328 * @returns The dequeued task.
330 readonly dequeueLastPrioritizedTask
: () => Task
<Data
> | undefined
332 * Clears tasks queue.
334 readonly clearTasksQueue
: () => void
336 * Whether the worker node has back pressure (i.e. its tasks queue is full).
337 * @returns `true` if the worker node has back pressure, `false` otherwise.
339 readonly hasBackPressure
: () => boolean
341 * Terminates the worker node.
343 readonly terminate
: () => Promise
<void>
345 * Registers a worker event handler.
346 * @param event - The event.
347 * @param handler - The event handler.
349 readonly registerWorkerEventHandler
: (
351 handler
: EventHandler
<Worker
>
354 * Registers once a worker event handler.
355 * @param event - The event.
356 * @param handler - The event handler.
358 readonly registerOnceWorkerEventHandler
: (
360 handler
: EventHandler
<Worker
>
363 * Gets task function worker usage statistics.
364 * @param name - The task function name.
365 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
367 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
369 * Deletes task function worker usage statistics.
370 * @param name - The task function name.
371 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
373 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
377 * Worker node event detail.
380 export interface WorkerNodeEventDetail
{
382 workerNodeKey
?: number