1 import type { EventEmitter
} from
'node:events'
2 import type { MessageChannel
, WorkerOptions
} from
'node:worker_threads'
4 import type { CircularBuffer
} from
'../circular-buffer.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 history size.
58 export const MeasurementHistorySize
= 386
61 * Measurement statistics.
65 export interface MeasurementStatistics
{
67 * Measurement aggregate.
71 * Measurement minimum.
75 * Measurement maximum.
79 * Measurement average.
87 * Measurement history.
89 readonly history
: CircularBuffer
<number>
93 * Event loop utilization measurement statistics.
97 export interface EventLoopUtilizationMeasurementStatistics
{
98 readonly idle
: MeasurementStatistics
99 readonly active
: MeasurementStatistics
108 export interface TaskStatistics
{
110 * Number of executed tasks.
114 * Number of executing tasks.
118 * Number of queued tasks.
120 readonly queued
: number
122 * Maximum number of queued tasks.
124 readonly maxQueued
?: number
126 * Number of sequentially stolen tasks.
128 sequentiallyStolen
: number
130 * Number of stolen tasks.
134 * Number of failed tasks.
140 * Enumeration of worker types.
142 export const WorkerTypes
: Readonly
<{ thread
: 'thread', cluster
: 'cluster' }> =
151 export type WorkerType
= keyof
typeof WorkerTypes
154 * Worker information.
158 export interface WorkerInfo
{
162 readonly id
: number | undefined
166 readonly type: WorkerType
177 * This flag is set to `true` when worker node is stealing tasks from another worker node.
181 * Back pressure flag.
182 * This flag is set to `true` when worker node tasks queue has back pressure.
184 backPressure
: boolean
186 * Task functions properties.
188 taskFunctionsProperties
?: TaskFunctionProperties
[]
192 * Worker usage statistics.
196 export interface WorkerUsage
{
200 readonly tasks
: TaskStatistics
202 * Tasks runtime statistics.
204 readonly runTime
: MeasurementStatistics
206 * Tasks wait time statistics.
208 readonly waitTime
: MeasurementStatistics
210 * Tasks event loop utilization statistics.
212 readonly elu
: EventLoopUtilizationMeasurementStatistics
216 * Worker choice strategy data.
220 export interface StrategyData
{
221 virtualTaskEndTimestamp
?: number
227 export interface IWorker
extends EventEmitter
{
233 * Worker thread worker id.
235 readonly threadId
?: number
237 * Registers an event handler.
239 * @param event - The event.
240 * @param handler - The event handler.
242 readonly on
: (event
: string, handler
: EventHandler
<this>) => this
244 * Registers once an event handler.
246 * @param event - The event.
247 * @param handler - The event handler.
249 readonly once
: (event
: string, handler
: EventHandler
<this>) => this
251 * Calling `unref()` on a worker allows the thread to exit if this is the only
252 * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
255 readonly unref
?: () => void
257 * Stop all JavaScript execution in the worker thread as soon as possible.
258 * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
260 readonly terminate
?: () => Promise
<number>
262 * Cluster worker disconnect.
264 readonly disconnect
?: () => void
266 * Cluster worker kill.
268 readonly kill
?: (signal
?: string) => void
272 * Worker node options.
276 export interface WorkerNodeOptions
{
277 workerOptions
?: WorkerOptions
278 env
?: Record
<string, unknown
>
279 tasksQueueBackPressureSize
: number | undefined
280 tasksQueueBucketSize
: number | undefined
284 * Worker node interface.
286 * @typeParam Worker - Type of worker.
287 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
290 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
291 extends EventEmitter
{
295 readonly worker
: Worker
299 readonly info
: WorkerInfo
301 * Worker usage statistics.
303 readonly usage
: WorkerUsage
305 * Worker choice strategy data.
306 * This is used to store data that are specific to the worker choice strategy.
308 strategyData
?: StrategyData
310 * Message channel (worker thread only).
312 readonly messageChannel
?: MessageChannel
314 * Tasks queue back pressure size.
315 * This is the number of tasks that can be enqueued before the worker node has back pressure.
317 tasksQueueBackPressureSize
: number
321 * @returns The tasks queue size.
323 readonly tasksQueueSize
: () => number
327 * @param task - The task to queue.
328 * @returns The tasks queue size.
330 readonly enqueueTask
: (task
: Task
<Data
>) => number
334 * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
335 * @returns The dequeued task.
337 readonly dequeueTask
: (bucket
?: number) => Task
<Data
> | undefined
339 * Dequeue last prioritized task.
341 * @returns The dequeued task.
343 readonly dequeueLastPrioritizedTask
: () => Task
<Data
> | undefined
345 * Clears tasks queue.
347 readonly clearTasksQueue
: () => void
349 * Whether the worker node has back pressure (i.e. its tasks queue is full).
351 * @returns `true` if the worker node has back pressure, `false` otherwise.
353 readonly hasBackPressure
: () => boolean
355 * Terminates the worker node.
357 readonly terminate
: () => Promise
<void>
359 * Registers a worker event handler.
361 * @param event - The event.
362 * @param handler - The event handler.
364 readonly registerWorkerEventHandler
: (
366 handler
: EventHandler
<Worker
>
369 * Registers once a worker event handler.
371 * @param event - The event.
372 * @param handler - The event handler.
374 readonly registerOnceWorkerEventHandler
: (
376 handler
: EventHandler
<Worker
>
379 * Gets task function worker usage statistics.
381 * @param name - The task function name.
382 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
384 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
386 * Deletes task function worker usage statistics.
388 * @param name - The task function name.
389 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
391 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
395 * Worker node event detail.
399 export interface WorkerNodeEventDetail
{
401 workerNodeKey
?: number