X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=b3e02f3ceb5ce8631848e65fcfcba871c4dd9e3c;hb=a09a79a68dbaf04a9dff2f9d9dcc37da8c7dd314;hp=7a8c59e7730eba9be39ae972f94022a8a65a14ce;hpb=ec287edfc71369699efd04e284db8759ace8a59c;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 7a8c59e7..b3e02f3c 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -4,11 +4,15 @@ import type { Task } from '../utility-types' /** * Callback invoked when the worker has started successfully. + * + * @typeParam Worker - Type of worker. */ export type OnlineHandler = (this: Worker) => void /** * Callback invoked if the worker has received a message. + * + * @typeParam Worker - Type of worker. */ export type MessageHandler = ( this: Worker, @@ -17,6 +21,8 @@ export type MessageHandler = ( /** * Callback invoked if the worker raised an error. + * + * @typeParam Worker - Type of worker. */ export type ErrorHandler = ( this: Worker, @@ -25,6 +31,8 @@ export type ErrorHandler = ( /** * Callback invoked when the worker exits successfully. + * + * @typeParam Worker - Type of worker. */ export type ExitHandler = ( this: Worker, @@ -132,7 +140,7 @@ export interface WorkerInfo { /** * Worker type. */ - type: WorkerType + readonly type: WorkerType /** * Dynamic flag. */ @@ -144,7 +152,7 @@ export interface WorkerInfo { /** * Task function names. */ - taskFunctions?: string[] + taskFunctionNames?: string[] } /** @@ -171,6 +179,15 @@ export interface WorkerUsage { readonly elu: EventLoopUtilizationMeasurementStatistics } +/** + * Worker choice strategy data. + * + * @internal + */ +export interface StrategyData { + virtualTaskEndTimestamp?: number +} + /** * Worker interface. */ @@ -199,8 +216,14 @@ export interface IWorker { readonly once: (event: 'exit', handler: ExitHandler) => void } -export type EmptyQueueCallback = (workerId: number) => void -export type BackPressureCallback = EmptyQueueCallback +/** + * Worker node event detail. + * + * @internal + */ +export interface WorkerNodeEventDetail { + workerId: number +} /** * Worker node interface. @@ -209,7 +232,8 @@ export type BackPressureCallback = EmptyQueueCallback * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. * @internal */ -export interface IWorkerNode { +export interface IWorkerNode + extends EventTarget { /** * Worker. */ @@ -221,7 +245,12 @@ export interface IWorkerNode { /** * Worker usage statistics. */ - usage: WorkerUsage + readonly usage: WorkerUsage + /** + * Worker choice strategy data. + * This is used to store data that are specific to the worker choice strategy. + */ + strategyData?: StrategyData /** * Message channel (worker_threads only). */ @@ -231,18 +260,6 @@ export interface IWorkerNode { * This is the number of tasks that can be enqueued before the worker node has back pressure. */ tasksQueueBackPressureSize: number - /** - * Callback invoked when worker node tasks queue is back pressured. - * - * @param workerId - The worker id. - */ - onBackPressure?: EmptyQueueCallback - /** - * Callback invoked when worker node tasks queue is empty. - * - * @param workerId - The worker id. - */ - onEmptyQueue?: BackPressureCallback /** * Tasks queue size. * @@ -300,4 +317,11 @@ export interface IWorkerNode { * @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 }