1 import type { MessageChannel
, WorkerOptions
} from
'node:worker_threads'
2 import type { EventEmitter
} from
'node:events'
3 import type { CircularArray
} from
'../circular-array.js'
4 import type { Task
} from
'../utility-types.js'
7 * Callback invoked when the worker has started successfully.
9 * @typeParam Worker - Type of worker.
11 export type OnlineHandler
<Worker
extends IWorker
> = (this: Worker
) => void
14 * Callback invoked if the worker has received a message.
16 * @typeParam Worker - Type of worker.
18 export type MessageHandler
<Worker
extends IWorker
> = (
24 * Callback invoked if the worker raised an error.
26 * @typeParam Worker - Type of worker.
28 export type ErrorHandler
<Worker
extends IWorker
> = (
34 * Callback invoked when the worker exits successfully.
36 * @typeParam Worker - Type of worker.
38 export type ExitHandler
<Worker
extends IWorker
> = (
44 * Worker event handler.
46 * @typeParam Worker - Type of worker.
48 export type EventHandler
<Worker
extends IWorker
> =
49 | OnlineHandler
<Worker
>
50 | MessageHandler
<Worker
>
51 | ErrorHandler
<Worker
>
55 * Measurement statistics.
59 export interface MeasurementStatistics
{
61 * Measurement aggregate.
65 * Measurement minimum.
69 * Measurement maximum.
73 * Measurement average.
81 * Measurement history.
83 readonly history
: CircularArray
<number>
87 * Event loop utilization measurement statistics.
91 export interface EventLoopUtilizationMeasurementStatistics
{
92 readonly idle
: MeasurementStatistics
93 readonly active
: MeasurementStatistics
102 export interface TaskStatistics
{
104 * Number of executed tasks.
108 * Number of executing tasks.
112 * Number of queued tasks.
114 readonly queued
: number
116 * Maximum number of queued tasks.
118 readonly maxQueued
?: number
120 * Number of sequentially stolen tasks.
122 sequentiallyStolen
: number
124 * Number of stolen tasks.
128 * Number of failed tasks.
134 * Enumeration of worker types.
136 export const WorkerTypes
= Object.freeze({
144 export type WorkerType
= keyof
typeof WorkerTypes
147 * Worker information.
151 export interface WorkerInfo
{
155 readonly id
: number | undefined
159 readonly type: WorkerType
170 * This flag is set to `true` when worker node is stealing tasks from another worker node.
174 * Task function names.
176 taskFunctionNames
?: string[]
180 * Worker usage statistics.
184 export interface WorkerUsage
{
188 readonly tasks
: TaskStatistics
190 * Tasks runtime statistics.
192 readonly runTime
: MeasurementStatistics
194 * Tasks wait time statistics.
196 readonly waitTime
: MeasurementStatistics
198 * Tasks event loop utilization statistics.
200 readonly elu
: EventLoopUtilizationMeasurementStatistics
204 * Worker choice strategy data.
208 export interface StrategyData
{
209 virtualTaskEndTimestamp
?: number
215 export interface IWorker
{
221 * Worker thread worker id.
223 readonly threadId
?: number
225 * Registers an event handler.
227 * @param event - The event.
228 * @param handler - The event handler.
230 readonly on
: (event
: string, handler
: EventHandler
<this>) => void
232 * Registers once an event handler.
234 * @param event - The event.
235 * @param handler - The event handler.
237 readonly once
: (event
: string, handler
: EventHandler
<this>) => void
239 * Stop all JavaScript execution in the worker thread as soon as possible.
240 * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
242 readonly terminate
?: () => Promise
<number>
244 * Cluster worker disconnect.
246 readonly disconnect
?: () => void
248 * Cluster worker kill.
250 readonly kill
?: (signal
?: string) => void
254 * Worker node options.
258 export interface WorkerNodeOptions
{
259 workerOptions
?: WorkerOptions
260 env
?: Record
<string, unknown
>
261 tasksQueueBackPressureSize
: number
265 * Worker node interface.
267 * @typeParam Worker - Type of worker.
268 * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
271 export interface IWorkerNode
<Worker
extends IWorker
, Data
= unknown
>
272 extends EventEmitter
{
276 readonly worker
: Worker
280 readonly info
: WorkerInfo
282 * Worker usage statistics.
284 readonly usage
: WorkerUsage
286 * Worker choice strategy data.
287 * This is used to store data that are specific to the worker choice strategy.
289 strategyData
?: StrategyData
291 * Message channel (worker thread only).
293 readonly messageChannel
?: MessageChannel
295 * Tasks queue back pressure size.
296 * This is the number of tasks that can be enqueued before the worker node has back pressure.
298 tasksQueueBackPressureSize
: number
302 * @returns The tasks queue size.
304 readonly tasksQueueSize
: () => number
308 * @param task - The task to queue.
309 * @returns The tasks queue size.
311 readonly enqueueTask
: (task
: Task
<Data
>) => number
313 * Prepends a task to the tasks queue.
315 * @param task - The task to prepend.
316 * @returns The tasks queue size.
318 readonly unshiftTask
: (task
: Task
<Data
>) => number
322 * @returns The dequeued task.
324 readonly dequeueTask
: () => Task
<Data
> | undefined
326 * Pops a task from the tasks queue.
328 * @returns The popped task.
330 readonly popTask
: () => Task
<Data
> | undefined
332 * Clears tasks queue.
334 readonly clearTasksQueue
: () => void
336 * Whether the worker node has back pressure (i.e. its tasks queue is full).
338 * @returns `true` if the worker node has back pressure, `false` otherwise.
340 readonly hasBackPressure
: () => boolean
342 * Resets usage statistics.
344 readonly resetUsage
: () => void
346 * Terminates the worker node.
348 readonly terminate
: () => Promise
<void>
350 * Registers a worker event handler.
352 * @param event - The event.
353 * @param handler - The event handler.
355 readonly registerWorkerEventHandler
: (
357 handler
: EventHandler
<Worker
>
360 * Registers once a worker event handler.
362 * @param event - The event.
363 * @param handler - The event handler.
365 readonly registerOnceWorkerEventHandler
: (
367 handler
: EventHandler
<Worker
>
370 * Gets task function worker usage statistics.
372 * @param name - The task function name.
373 * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
375 readonly getTaskFunctionWorkerUsage
: (name
: string) => WorkerUsage
| undefined
377 * Deletes task function worker usage statistics.
379 * @param name - The task function name.
380 * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
382 readonly deleteTaskFunctionWorkerUsage
: (name
: string) => boolean
386 * Worker node event detail.
390 export interface WorkerNodeEventDetail
{
392 workerNodeKey
?: number