X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futility-types.ts;h=a8da491e16fe62143d1d716672c4b4db44aa8e9f;hb=refs%2Ftags%2Fv2.6.10;hp=2403b8a257b0377e441a4dc2568b49571be4cdc2;hpb=deb85c12b77faf6974551cefcd9676e62a392086;p=poolifier.git diff --git a/src/utility-types.ts b/src/utility-types.ts index 2403b8a2..a8da491e 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,39 +1,109 @@ -import type { Worker } from 'cluster' -import type { MessagePort } from '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. + * Task error. + * + * @typeParam Data - Type of data sent to the worker triggering an error. This can only be structured-cloneable data. */ -export type Draft = { -readonly [P in keyof T]?: T[P] } +export interface TaskError { + /** + * Error message. + */ + readonly message: string + /** + * Data passed to the worker triggering the error. + */ + readonly data?: Data +} /** - * Message object that is passed between worker and main worker. + * Task performance. + * + * @internal */ -export interface MessageValue< - Data = unknown, - MainWorker extends Worker | MessagePort | unknown = unknown -> { +export interface TaskPerformance { /** - * Input data that will be passed to the worker. + * Task performance timestamp. */ - readonly data?: Data + readonly timestamp: number + /** + * Task runtime. + */ + readonly runTime?: number /** - * ID of the message. + * Task event loop utilization. */ - readonly id?: number + readonly elu?: EventLoopUtilization +} + +/** + * Performance statistics computation. + * + * @internal + */ +export interface WorkerStatistics { + runTime: boolean + elu: boolean +} + +/** + * Message object that is passed between main worker and worker. + * + * @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 interface MessageValue + extends Task { /** * Kill code. */ - readonly kill?: KillBehavior | 1 + readonly kill?: KillBehavior | true + /** + * Task error. + */ + readonly taskError?: TaskError + /** + * 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 structured-cloneable data. + * @internal + */ +export interface PromiseResponseWrapper< + Worker extends IWorker, + Response = unknown +> { + /** + * Resolve callback to fulfill the promise. + */ + readonly resolve: (value: Response) => void /** - * Error. + * Reject callback to reject the promise. */ - readonly error?: string + readonly reject: (reason?: string) => void /** - * Reference to main worker. - * - * _Only for internal use_ + * The worker handling the execution. */ - readonly parent?: MainWorker + readonly worker: Worker }