X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fworker.ts;h=3c44f05eae148883c5c83f91304f894d2297e5fc;hb=2809112e53951451aeb76e86a2dcc263ba80e43c;hp=58610eaaddacc2beea93809f80675bb8530010c0;hpb=5c4d16da7677746e563fdcfe7f82cbb842d1c9e6;p=poolifier.git diff --git a/src/pools/worker.ts b/src/pools/worker.ts index 58610eaa..3c44f05e 100644 --- a/src/pools/worker.ts +++ b/src/pools/worker.ts @@ -1,6 +1,12 @@ +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. */ @@ -17,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. */ @@ -105,8 +106,8 @@ export interface TaskStatistics { * Enumeration of worker types. */ export const WorkerTypes = Object.freeze({ - cluster: 'cluster', - thread: 'thread' + thread: 'thread', + cluster: 'cluster' } as const) /** @@ -136,6 +137,10 @@ export interface WorkerInfo { * Ready flag. */ ready: boolean + /** + * Task function names. + */ + taskFunctions?: string[] } /** @@ -177,9 +182,9 @@ 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. @@ -206,6 +211,10 @@ export interface IWorkerNode { * Worker info. */ readonly info: WorkerInfo + /** + * Message channel. + */ + readonly messageChannel?: MessageChannel /** * Worker usage statistics. */ @@ -233,12 +242,25 @@ export interface IWorkerNode { * Clears tasks queue. */ readonly clearTasksQueue: () => void + /** + * Whether the worker node has back pressure. + * + * @returns `true` if the worker node has back pressure, `false` otherwise. + */ + readonly hasBackPressure: () => boolean /** * Resets usage statistics . */ readonly resetUsage: () => void /** - * Gets task 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 getTaskWorkerUsage: (name: string) => WorkerUsage | undefined }