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
, 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 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 functions properties.
178 taskFunctionsProperties
?: TaskFunctionProperties
[]
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
270 tasksQueueBucketSize
: number | undefined
274 * Worker node interface.
276 * @typeParam Worker - Type of worker.
277 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
280 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
281 extends EventEmitter
{
285 readonly worker
: Worker
289 readonly info
: WorkerInfo
291 * Worker usage statistics.
293 readonly usage
: WorkerUsage
295 * Worker choice strategy data.
296 * This is used to store data that are specific to the worker choice strategy.
298 strategyData
?: StrategyData
300 * Message channel (worker thread only).
302 readonly messageChannel
?: MessageChannel
304 * Tasks queue back pressure size.
305 * This is the number of tasks that can be enqueued before the worker node has back pressure.
307 tasksQueueBackPressureSize
: number
311 * @returns The tasks queue size.
313 readonly tasksQueueSize
: () => number
317 * @param task - The task to queue.
318 * @returns The tasks queue size.
320 readonly enqueueTask
: (task
: Task
<Data
>) => number
324 * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
325 * @returns The dequeued task.
327 readonly dequeueTask
: (bucket
?: number) => Task
<Data
> | undefined
329 * Clears tasks queue.
331 readonly clearTasksQueue
: () => void
333 * Whether the worker node has back pressure (i.e. its tasks queue is full).
335 * @returns `true` if the worker node has back pressure, `false` otherwise.
337 readonly hasBackPressure
: () => boolean
339 * Terminates the worker node.
341 readonly terminate
: () => Promise
<void>
343 * Registers a worker event handler.
345 * @param event - The event.
346 * @param handler - The event handler.
348 readonly registerWorkerEventHandler
: (
350 handler
: EventHandler
<Worker
>
353 * Registers once a worker event handler.
355 * @param event - The event.
356 * @param handler - The event handler.
358 readonly registerOnceWorkerEventHandler
: (
360 handler
: EventHandler
<Worker
>
363 * Gets task function worker usage statistics.
365 * @param name - The task function name.
366 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
368 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
370 * Deletes task function worker usage statistics.
372 * @param name - The task function name.
373 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
375 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
379 * Worker node event detail.
383 export interface WorkerNodeEventDetail
{
385 workerNodeKey
?: number