X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=03cecce849a2cb9d71ad04a566795d3588238509;hb=d91689fda0fa7a85014ac25276cf2cf0a9d81ce2;hp=b7cd7f6799b9fe5b3c5da9c35be9d417a700c1b7;hpb=85aeb3f356d749b96361e74cf17d403a697e3dd7;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index b7cd7f67..03cecce8 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -2,6 +2,11 @@ import type { MessageChannel } from 'node:worker_threads' import type { CircularArray } from '../circular-array' import type { Task } from '../utility-types' +/** + * Callback invoked when the worker has started successfully. + */ +export type OnlineHandler = (this: Worker) => void + /** * Callback invoked if the worker has received a message. */ @@ -18,11 +23,6 @@ export type ErrorHandler = ( error: Error ) => void -/** - * Callback invoked when the worker has started successfully. - */ -export type OnlineHandler = (this: Worker) => void - /** * Callback invoked when the worker exits successfully. */ @@ -96,6 +96,10 @@ export interface TaskStatistics { * Maximum number of queued tasks. */ readonly maxQueued?: number + /** + * Number of stolen tasks. + */ + stolen: number /** * Number of failed tasks. */ @@ -106,8 +110,8 @@ export interface TaskStatistics { * Enumeration of worker types. */ export const WorkerTypes = Object.freeze({ - cluster: 'cluster', - thread: 'thread' + thread: 'thread', + cluster: 'cluster' } as const) /** @@ -128,7 +132,7 @@ export interface WorkerInfo { /** * Worker type. */ - type: WorkerType + readonly type: WorkerType /** * Dynamic flag. */ @@ -138,9 +142,9 @@ export interface WorkerInfo { */ ready: boolean /** - * Message channel. + * Task function names. */ - messageChannel?: MessageChannel + taskFunctionNames?: string[] } /** @@ -167,6 +171,15 @@ export interface WorkerUsage { readonly elu: EventLoopUtilizationMeasurementStatistics } +/** + * Worker choice strategy data. + * + * @internal + */ +export interface StrategyData { + virtualTaskEndTimestamp?: number +} + /** * Worker interface. */ @@ -182,19 +195,28 @@ export interface IWorker { * @param event - The event. * @param handler - The event handler. */ - readonly on: ((event: 'message', handler: MessageHandler) => void) & + readonly on: ((event: 'online', handler: OnlineHandler) => void) & + ((event: 'message', handler: MessageHandler) => void) & ((event: 'error', handler: ErrorHandler) => void) & - ((event: 'online', handler: OnlineHandler) => void) & ((event: 'exit', handler: ExitHandler) => void) /** * 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 +} + /** * Worker node interface. * @@ -202,7 +224,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 EventTarget { /** * Worker. */ @@ -214,7 +237,21 @@ 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). + */ + readonly messageChannel?: MessageChannel + /** + * Tasks queue back pressure size. + * This is the number of tasks that can be enqueued before the worker node has back pressure. + */ + tasksQueueBackPressureSize: number /** * Tasks queue size. * @@ -225,25 +262,58 @@ export interface IWorkerNode { * Enqueue task. * * @param task - The task to queue. - * @returns The task queue size. + * @returns The tasks queue size. */ readonly enqueueTask: (task: Task) => number + /** + * Prepends a task to the tasks queue. + * + * @param task - The task to prepend. + * @returns The tasks queue size. + */ + readonly unshiftTask: (task: Task) => number /** * Dequeue task. * * @returns The dequeued task. */ readonly dequeueTask: () => Task | undefined + /** + * Pops a task from the tasks queue. + * + * @returns The popped task. + */ + readonly popTask: () => Task | undefined /** * Clears tasks queue. */ readonly clearTasksQueue: () => void /** - * Resets usage statistics . + * Whether the worker node has back pressure (i.e. its tasks queue is full). + * + * @returns `true` if the worker node has back pressure, `false` otherwise. + */ + readonly hasBackPressure: () => boolean + /** + * Resets usage statistics. */ readonly resetUsage: () => void /** - * Gets task worker usage statistics. + * Closes communication channel. + */ + readonly closeChannel: () => void + /** + * Gets task function worker usage statistics. + * + * @param name - The task function name. + * @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 getTaskWorkerUsage: (name: string) => WorkerUsage | undefined + readonly deleteTaskFunctionWorkerUsage: (name: string) => boolean }