1 import type { MessageChannel
} from
'node:worker_threads'
2 import type { CircularArray
} from
'../circular-array'
3 import type { Task
} from
'../utility-types'
6 * Callback invoked when the worker has started successfully.
8 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
11 * Callback invoked if the worker has received a message.
13 export type MessageHandler
<Worker
extends IWorker
> = (
19 * Callback invoked if the worker raised an error.
21 export type ErrorHandler
<Worker
extends IWorker
> = (
27 * Callback invoked when the worker exits successfully.
29 export type ExitHandler
<Worker
extends IWorker
> = (
35 * Measurement statistics.
39 export interface MeasurementStatistics
{
41 * Measurement aggregate.
45 * Measurement minimum.
49 * Measurement maximum.
53 * Measurement average.
61 * Measurement history.
63 readonly history
: CircularArray
<number>
67 * Event loop utilization measurement statistics.
71 export interface EventLoopUtilizationMeasurementStatistics
{
72 readonly idle
: MeasurementStatistics
73 readonly active
: MeasurementStatistics
82 export interface TaskStatistics
{
84 * Number of executed tasks.
88 * Number of executing tasks.
92 * Number of queued tasks.
94 readonly queued
: number
96 * Maximum number of queued tasks.
98 readonly maxQueued
?: number
100 * Number of failed tasks.
106 * Enumeration of worker types.
108 export const WorkerTypes
= Object.freeze({
116 export type WorkerType
= keyof
typeof WorkerTypes
119 * Worker information.
123 export interface WorkerInfo
{
127 readonly id
: number | undefined
141 * Task function names.
143 taskFunctions
?: string[]
147 * Worker usage statistics.
151 export interface WorkerUsage
{
155 readonly tasks
: TaskStatistics
157 * Tasks runtime statistics.
159 readonly runTime
: MeasurementStatistics
161 * Tasks wait time statistics.
163 readonly waitTime
: MeasurementStatistics
165 * Tasks event loop utilization statistics.
167 readonly elu
: EventLoopUtilizationMeasurementStatistics
173 export interface IWorker
{
178 readonly threadId
?: number
180 * Registers an event listener.
182 * @param event - The event.
183 * @param handler - The event handler.
185 readonly on
: ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
186 ((event
: 'message', handler
: MessageHandler
<this>) => void) &
187 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
188 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
190 * Registers a listener to the exit event that will only be performed once.
192 * @param event - `'exit'`.
193 * @param handler - The exit handler.
195 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
199 * Worker node interface.
201 * @typeParam Worker - Type of worker.
202 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
205 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
209 readonly worker
: Worker
213 readonly info
: WorkerInfo
217 readonly messageChannel
?: MessageChannel
219 * Worker usage statistics.
225 * @returns The tasks queue size.
227 readonly tasksQueueSize
: () => number
231 * @param task - The task to queue.
232 * @returns The tasks queue size.
234 readonly enqueueTask
: (task
: Task
<Data
>) => number
238 * @returns The dequeued task.
240 readonly dequeueTask
: () => Task
<Data
> | undefined
242 * Clears tasks queue.
244 readonly clearTasksQueue
: () => void
246 * Whether the worker node has back pressure (i.e. its tasks queue is full).
248 * @returns `true` if the worker node has back pressure, `false` otherwise.
250 readonly hasBackPressure
: () => boolean
252 * Resets usage statistics.
254 readonly resetUsage
: () => void
256 * Closes communication channel.
258 readonly closeChannel
: () => void
260 * Gets task function worker usage statistics.
262 * @param name - The task function name.
263 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
265 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined