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 stolen tasks.
104 * Number of failed tasks.
110 * Enumeration of worker types.
112 export const WorkerTypes
= Object.freeze({
120 export type WorkerType
= keyof
typeof WorkerTypes
123 * Worker information.
127 export interface WorkerInfo
{
131 readonly id
: number | undefined
135 readonly type: WorkerType
145 * Task function names.
147 taskFunctions
?: string[]
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 - The `'exit'` event.
197 * @param handler - The exit handler.
199 readonly once
: (event
: 'exit', handler
: ExitHandler
<this>) => void
203 * Worker node event callback.
205 * @param workerId - The worker id.
208 export type WorkerNodeEventCallback
= (workerId
: number) => void
211 * Worker node interface.
213 * @typeParam Worker - Type of worker.
214 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
217 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
> {
221 readonly worker
: Worker
225 readonly info
: WorkerInfo
227 * Worker usage statistics.
229 readonly usage
: WorkerUsage
231 * Message channel (worker_threads only).
233 readonly messageChannel
?: MessageChannel
235 * Tasks queue back pressure size.
236 * This is the number of tasks that can be enqueued before the worker node has back pressure.
238 tasksQueueBackPressureSize
: number
240 * Callback invoked when worker node tasks queue is back pressured.
242 onBackPressure
?: WorkerNodeEventCallback
244 * Callback invoked when worker node tasks queue is empty.
246 onEmptyQueue
?: WorkerNodeEventCallback
250 * @returns The tasks queue size.
252 readonly tasksQueueSize
: () => number
256 * @param task - The task to queue.
257 * @returns The tasks queue size.
259 readonly enqueueTask
: (task
: Task
<Data
>) => number
261 * Prepends a task to the tasks queue.
263 * @param task - The task to prepend.
264 * @returns The tasks queue size.
266 readonly unshiftTask
: (task
: Task
<Data
>) => number
270 * @returns The dequeued task.
272 readonly dequeueTask
: () => Task
<Data
> | undefined
274 * Pops a task from the tasks queue.
276 * @returns The popped task.
278 readonly popTask
: () => Task
<Data
> | undefined
280 * Clears tasks queue.
282 readonly clearTasksQueue
: () => void
284 * Whether the worker node has back pressure (i.e. its tasks queue is full).
286 * @returns `true` if the worker node has back pressure, `false` otherwise.
288 readonly hasBackPressure
: () => boolean
290 * Resets usage statistics.
292 readonly resetUsage
: () => void
294 * Closes communication channel.
296 readonly closeChannel
: () => void
298 * Gets task function worker usage statistics.
300 * @param name - The task function name.
301 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
303 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined