X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futility-types.ts;h=3917ad2042b920f7509749605d86f073208c71cb;hb=82f36766232abd4e4ffde72ecd68462adf59cca7;hp=9c8d9650b15ccc31007a19d199117860a650ac58;hpb=e088a00c285c320395d883d57d1db51d42300b10;p=poolifier.git diff --git a/src/utility-types.ts b/src/utility-types.ts index 9c8d9650..3917ad20 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,51 +1,100 @@ -import type { Worker } from 'cluster' -import type { MessagePort } from 'worker_threads' -import type { IWorker } from './pools/abstract-pool' +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' /** * Make all properties in T non-readonly. + * + * @typeParam T - Type in which properties will be non-readonly. */ export type Draft = { -readonly [P in keyof T]?: T[P] } +export interface TaskError { + /** + * Error message. + */ + message: string + /** + * Data passed to the worker triggering the error. + */ + data?: Data +} + /** - * Message object that is passed between worker and main worker. + * Task performance. */ -export interface MessageValue< - Data = unknown, - MainWorker extends Worker | MessagePort | unknown = unknown -> { +export interface TaskPerformance { + /** + * Task performance timestamp. + */ + timestamp: number /** - * Input data that will be passed to the worker. + * Task runtime. */ - readonly data?: Data + runTime?: number /** - * Id of the message. + * Task wait time. */ - readonly id?: number + waitTime?: number + /** + * Task event loop utilization. + */ + elu?: EventLoopUtilization +} + +/** + * Performance statistics computation. + */ +export interface WorkerStatistics { + runTime: boolean + waitTime: boolean + elu: boolean +} + +/** + * 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 MainWorker - Type of main worker. + * @internal + */ +export interface MessageValue< + Data = unknown, + ErrorData = unknown, + MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort +> extends Task { /** * Kill code. */ readonly kill?: KillBehavior | 1 /** - * Error. + * Task error. */ - readonly error?: string + readonly taskError?: TaskError + /** + * Task performance. + */ + readonly taskPerformance?: TaskPerformance /** * Reference to main worker. - * - * _Only for internal use_ */ readonly parent?: MainWorker + /** + * Whether to compute the given statistics or not. + */ + readonly statistics?: WorkerStatistics } /** - * An object holding the worker that will be used to resolve/rejects the promise later on. + * An object holding the execution response promise resolve/reject callbacks. * - * @template Worker Type of worker. - * @template Response Type of response of execution. This can only be serializable data. + * @typeParam Worker - Type of worker. + * @typeParam Response - Type of execution response. This can only be serializable data. + * @internal */ -export interface PromiseWorkerResponseWrapper< +export interface PromiseResponseWrapper< Worker extends IWorker, Response = unknown > { @@ -58,7 +107,7 @@ export interface PromiseWorkerResponseWrapper< */ readonly reject: (reason?: string) => void /** - * The worker that has the assigned task. + * The worker handling the execution. */ readonly worker: Worker }