X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=5439606d420f0abd165b84c49157808de230aee6;hb=fa548cda5120ac0708d82f37cd0ce1260d7f96c1;hp=bfd73325c3204d48c656924367500b9f30efbc1d;hpb=26fb3c18b678a1daab6b18a351a238fb5a3ed5df;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index bfd73325..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,10 @@ export interface TaskStatistics { * Maximum number of queued tasks. */ readonly maxQueued?: number + /** + * Number of sequentially stolen tasks. + */ + sequentiallyStolen: number /** * Number of stolen tasks. */ @@ -132,7 +145,7 @@ export interface WorkerInfo { /** * Worker type. */ - type: WorkerType + readonly type: WorkerType /** * Dynamic flag. */ @@ -144,7 +157,7 @@ export interface WorkerInfo { /** * Task function names. */ - taskFunctions?: string[] + taskFunctionNames?: string[] } /** @@ -171,6 +184,15 @@ export interface WorkerUsage { readonly elu: EventLoopUtilizationMeasurementStatistics } +/** + * Worker choice strategy data. + * + * @internal + */ +export interface StrategyData { + virtualTaskEndTimestamp?: number +} + /** * Worker interface. */ @@ -199,6 +221,16 @@ export interface IWorker { readonly once: (event: 'exit', handler: ExitHandler) => void } +/** + * Worker node event detail. + * + * @internal + */ +export interface WorkerNodeEventDetail { + workerId: number + workerNodeKey?: number +} + /** * Worker node interface. * @@ -206,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. */ @@ -218,7 +251,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). */ @@ -228,18 +266,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?: (workerId: number) => void - /** - * Callback invoked when worker node tasks queue is empty. - * - * @param workerId - The worker id. - */ - onEmptyQueue?: (workerId: number) => void /** * Tasks queue size. * @@ -297,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 }