1 import type { EventEmitter
} from
'node:events'
2 import type { MessageChannel
, WorkerOptions
} from
'node:worker_threads'
4 import type { CircularArray
} from
'../circular-array.js'
5 import type { Task
, TaskFunctionProperties
} from
'../utility-types.js'
8 * Callback invoked when the worker has started successfully.
10 * @typeParam Worker - Type of worker.
12 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
15 * Callback invoked if the worker has received a message.
17 * @typeParam Worker - Type of worker.
19 export type MessageHandler
<Worker
extends IWorker
> = (
25 * Callback invoked if the worker raised an error.
27 * @typeParam Worker - Type of worker.
29 export type ErrorHandler
<Worker
extends IWorker
> = (
35 * Callback invoked when the worker exits successfully.
37 * @typeParam Worker - Type of worker.
39 export type ExitHandler
<Worker
extends IWorker
> = (
45 * Worker event handler.
47 * @typeParam Worker - Type of worker.
49 export type EventHandler
<Worker
extends IWorker
> =
50 | OnlineHandler
<Worker
>
51 | MessageHandler
<Worker
>
52 | ErrorHandler
<Worker
>
56 * Measurement statistics.
60 export interface MeasurementStatistics
{
62 * Measurement aggregate.
66 * Measurement minimum.
70 * Measurement maximum.
74 * Measurement average.
82 * Measurement history.
84 readonly history
: CircularArray
<number>
88 * Event loop utilization measurement statistics.
92 export interface EventLoopUtilizationMeasurementStatistics
{
93 readonly idle
: MeasurementStatistics
94 readonly active
: MeasurementStatistics
103 export interface TaskStatistics
{
105 * Number of executed tasks.
109 * Number of executing tasks.
113 * Number of queued tasks.
115 readonly queued
: number
117 * Maximum number of queued tasks.
119 readonly maxQueued
?: number
121 * Number of sequentially stolen tasks.
123 sequentiallyStolen
: number
125 * Number of stolen tasks.
129 * Number of failed tasks.
135 * Enumeration of worker types.
137 export const WorkerTypes
: Readonly
<{ thread
: 'thread', cluster
: 'cluster' }> =
146 export type WorkerType
= keyof
typeof WorkerTypes
149 * Worker information.
153 export interface WorkerInfo
{
157 readonly id
: number | undefined
161 readonly type: WorkerType
172 * This flag is set to `true` when worker node is stealing tasks from another worker node.
176 * Back pressure flag.
177 * This flag is set to `true` when worker node tasks queue has back pressure.
179 backPressure
: boolean
181 * Task functions properties.
183 taskFunctionsProperties
?: TaskFunctionProperties
[]
187 * Worker usage statistics.
191 export interface WorkerUsage
{
195 readonly tasks
: TaskStatistics
197 * Tasks runtime statistics.
199 readonly runTime
: MeasurementStatistics
201 * Tasks wait time statistics.
203 readonly waitTime
: MeasurementStatistics
205 * Tasks event loop utilization statistics.
207 readonly elu
: EventLoopUtilizationMeasurementStatistics
211 * Worker choice strategy data.
215 export interface StrategyData
{
216 virtualTaskEndTimestamp
?: number
222 export interface IWorker
extends EventEmitter
{
228 * Worker thread worker id.
230 readonly threadId
?: number
232 * Registers an event handler.
234 * @param event - The event.
235 * @param handler - The event handler.
237 readonly on
: (event
: string, handler
: EventHandler
<this>) => this
239 * Registers once an event handler.
241 * @param event - The event.
242 * @param handler - The event handler.
244 readonly once
: (event
: string, handler
: EventHandler
<this>) => this
246 * Calling `unref()` on a worker allows the thread to exit if this is the only
247 * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
250 readonly unref
?: () => void
252 * Stop all JavaScript execution in the worker thread as soon as possible.
253 * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
255 readonly terminate
?: () => Promise
<number>
257 * Cluster worker disconnect.
259 readonly disconnect
?: () => void
261 * Cluster worker kill.
263 readonly kill
?: (signal
?: string) => void
267 * Worker node options.
271 export interface WorkerNodeOptions
{
272 workerOptions
?: WorkerOptions
273 env
?: Record
<string, unknown
>
274 tasksQueueBackPressureSize
: number | undefined
275 tasksQueueBucketSize
: number | undefined
279 * Worker node interface.
281 * @typeParam Worker - Type of worker.
282 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
285 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
286 extends EventEmitter
{
290 readonly worker
: Worker
294 readonly info
: WorkerInfo
296 * Worker usage statistics.
298 readonly usage
: WorkerUsage
300 * Worker choice strategy data.
301 * This is used to store data that are specific to the worker choice strategy.
303 strategyData
?: StrategyData
305 * Message channel (worker thread only).
307 readonly messageChannel
?: MessageChannel
309 * Tasks queue back pressure size.
310 * This is the number of tasks that can be enqueued before the worker node has back pressure.
312 tasksQueueBackPressureSize
: number
316 * @returns The tasks queue size.
318 readonly tasksQueueSize
: () => number
322 * @param task - The task to queue.
323 * @returns The tasks queue size.
325 readonly enqueueTask
: (task
: Task
<Data
>) => number
329 * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
330 * @returns The dequeued task.
332 readonly dequeueTask
: (bucket
?: number) => Task
<Data
> | undefined
334 * Dequeue last prioritized task.
336 * @returns The dequeued task.
338 readonly dequeueLastPrioritizedTask
: () => Task
<Data
> | undefined
340 * Clears tasks queue.
342 readonly clearTasksQueue
: () => void
344 * Whether the worker node has back pressure (i.e. its tasks queue is full).
346 * @returns `true` if the worker node has back pressure, `false` otherwise.
348 readonly hasBackPressure
: () => boolean
350 * Terminates the worker node.
352 readonly terminate
: () => Promise
<void>
354 * Registers a worker event handler.
356 * @param event - The event.
357 * @param handler - The event handler.
359 readonly registerWorkerEventHandler
: (
361 handler
: EventHandler
<Worker
>
364 * Registers once a worker event handler.
366 * @param event - The event.
367 * @param handler - The event handler.
369 readonly registerOnceWorkerEventHandler
: (
371 handler
: EventHandler
<Worker
>
374 * Gets task function worker usage statistics.
376 * @param name - The task function name.
377 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
379 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
381 * Deletes task function worker usage statistics.
383 * @param name - The task function name.
384 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
386 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
390 * Worker node event detail.
394 export interface WorkerNodeEventDetail
{
396 workerNodeKey
?: number