X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=5439606d420f0abd165b84c49157808de230aee6;hb=563d12ded5ff23152ed70f6dc675f01eb7cd9ee5;hp=14c8dbe1e1d444f5404e8562c1c59ecf1585d869;hpb=028d05710d565c03bfb6a0931411f0955b81d250;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 14c8dbe1..5439606d 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -1,14 +1,19 @@ import type { MessageChannel } from 'node:worker_threads' +import type { EventEmitter } from 'node:events' import type { CircularArray } from '../circular-array' 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 +22,8 @@ export type MessageHandler = ( /** * Callback invoked if the worker raised an error. + * + * @typeParam Worker - Type of worker. */ export type ErrorHandler = ( this: Worker, @@ -25,6 +32,8 @@ export type ErrorHandler = ( /** * Callback invoked when the worker exits successfully. + * + * @typeParam Worker - Type of worker. */ export type ExitHandler = ( this: Worker, @@ -96,6 +105,14 @@ export interface TaskStatistics { * Maximum number of queued tasks. */ readonly maxQueued?: number + /** + * Number of sequentially stolen tasks. + */ + sequentiallyStolen: number + /** + * Number of stolen tasks. + */ + stolen: number /** * Number of failed tasks. */ @@ -128,7 +145,7 @@ export interface WorkerInfo { /** * Worker type. */ - type: WorkerType + readonly type: WorkerType /** * Dynamic flag. */ @@ -140,7 +157,7 @@ export interface WorkerInfo { /** * Task function names. */ - taskFunctions?: string[] + taskFunctionNames?: string[] } /** @@ -167,6 +184,15 @@ export interface WorkerUsage { readonly elu: EventLoopUtilizationMeasurementStatistics } +/** + * Worker choice strategy data. + * + * @internal + */ +export interface StrategyData { + virtualTaskEndTimestamp?: number +} + /** * Worker interface. */ @@ -189,12 +215,22 @@ export interface IWorker { /** * Registers a listener to the exit event that will only be performed once. * - * @param event - `'exit'`. + * @param event - The `'exit'` event. * @param handler - The exit handler. */ readonly once: (event: 'exit', handler: ExitHandler) => void } +/** + * Worker node event detail. + * + * @internal + */ +export interface WorkerNodeEventDetail { + workerId: number + workerNodeKey?: number +} + /** * Worker node interface. * @@ -202,7 +238,8 @@ export interface IWorker { * @typeParam Data - Type of data sent to the worker. This can only be structured-cloneable data. * @internal */ -export interface IWorkerNode { +export interface IWorkerNode + extends EventEmitter { /** * Worker. */ @@ -211,31 +248,24 @@ export interface IWorkerNode { * Worker info. */ readonly info: WorkerInfo - /** - * Message channel (worker_threads only). - */ - readonly messageChannel?: MessageChannel /** * Worker usage statistics. */ - usage: WorkerUsage + readonly usage: WorkerUsage /** - * Tasks queue back pressure size. - * This is the number of tasks that can be enqueued before the worker node has back pressure. + * Worker choice strategy data. + * This is used to store data that are specific to the worker choice strategy. */ - tasksQueueBackPressureSize: number + strategyData?: StrategyData /** - * Callback invoked when worker node tasks queue is back pressured. - * - * @param workerId - The worker id. + * Message channel (worker_threads only). */ - onBackPressure?: (workerId: number) => void + readonly messageChannel?: MessageChannel /** - * Callback invoked when worker node tasks queue is empty. - * - * @param workerId - The worker id. + * Tasks queue back pressure size. + * This is the number of tasks that can be enqueued before the worker node has back pressure. */ - onEmptyQueue?: (workerId: number) => void + tasksQueueBackPressureSize: number /** * Tasks queue size. * @@ -293,4 +323,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 }