build(deps-dev): apply updates
[poolifier.git] / src / pools / worker.ts
index f439c882a66a703e23cffafc6d02b5f0fcb7fef1..9f5fc0c18dd1d2f214dce86d63d65e88faa11642 100644 (file)
@@ -1,18 +1,17 @@
-import type { MessageChannel, WorkerOptions } from 'node:worker_threads'
 import type { EventEmitter } from 'node:events'
-import type { CircularArray } from '../circular-array.js'
-import type { Task } from '../utility-types.js'
+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> = (
@@ -22,7 +21,6 @@ export type MessageHandler<Worker extends IWorker> = (
 
 /**
  * Callback invoked if the worker raised an error.
- *
  * @typeParam Worker - Type of worker.
  */
 export type ErrorHandler<Worker extends IWorker> = (
@@ -32,7 +30,6 @@ export type ErrorHandler<Worker extends IWorker> = (
 
 /**
  * Callback invoked when the worker exits successfully.
- *
  * @typeParam Worker - Type of worker.
  */
 export type ExitHandler<Worker extends IWorker> = (
@@ -42,7 +39,6 @@ export type ExitHandler<Worker extends IWorker> = (
 
 /**
  * Worker event handler.
- *
  * @typeParam Worker - Type of worker.
  */
 export type EventHandler<Worker extends IWorker> =
@@ -51,9 +47,13 @@ export type EventHandler<Worker extends IWorker> =
   | ErrorHandler<Worker>
   | ExitHandler<Worker>
 
+/**
+ * Measurement history size.
+ */
+export const MeasurementHistorySize = 386
+
 /**
  * Measurement statistics.
- *
  * @internal
  */
 export interface MeasurementStatistics {
@@ -80,12 +80,11 @@ export interface MeasurementStatistics {
   /**
    * Measurement history.
    */
-  readonly history: CircularArray<number>
+  readonly history: CircularBuffer
 }
 
 /**
  * Event loop utilization measurement statistics.
- *
  * @internal
  */
 export interface EventLoopUtilizationMeasurementStatistics {
@@ -96,7 +95,6 @@ export interface EventLoopUtilizationMeasurementStatistics {
 
 /**
  * Task statistics.
- *
  * @internal
  */
 export interface TaskStatistics {
@@ -133,10 +131,11 @@ export interface TaskStatistics {
 /**
  * Enumeration of worker types.
  */
-export const WorkerTypes = Object.freeze({
-  thread: 'thread',
-  cluster: 'cluster'
-} as const)
+export const WorkerTypes: Readonly<{ thread: 'thread'; cluster: 'cluster' }> =
+  Object.freeze({
+    thread: 'thread',
+    cluster: 'cluster',
+  } as const)
 
 /**
  * Worker type.
@@ -145,7 +144,6 @@ export type WorkerType = keyof typeof WorkerTypes
 
 /**
  * Worker information.
- *
  * @internal
  */
 export interface WorkerInfo {
@@ -171,14 +169,18 @@ export interface WorkerInfo {
    */
   stealing: boolean
   /**
-   * Task function names.
+   * Back pressure flag.
+   * This flag is set to `true` when worker node tasks queue has back pressure.
+   */
+  backPressure: boolean
+  /**
+   * Task functions properties.
    */
-  taskFunctionNames?: string[]
+  taskFunctionsProperties?: TaskFunctionProperties[]
 }
 
 /**
  * Worker usage statistics.
- *
  * @internal
  */
 export interface WorkerUsage {
@@ -202,7 +204,6 @@ export interface WorkerUsage {
 
 /**
  * Worker choice strategy data.
- *
  * @internal
  */
 export interface StrategyData {
@@ -212,7 +213,7 @@ export interface StrategyData {
 /**
  * Worker interface.
  */
-export interface IWorker {
+export interface IWorker extends EventEmitter {
   /**
    * Cluster worker id.
    */
@@ -223,18 +224,22 @@ export interface IWorker {
   readonly threadId?: number
   /**
    * Registers an event handler.
-   *
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly on: (event: string, handler: EventHandler<this>) => void
+  readonly on: (event: string, handler: EventHandler<this>) => this
   /**
    * Registers once an event handler.
-   *
    * @param event - The event.
    * @param handler - The event handler.
    */
-  readonly once: (event: string, handler: EventHandler<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.
@@ -252,18 +257,18 @@ export interface IWorker {
 
 /**
  * Worker node options.
- *
  * @internal
  */
 export interface WorkerNodeOptions {
   workerOptions?: WorkerOptions
   env?: Record<string, unknown>
-  tasksQueueBackPressureSize: number
+  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
@@ -296,59 +301,48 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
    * This is the number of tasks that can be enqueued before the worker node has back pressure.
    */
   tasksQueueBackPressureSize: number
+  /**
+   * 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
   /**
    * 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.
-   *
+   * @param bucket - The prioritized bucket to dequeue from. @defaultValue 0
    * @returns The dequeued task.
    */
-  readonly dequeueTask: () => Task<Data> | undefined
+  readonly dequeueTask: (bucket?: number) => Task<Data> | undefined
   /**
-   * Pops a task from the tasks queue.
-   *
-   * @returns The popped task.
+   * Dequeue last prioritized task.
+   * @returns The dequeued task.
    */
-  readonly popTask: () => Task<Data> | undefined
+  readonly dequeueLastPrioritizedTask: () => 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
   /**
    * Terminates the worker node.
    */
   readonly terminate: () => Promise<void>
   /**
    * Registers a worker event handler.
-   *
    * @param event - The event.
    * @param handler - The event handler.
    */
@@ -358,7 +352,6 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
   ) => void
   /**
    * Registers once a worker event handler.
-   *
    * @param event - The event.
    * @param handler - The event handler.
    */
@@ -368,14 +361,12 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
   ) => 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.
    */
@@ -384,10 +375,9 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown>
 
 /**
  * Worker node event detail.
- *
  * @internal
  */
 export interface WorkerNodeEventDetail {
-  workerId: number
+  workerId?: number
   workerNodeKey?: number
 }