X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futility-types.ts;h=10d9183ca6f262cd25d475737d0e86613a55d247;hb=85bbc7ab16c9f69a5dd358b71e3e6d4204dfd630;hp=9cdf6b0a7e82f9d1e3da5fb434537ca8e2d943f9;hpb=a788de39a01b042deb0707c2e92af99181b933d2;p=poolifier.git diff --git a/src/utility-types.ts b/src/utility-types.ts index 9cdf6b0a..10d9183c 100644 --- a/src/utility-types.ts +++ b/src/utility-types.ts @@ -1,91 +1,208 @@ -import type { Worker as ClusterWorker } from 'node:cluster' -import type { MessagePort } from 'node:worker_threads' +import type { AsyncResource } from 'node:async_hooks' import type { EventLoopUtilization } from 'node:perf_hooks' -import type { KillBehavior } from './worker/worker-options' -import type { IWorker, Task } from './pools/worker' +import type { MessagePort, TransferListItem } from 'node:worker_threads' + +import type { WorkerChoiceStrategy } from './pools/selection-strategies/selection-strategies-types.js' +import type { KillBehavior } from './worker/worker-options.js' /** - * Make all properties in T non-readonly. + * Worker 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 = { -readonly [P in keyof T]?: T[P] } +export interface WorkerError { + /** + * Task function name triggering the error. + */ + readonly name: string + /** + * Error message. + */ + readonly message: string + /** + * Data triggering the error. + */ + readonly data?: Data +} /** - * Performance statistics computation. + * Task performance. + * + * @internal + */ +export interface TaskPerformance { + /** + * Task name. + */ + readonly name: string + /** + * Task performance timestamp. + */ + readonly timestamp: number + /** + * Task runtime. + */ + readonly runTime?: number + /** + * Task event loop utilization. + */ + readonly elu?: EventLoopUtilization +} + +/** + * Worker task performance statistics computation settings. + * + * @internal */ export interface WorkerStatistics { - runTime: boolean - waitTime: boolean - elu: boolean + /** + * Whether the worker computes the task runtime or not. + */ + readonly runTime: boolean + /** + * Whether the worker computes the task event loop utilization (ELU) or not. + */ + readonly elu: boolean +} + +/** + * Task function properties. + * + * @internal + */ +export interface TaskFunctionProperties { + /** + * Task function name. + */ + name: string + /** + * Task function priority. Lower values have higher priority. + */ + priority?: number + /** + * Task function worker choice strategy. + */ + strategy?: WorkerChoiceStrategy +} + +/** + * 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 structured-cloneable data. + * @internal + */ +export interface Task { + /** + * Task name. + */ + readonly name?: string + /** + * Task input data that will be passed to the worker. + */ + readonly data?: Data + /** + * Array of transferable objects. + */ + readonly transferList?: readonly TransferListItem[] + /** + * Timestamp. + */ + readonly timestamp?: number + /** + * Task UUID. + */ + readonly taskId?: string } /** * 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. + * @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< - Data = unknown, - MainWorker extends ClusterWorker | MessagePort = ClusterWorker | MessagePort -> extends Task { +export interface MessageValue + extends Task { + /** + * Worker id. + */ + readonly workerId?: number /** * Kill code. */ - readonly kill?: KillBehavior | 1 + readonly kill?: KillBehavior | true | 'success' | 'failure' /** - * Task error. + * Worker error. */ - readonly error?: string + readonly workerError?: WorkerError /** - * Task data triggering task error. + * Task performance. */ - readonly errorData?: unknown + readonly taskPerformance?: TaskPerformance /** - * Runtime. + * Task function operation: + * - `'add'` - Add a task function. + * - `'remove'` - Remove a task function. + * - `'default'` - Set a task function as default. */ - readonly runTime?: number + readonly taskFunctionOperation?: 'add' | 'remove' | 'default' /** - * Wait time. + * Whether the task function operation is successful or not. */ - readonly waitTime?: number + readonly taskFunctionOperationStatus?: boolean /** - * Event loop utilization. + * Task function properties. */ - readonly elu?: EventLoopUtilization + readonly taskFunctionProperties?: TaskFunctionProperties + /** + * Task function serialized to string. + */ + readonly taskFunction?: string /** - * Reference to main worker. + * Task functions properties. */ - readonly parent?: MainWorker + readonly taskFunctionsProperties?: TaskFunctionProperties[] /** - * Whether to compute the given statistics or not. + * 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 activity check. + */ + readonly checkActive?: boolean + /** + * Message port. + */ + readonly port?: MessagePort } /** - * An object holding the execution response promise resolve/reject callbacks. + * An object holding the task 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< - Worker extends IWorker, - Response = unknown -> { +export interface PromiseResponseWrapper { /** * Resolve callback to fulfill the promise. */ - readonly resolve: (value: Response) => void + readonly resolve: (value: Response | PromiseLike) => void /** * Reject callback to reject the promise. */ - readonly reject: (reason?: string) => void + readonly reject: (reason?: unknown) => void + /** + * The worker node key executing the task. + */ + readonly workerNodeKey: number /** - * The worker handling the execution. + * The asynchronous resource used to track the task execution. */ - readonly worker: Worker + readonly asyncResource?: AsyncResource } + +export type Writable = { -readonly [P in keyof T]: T[P] }