fix: unregister worker callbacks after usage
[poolifier.git] / src / pools / worker.ts
index 14c8dbe1e1d444f5404e8562c1c59ecf1585d869..4c877bc8b1dcc58871a55cd70c87871d918c4596 100644 (file)
@@ -96,6 +96,10 @@ export interface TaskStatistics {
    * Maximum number of queued tasks.
    */
   readonly maxQueued?: number
+  /**
+   * Number of stolen tasks.
+   */
+  stolen: number
   /**
    * Number of failed tasks.
    */
@@ -128,7 +132,7 @@ export interface WorkerInfo {
   /**
    * Worker type.
    */
-  type: WorkerType
+  readonly type: WorkerType
   /**
    * Dynamic flag.
    */
@@ -140,7 +144,7 @@ export interface WorkerInfo {
   /**
    * Task function names.
    */
-  taskFunctions?: string[]
+  taskFunctionNames?: string[]
 }
 
 /**
@@ -167,6 +171,15 @@ export interface WorkerUsage {
   readonly elu: EventLoopUtilizationMeasurementStatistics
 }
 
+/**
+ * Worker choice strategy data.
+ *
+ * @internal
+ */
+export interface StrategyData {
+  virtualTaskEndTimestamp?: number
+}
+
 /**
  * Worker interface.
  */
@@ -189,12 +202,20 @@ export interface IWorker {
   /**
    * 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.
    */
   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.
  *
@@ -212,13 +233,18 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    */
   readonly info: WorkerInfo
   /**
-   * Message channel (worker_threads only).
+   * Worker usage statistics.
    */
-  readonly messageChannel?: MessageChannel
+  readonly usage: WorkerUsage
   /**
-   * Worker usage statistics.
+   * Worker choice strategy data.
+   * This is used to store data that is specific to the worker choice strategy.
    */
-  usage: WorkerUsage
+  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.
@@ -226,16 +252,12 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
   tasksQueueBackPressureSize: number
   /**
    * Callback invoked when worker node tasks queue is back pressured.
-   *
-   * @param workerId - The worker id.
    */
-  onBackPressure?: (workerId: number) => void
+  onBackPressure?: WorkerNodeEventCallback
   /**
    * Callback invoked when worker node tasks queue is empty.
-   *
-   * @param workerId - The worker id.
    */
-  onEmptyQueue?: (workerId: number) => void
+  onEmptyQueue?: WorkerNodeEventCallback
   /**
    * Tasks queue size.
    *
@@ -293,4 +315,11 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
    * @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
 }