-import type { CircularArray } from '../circular-array'
+import type { EventEmitter } from 'node:events'
+import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
+
+import type { CircularBuffer } from '../circular-buffer.js'
+import type { Task, TaskFunctionProperties } from '../utility-types.js'
+
+/**
+ * Callback invoked when the worker has started successfully.
+ * @typeParam Worker - Type of worker.
+ */
+export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
/**
* Callback invoked if the worker has received a message.
+ * @typeParam Worker - Type of worker.
*/
export type MessageHandler<Worker extends IWorker> = (
this: Worker,
/**
* Callback invoked if the worker raised an error.
+ * @typeParam Worker - Type of worker.
*/
export type ErrorHandler<Worker extends IWorker> = (
this: Worker,
error: Error
) => void
-/**
- * Callback invoked when the worker has started successfully.
- */
-export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
-
/**
* Callback invoked when the worker exits successfully.
+ * @typeParam Worker - Type of worker.
*/
export type ExitHandler<Worker extends IWorker> = (
this: Worker,
) => void
/**
- * Message object that is passed as a task between main worker and worker.
- *
- * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
- * @internal
+ * Worker event handler.
+ * @typeParam Worker - Type of worker.
*/
-export interface Task<Data = unknown> {
- /**
- * Worker id.
- */
- readonly workerId: number
- /**
- * Task name.
- */
- readonly name?: string
- /**
- * Task input data that will be passed to the worker.
- */
- readonly data?: Data
- /**
- * Timestamp.
- */
- readonly timestamp?: number
- /**
- * Message UUID.
- */
- readonly id?: string
-}
+export type EventHandler<Worker extends IWorker> =
+ | OnlineHandler<Worker>
+ | MessageHandler<Worker>
+ | ErrorHandler<Worker>
+ | ExitHandler<Worker>
+
+/**
+ * Measurement history size.
+ */
+export const MeasurementHistorySize = 386
/**
* Measurement statistics.
- *
* @internal
*/
export interface MeasurementStatistics {
/**
* Measurement history.
*/
- readonly history: CircularArray<number>
+ readonly history: CircularBuffer
}
/**
* Event loop utilization measurement statistics.
- *
* @internal
*/
export interface EventLoopUtilizationMeasurementStatistics {
/**
* Task statistics.
- *
* @internal
*/
export interface TaskStatistics {
* Maximum number of queued tasks.
*/
readonly maxQueued?: number
+ /**
+ * Number of sequentially stolen tasks.
+ */
+ sequentiallyStolen: number
+ /**
+ * Number of stolen tasks.
+ */
+ stolen: number
/**
* Number of failed tasks.
*/
/**
* Enumeration of worker types.
*/
-export const WorkerTypes = Object.freeze({
- cluster: 'cluster',
- thread: 'thread'
-} as const)
+export const WorkerTypes: Readonly<{ thread: 'thread'; cluster: 'cluster' }> =
+ Object.freeze({
+ thread: 'thread',
+ cluster: 'cluster',
+ } as const)
/**
* Worker type.
/**
* Worker information.
- *
* @internal
*/
export interface WorkerInfo {
/**
* Worker type.
*/
- type: WorkerType
+ readonly type: WorkerType
/**
* Dynamic flag.
*/
* Ready flag.
*/
ready: boolean
+ /**
+ * Stealing flag.
+ * This flag is set to `true` when worker node is stealing tasks from another worker node.
+ */
+ stealing: boolean
+ /**
+ * Back pressure flag.
+ * This flag is set to `true` when worker node tasks queue has back pressure.
+ */
+ backPressure: boolean
+ /**
+ * Task functions properties.
+ */
+ taskFunctionsProperties?: TaskFunctionProperties[]
}
/**
* Worker usage statistics.
- *
* @internal
*/
export interface WorkerUsage {
readonly elu: EventLoopUtilizationMeasurementStatistics
}
+/**
+ * Worker choice strategy data.
+ * @internal
+ */
+export interface StrategyData {
+ virtualTaskEndTimestamp?: number
+}
+
/**
* Worker interface.
*/
-export interface IWorker {
+export interface IWorker extends EventEmitter {
/**
- * Worker id.
+ * Cluster worker id.
*/
readonly id?: number
+ /**
+ * Worker thread worker id.
+ */
readonly threadId?: number
/**
- * Registers an event listener.
- *
+ * Registers an event handler.
+ * @param event - The event.
+ * @param handler - The event handler.
+ */
+ readonly on: (event: string, handler: EventHandler<this>) => this
+ /**
+ * Registers once an event handler.
* @param event - The event.
* @param handler - The event handler.
*/
- readonly on: ((event: 'message', handler: MessageHandler<this>) => void) &
- ((event: 'error', handler: ErrorHandler<this>) => void) &
- ((event: 'online', handler: OnlineHandler<this>) => void) &
- ((event: 'exit', handler: ExitHandler<this>) => void)
+ readonly once: (event: string, handler: EventHandler<this>) => this
+ /**
+ * Calling `unref()` on a worker allows the thread to exit if this is the only
+ * active handle in the event system. If the worker is already `unref()`ed calling`unref()` again has no effect.
+ * @since v10.5.0
+ */
+ readonly unref?: () => void
+ /**
+ * Stop all JavaScript execution in the worker thread as soon as possible.
+ * Returns a Promise for the exit code that is fulfilled when the `'exit' event` is emitted.
+ */
+ readonly terminate?: () => Promise<number>
+ /**
+ * Cluster worker disconnect.
+ */
+ readonly disconnect?: () => void
/**
- * Registers a listener to the exit event that will only be performed once.
- *
- * @param event - `'exit'`.
- * @param handler - The exit handler.
+ * Cluster worker kill.
*/
- readonly once: (event: 'exit', handler: ExitHandler<this>) => void
+ readonly kill?: (signal?: string) => void
+}
+
+/**
+ * Worker node options.
+ * @internal
+ */
+export interface WorkerNodeOptions {
+ workerOptions?: WorkerOptions
+ env?: Record<string, unknown>
+ tasksQueueBackPressureSize: number | undefined
+ tasksQueueBucketSize: number | undefined
+ tasksQueuePriority: boolean | undefined
}
/**
* Worker node interface.
- *
* @typeParam Worker - Type of worker.
* @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
*/
-export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
+export interface IWorkerNode<Worker extends IWorker, Data = unknown>
+ extends EventEmitter {
/**
- * Worker node worker.
+ * Worker.
*/
readonly worker: Worker
/**
- * Worker node worker info.
+ * Worker info.
*/
readonly info: WorkerInfo
/**
- * Worker node worker usage statistics.
+ * Worker usage statistics.
+ */
+ readonly usage: WorkerUsage
+ /**
+ * Worker choice strategy data.
+ * This is used to store data that are specific to the worker choice strategy.
+ */
+ strategyData?: StrategyData
+ /**
+ * Message channel (worker thread only).
+ */
+ readonly messageChannel?: MessageChannel
+ /**
+ * Tasks queue back pressure size.
+ * This is the number of tasks that can be enqueued before the worker node has back pressure.
*/
- usage: WorkerUsage
+ tasksQueueBackPressureSize: number
/**
- * Worker node tasks queue size.
- *
+ * Sets tasks queue priority.
+ * @param enablePriority - Whether to enable tasks queue priority.
+ */
+ readonly setTasksQueuePriority: (enablePriority: boolean) => void
+ /**
+ * Tasks queue size.
* @returns The tasks queue size.
*/
readonly tasksQueueSize: () => number
/**
- * Worker node enqueue task.
- *
+ * Enqueue task.
* @param task - The task to queue.
- * @returns The task queue size.
+ * @returns The tasks queue size.
*/
readonly enqueueTask: (task: Task<Data>) => number
/**
- * Worker node dequeue task.
- *
+ * Dequeue task.
+ * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
+ * @returns The dequeued task.
+ */
+ readonly dequeueTask: (bucket?: number) => Task<Data> | undefined
+ /**
+ * Dequeue last prioritized task.
* @returns The dequeued task.
*/
- readonly dequeueTask: () => Task<Data> | undefined
+ readonly dequeueLastPrioritizedTask: () => Task<Data> | undefined
/**
- * Worker node clear tasks queue.
+ * Clears tasks queue.
*/
readonly clearTasksQueue: () => void
/**
- * Worker node reset usage statistics .
+ * Whether the worker node has back pressure (i.e. its tasks queue is full).
+ * @returns `true` if the worker node has back pressure, `false` otherwise.
*/
- readonly resetUsage: () => void
+ readonly hasBackPressure: () => boolean
/**
- * Worker node get task usage statistics.
+ * Terminates the worker node.
*/
- readonly getTaskWorkerUsage: (name: string) => WorkerUsage | undefined
+ readonly terminate: () => Promise<void>
+ /**
+ * Registers a worker event handler.
+ * @param event - The event.
+ * @param handler - The event handler.
+ */
+ readonly registerWorkerEventHandler: (
+ event: string,
+ handler: EventHandler<Worker>
+ ) => void
+ /**
+ * Registers once a worker event handler.
+ * @param event - The event.
+ * @param handler - The event handler.
+ */
+ readonly registerOnceWorkerEventHandler: (
+ event: string,
+ handler: EventHandler<Worker>
+ ) => void
+ /**
+ * Gets task function worker usage statistics.
+ * @param name - The task function name.
+ * @returns The task function worker usage statistics if the task function worker usage statistics are initialized, `undefined` otherwise.
+ */
+ readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
+ /**
+ * Deletes task function worker usage statistics.
+ * @param name - The task function name.
+ * @returns `true` if the task function worker usage statistics were deleted, `false` otherwise.
+ */
+ readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean
+}
+
+/**
+ * Worker node event detail.
+ * @internal
+ */
+export interface WorkerNodeEventDetail {
+ workerId?: number
+ workerNodeKey?: number
}