refactor: cleanup task handling in worker code
[poolifier.git] / src / utility-types.ts
index cc04f2cb99113b9cb66305c102ae5c8fe88563ad..28bf19e256abaef2f619051773ad55456643b842 100644 (file)
-import type { Worker as ClusterWorker } from 'node:cluster'
-import type { MessagePort } from 'node:worker_threads'
+import type { EventLoopUtilization } from 'node:perf_hooks'
 import type { KillBehavior } from './worker/worker-options'
-import type { IWorker, Task } from './pools/worker'
+import type { IWorker } from './pools/worker'
 
 /**
- * Make all properties in T non-readonly.
+ * Task error.
  *
- * @typeParam T - Type in which properties will be non-readonly.
+ * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
  */
-export type Draft<T> = { -readonly [P in keyof T]?: T[P] }
+export interface TaskError<Data = unknown> {
+  /**
+   * Task name triggering the error.
+   */
+  readonly name: string
+  /**
+   * Error message.
+   */
+  readonly message: string
+  /**
+   * Data triggering the error.
+   */
+  readonly data?: Data
+}
 
 /**
- * Message object that is passed between main worker and worker.
+ * Task performance.
  *
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam MainWorker - Type of main worker.
  * @internal
  */
-export interface MessageValue<
-  Data = unknown,
-  MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort
-> extends Task<Data> {
+export interface TaskPerformance {
   /**
-   * Kill code.
+   * Task name.
    */
-  readonly kill?: KillBehavior | 1
+  readonly name: string
   /**
-   * Error.
+   * Task performance timestamp.
    */
-  readonly error?: string
+  readonly timestamp: number
   /**
-   * Runtime.
+   * Task runtime.
    */
   readonly runTime?: number
   /**
-   * Wait time.
+   * Task event loop utilization.
    */
-  readonly waitTime?: number
-  /**
-   * Reference to main worker.
-   */
-  readonly parent?: MainWorker
+  readonly elu?: EventLoopUtilization
 }
 
 /**
- * Worker synchronous function that can be executed.
+ * Performance statistics computation.
  *
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
- */
-export type WorkerSyncFunction<Data = unknown, Response = unknown> = (
-  data?: Data
-) => Response
-
-/**
- * Worker asynchronous function that can be executed.
- * This function must return a promise.
- *
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @internal
  */
-export type WorkerAsyncFunction<Data = unknown, Response = unknown> = (
-  data?: Data
-) => Promise<Response>
+export interface WorkerStatistics {
+  runTime: boolean
+  elu: boolean
+}
 
 /**
- * Worker function that can be executed.
- * This function can be synchronous or asynchronous.
+ * 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 serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data.
+ * @internal
  */
-export type WorkerFunction<Data = unknown, Response = unknown> =
-  | WorkerSyncFunction<Data, Response>
-  | WorkerAsyncFunction<Data, Response>
+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
+}
 
 /**
- * Worker functions that can be executed.
- * This object can contain synchronous or asynchronous functions.
- * The key is the name of the function.
- * The value is the function itself.
+ * Message object that is passed between main worker and worker.
  *
- * @typeParam Data - Type of data sent to the worker. This can only be serializable data.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Data - Type of data sent to the worker or execution response. This can only be structured-cloneable data.
+ * @typeParam ErrorData - Type of data sent to the worker triggering an error. This can only be structured-cloneable data.
+ * @internal
  */
-export type TaskFunctions<Data = unknown, Response = unknown> = Record<
-string,
-WorkerFunction<Data, Response>
->
+export interface MessageValue<Data = unknown, ErrorData = unknown>
+  extends Task<Data> {
+  /**
+   * Kill code.
+   */
+  readonly kill?: KillBehavior | true
+  /**
+   * Task error.
+   */
+  readonly taskError?: TaskError<ErrorData>
+  /**
+   * Task performance.
+   */
+  readonly taskPerformance?: TaskPerformance
+  /**
+   * Whether the worker computes the given statistics or not.
+   */
+  readonly statistics?: WorkerStatistics
+  /**
+   * Whether the worker is ready or not.
+   */
+  readonly ready?: boolean
+  /**
+   * Whether the worker starts or stops its aliveness check.
+   */
+  readonly checkAlive?: boolean
+}
 
 /**
  * An object holding the execution response promise resolve/reject callbacks.
  *
  * @typeParam Worker - Type of worker.
- * @typeParam Response - Type of execution response. This can only be serializable data.
+ * @typeParam Response - Type of execution response. This can only be structured-cloneable data.
  * @internal
  */
 export interface PromiseResponseWrapper<