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
} 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 * Task function names.
178 taskFunctionNames
?: string[]
182 * 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.
210 export interface StrategyData
{
211 virtualTaskEndTimestamp
?: number
217 export interface IWorker
extends EventEmitter
{
223 * Worker thread worker id.
225 readonly threadId
?: number
227 * Registers an event handler.
229 * @param event - The event.
230 * @param handler - The event handler.
232 readonly on
: (event
: string, handler
: EventHandler
<this>) => this
234 * Registers once an event handler.
236 * @param event - The event.
237 * @param handler - The event handler.
239 readonly once
: (event
: string, handler
: EventHandler
<this>) => this
241 * Calling `unref()` on a worker allows the thread to exit if this is the only
242 * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
245 readonly unref
?: () => void
247 * Stop all JavaScript execution in the worker thread as soon as possible.
248 * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
250 readonly terminate
?: () => Promise
<number>
252 * Cluster worker disconnect.
254 readonly disconnect
?: () => void
256 * Cluster worker kill.
258 readonly kill
?: (signal
?: string) => void
262 * Worker node options.
266 export interface WorkerNodeOptions
{
267 workerOptions
?: WorkerOptions
268 env
?: Record
<string, unknown
>
269 tasksQueueBackPressureSize
: number | undefined
273 * Worker node interface.
275 * @typeParam Worker - Type of worker.
276 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
279 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
280 extends EventEmitter
{
284 readonly worker
: Worker
288 readonly info
: WorkerInfo
290 * Worker usage statistics.
292 readonly usage
: WorkerUsage
294 * Worker choice strategy data.
295 * This is used to store data that are specific to the worker choice strategy.
297 strategyData
?: StrategyData
299 * Message channel (worker thread only).
301 readonly messageChannel
?: MessageChannel
303 * Tasks queue back pressure size.
304 * This is the number of tasks that can be enqueued before the worker node has back pressure.
306 tasksQueueBackPressureSize
: number
310 * @returns The tasks queue size.
312 readonly tasksQueueSize
: () => number
316 * @param task - The task to queue.
317 * @returns The tasks queue size.
319 readonly enqueueTask
: (task
: Task
<Data
>) => number
321 * Prepends a task to the tasks queue.
323 * @param task - The task to prepend.
324 * @returns The tasks queue size.
326 readonly unshiftTask
: (task
: Task
<Data
>) => number
330 * @returns The dequeued task.
332 readonly dequeueTask
: () => Task
<Data
> | undefined
334 * Pops a task from the tasks queue.
336 * @returns The popped task.
338 readonly popTask
: () => 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 * Resets usage statistics.
352 readonly resetUsage
: () => void
354 * Terminates the worker node.
356 readonly terminate
: () => Promise
<void>
358 * Registers a worker event handler.
360 * @param event - The event.
361 * @param handler - The event handler.
363 readonly registerWorkerEventHandler
: (
365 handler
: EventHandler
<Worker
>
368 * Registers once a worker event handler.
370 * @param event - The event.
371 * @param handler - The event handler.
373 readonly registerOnceWorkerEventHandler
: (
375 handler
: EventHandler
<Worker
>
378 * Gets task function worker usage statistics.
380 * @param name - The task function name.
381 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
383 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
385 * Deletes task function worker usage statistics.
387 * @param name - The task function name.
388 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
390 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
394 * Worker node event detail.
398 export interface WorkerNodeEventDetail
{
400 workerNodeKey
?: number