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 if the worker has received a message.
8 export type MessageHandler
<Worker
extends IWorker
> = (
14 * Callback invoked if the worker raised an error.
16 export type ErrorHandler
<Worker
extends IWorker
> = (
22 * Callback invoked when the worker has started successfully.
24 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
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 messageChannel
?: MessageChannel
151 * Worker usage statistics.
155 export interface WorkerUsage
{
159 readonly tasks
: TaskStatistics
161 * Tasks runtime statistics.
163 readonly runTime
: MeasurementStatistics
165 * Tasks wait time statistics.
167 readonly waitTime
: MeasurementStatistics
169 * Tasks event loop utilization statistics.
171 readonly elu
: EventLoopUtilizationMeasurementStatistics
177 export interface IWorker
{
182 readonly threadId
?: number
184 * Registers an event listener.
186 * @param event - The event.
187 * @param handler - The event handler.
189 readonly on
: ((event
: 'online', handler
: OnlineHandler
<this>) => void) &
190 ((event
: 'message', handler
: MessageHandler
<this>) => void) &
191 ((event
: 'error', handler
: ErrorHandler
<this>) => void) &
192 ((event
: 'exit', handler
: ExitHandler
<this>) => void)
194 * Registers a listener to the exit event that will only be performed once.
196 * @param event - `'exit'`.
197 * @param handler - The exit handler.
199 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
203 * Worker node interface.
205 * @typeParam Worker - Type of worker.
206 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
209 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
213 readonly worker
: Worker
217 readonly info
: WorkerInfo
219 * Worker usage statistics.
225 * @returns The tasks queue size.
227 readonly tasksQueueSize
: () => number
231 * @param task - The task to queue.
232 * @returns The task 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 * Resets usage statistics .
248 readonly resetUsage
: () => void
250 * Close communication channel.
252 readonly closeChannel
: () => void
254 * Gets task worker usage statistics.
256 readonly getTaskWorkerUsage
: (name
: string) => WorkerUsage
| undefined