+import type { MessageChannel } from 'node:worker_threads'
import type { CircularArray } from '../circular-array'
+import type { Task } from '../utility-types'
+
+/**
+ * Callback invoked when the worker has started successfully.
+ */
+export type OnlineHandler<Worker extends IWorker> = (this: Worker) => void
/**
* Callback invoked if the worker has received a message.
*/
export type MessageHandler<Worker extends IWorker> = (
this: Worker,
- m: unknown
+ message: unknown
) => void
/**
*/
export type ErrorHandler<Worker extends IWorker> = (
this: Worker,
- e: Error
+ 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.
*/
export type ExitHandler<Worker extends IWorker> = (
this: Worker,
- code: number
+ exitCode: number
) => void
/**
- * Message object that is passed as a task between main worker and worker.
+ * Measurement statistics.
+ *
+ * @internal
+ */
+export interface MeasurementStatistics {
+ /**
+ * Measurement aggregate.
+ */
+ aggregate?: number
+ /**
+ * Measurement minimum.
+ */
+ minimum?: number
+ /**
+ * Measurement maximum.
+ */
+ maximum?: number
+ /**
+ * Measurement average.
+ */
+ average?: number
+ /**
+ * Measurement median.
+ */
+ median?: number
+ /**
+ * Measurement history.
+ */
+ readonly history: CircularArray<number>
+}
+
+/**
+ * Event loop utilization measurement statistics.
+ *
+ * @internal
+ */
+export interface EventLoopUtilizationMeasurementStatistics {
+ readonly idle: MeasurementStatistics
+ readonly active: MeasurementStatistics
+ utilization?: number
+}
+
+/**
+ * Task statistics.
*
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
* @internal
*/
-export interface Task<Data = unknown> {
+export interface TaskStatistics {
/**
- * Task input data that will be passed to the worker.
+ * Number of executed tasks.
*/
- readonly data?: Data
+ executed: number
/**
- * UUID of the message.
+ * Number of executing tasks.
*/
- readonly id?: string
+ executing: number
+ /**
+ * Number of queued tasks.
+ */
+ readonly queued: number
+ /**
+ * Maximum number of queued tasks.
+ */
+ readonly maxQueued?: number
+ /**
+ * Number of stolen tasks.
+ */
+ stolen: number
+ /**
+ * Number of failed tasks.
+ */
+ failed: number
}
/**
- * Worker tasks usage statistics.
+ * Enumeration of worker types.
+ */
+export const WorkerTypes = Object.freeze({
+ thread: 'thread',
+ cluster: 'cluster'
+} as const)
+
+/**
+ * Worker type.
+ */
+export type WorkerType = keyof typeof WorkerTypes
+
+/**
+ * Worker information.
*
* @internal
*/
-export interface TasksUsage {
+export interface WorkerInfo {
/**
- * Number of tasks executed.
+ * Worker id.
*/
- run: number
+ readonly id: number | undefined
/**
- * Number of tasks running.
+ * Worker type.
*/
- running: number
+ readonly type: WorkerType
/**
- * Tasks runtime.
+ * Dynamic flag.
*/
- runTime: number
+ dynamic: boolean
+ /**
+ * Ready flag.
+ */
+ ready: boolean
+ /**
+ * Task function names.
+ */
+ taskFunctionNames?: string[]
+}
+
+/**
+ * Worker usage statistics.
+ *
+ * @internal
+ */
+export interface WorkerUsage {
/**
- * Tasks runtime history.
+ * Tasks statistics.
*/
- runTimeHistory: CircularArray<number>
+ readonly tasks: TaskStatistics
/**
- * Average tasks runtime.
+ * Tasks runtime statistics.
*/
- avgRunTime: number
+ readonly runTime: MeasurementStatistics
/**
- * Median tasks runtime.
+ * Tasks wait time statistics.
*/
- medRunTime: number
+ readonly waitTime: MeasurementStatistics
/**
- * Number of tasks errored.
+ * Tasks event loop utilization statistics.
*/
- error: number
+ readonly elu: EventLoopUtilizationMeasurementStatistics
+}
+
+/**
+ * Worker choice strategy data.
+ *
+ * @internal
+ */
+export interface StrategyData {
+ virtualTaskEndTimestamp?: number
}
/**
*/
export interface IWorker {
/**
- * Register an event listener.
+ * Worker id.
+ */
+ readonly id?: number
+ readonly threadId?: number
+ /**
+ * Registers an event listener.
*
* @param event - The event.
* @param handler - The event handler.
*/
- on: ((event: 'message', handler: MessageHandler<this>) => void) &
+ readonly on: ((event: 'online', handler: OnlineHandler<this>) => void) &
+ ((event: 'message', handler: MessageHandler<this>) => void) &
((event: 'error', handler: ErrorHandler<this>) => void) &
- ((event: 'online', handler: OnlineHandler<this>) => void) &
((event: 'exit', handler: ExitHandler<this>) => void)
/**
- * Register a listener to the exit event that will only be performed once.
+ * Registers a listener to the exit event that will only be performed once.
*
- * @param event - `'exit'`.
+ * @param event - The `'exit'` event.
* @param handler - The exit handler.
*/
- once: (event: 'exit', handler: ExitHandler<this>) => void
+ readonly once: (event: 'exit', handler: ExitHandler<this>) => void
}
+/**
+ * Worker node event callback.
+ *
+ * @param workerId - The worker id.
+ * @internal
+ */
+export type WorkerNodeEventCallback = (workerId: number) => void
+
/**
* Worker node interface.
*
* @typeParam Worker - Type of worker.
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
* @internal
*/
-export interface WorkerNode<Worker extends IWorker, Data = unknown> {
+export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
/**
- * Worker node worker.
+ * Worker.
*/
readonly worker: Worker
/**
- * Worker node tasks usage statistics.
+ * Worker info.
+ */
+ readonly info: WorkerInfo
+ /**
+ * Worker usage statistics.
+ */
+ readonly usage: WorkerUsage
+ /**
+ * Worker choice strategy data.
+ * This is used to store data that is specific to the worker choice strategy.
+ */
+ strategyData?: StrategyData
+ /**
+ * Message channel (worker_threads 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.
+ */
+ tasksQueueBackPressureSize: number
+ /**
+ * Callback invoked when worker node tasks queue is back pressured.
*/
- tasksUsage: TasksUsage
+ onBackPressure?: WorkerNodeEventCallback
/**
- * Worker node tasks queue.
+ * Callback invoked when worker node tasks queue is empty.
+ */
+ onEmptyQueue?: WorkerNodeEventCallback
+ /**
+ * Tasks queue size.
+ *
+ * @returns The tasks queue size.
+ */
+ readonly tasksQueueSize: () => number
+ /**
+ * Enqueue task.
+ *
+ * @param task - The task to queue.
+ * @returns The tasks queue size.
+ */
+ readonly enqueueTask: (task: Task<Data>) => number
+ /**
+ * Prepends a task to the tasks queue.
+ *
+ * @param task - The task to prepend.
+ * @returns The tasks queue size.
+ */
+ readonly unshiftTask: (task: Task<Data>) => number
+ /**
+ * Dequeue task.
+ *
+ * @returns The dequeued task.
+ */
+ readonly dequeueTask: () => Task<Data> | undefined
+ /**
+ * Pops a task from the tasks queue.
+ *
+ * @returns The popped task.
+ */
+ readonly popTask: () => Task<Data> | undefined
+ /**
+ * Clears tasks queue.
+ */
+ readonly clearTasksQueue: () => void
+ /**
+ * 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 hasBackPressure: () => boolean
+ /**
+ * Resets usage statistics.
+ */
+ readonly resetUsage: () => void
+ /**
+ * Closes communication channel.
+ */
+ readonly closeChannel: () => 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 tasksQueue: Array<Task<Data>>
+ readonly getTaskFunctionWorkerUsage: (name: string) => WorkerUsage | undefined
}