X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=bb6ac5666e0252195b4f1cafbda38471ec7fcf07;hb=910416386b4f7d0da4e6f0d8551cefa2539c5ced;hp=6c7fd812097dea235462c124904a3b3e69812869;hpb=ea4b5fd037c5fe09eb6a2810332ad6bed1b5bc7f;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 6c7fd812..bb6ac566 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -5,10 +5,12 @@ import type { MessagePort } from 'node:worker_threads' import type { MessageValue, Task, + TaskFunctionProperties, TaskPerformance, WorkerStatistics } from '../utility-types.js' import { + buildTaskFunctionProperties, DEFAULT_TASK_NAME, EMPTY_FUNCTION, isAsyncFunction, @@ -17,13 +19,14 @@ import { import type { TaskAsyncFunction, TaskFunction, + TaskFunctionObject, TaskFunctionOperationResult, TaskFunctions, TaskSyncFunction } from './task-functions.js' import { checkTaskFunctionName, - checkValidTaskFunctionEntry, + checkValidTaskFunctionObjectEntry, checkValidWorkerOptions } from './utils.js' import { KillBehaviors, type WorkerOptions } from './worker-options.js' @@ -62,9 +65,9 @@ export abstract class AbstractWorker< */ protected abstract id: number /** - * Task function(s) processed by the worker when the pool's `execution` function is invoked. + * Task function object(s) processed by the worker when the pool's `execution` function is invoked. */ - protected taskFunctions!: Map> + protected taskFunctions!: Map> /** * Timestamp of the last task processed by this worker. */ @@ -122,27 +125,33 @@ export abstract class AbstractWorker< if (taskFunctions == null) { throw new Error('taskFunctions parameter is mandatory') } - this.taskFunctions = new Map>() + this.taskFunctions = new Map>() if (typeof taskFunctions === 'function') { - const boundFn = taskFunctions.bind(this) - this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) + const fnObj = { taskFunction: taskFunctions.bind(this) } + this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj) this.taskFunctions.set( typeof taskFunctions.name === 'string' && taskFunctions.name.trim().length > 0 ? taskFunctions.name : 'fn1', - boundFn + fnObj ) } else if (isPlainObject(taskFunctions)) { let firstEntry = true - for (const [name, fn] of Object.entries(taskFunctions)) { - checkValidTaskFunctionEntry(name, fn) - const boundFn = fn.bind(this) + for (let [name, fnObj] of Object.entries(taskFunctions)) { + if (typeof fnObj === 'function') { + fnObj = { taskFunction: fnObj } satisfies TaskFunctionObject< + Data, + Response + > + } + checkValidTaskFunctionObjectEntry(name, fnObj) + fnObj.taskFunction = fnObj.taskFunction.bind(this) if (firstEntry) { - this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) + this.taskFunctions.set(DEFAULT_TASK_NAME, fnObj) firstEntry = false } - this.taskFunctions.set(name, boundFn) + this.taskFunctions.set(name, fnObj) } if (firstEntry) { throw new Error('taskFunctions parameter object is empty') @@ -179,7 +188,7 @@ export abstract class AbstractWorker< */ public addTaskFunction ( name: string, - fn: TaskFunction + fn: TaskFunction | TaskFunctionObject ): TaskFunctionOperationResult { try { checkTaskFunctionName(name) @@ -188,18 +197,19 @@ export abstract class AbstractWorker< 'Cannot add a task function with the default reserved name' ) } - if (typeof fn !== 'function') { - throw new TypeError('fn parameter is not a function') + if (typeof fn === 'function') { + fn = { taskFunction: fn } satisfies TaskFunctionObject } - const boundFn = fn.bind(this) + checkValidTaskFunctionObjectEntry(name, fn) + fn.taskFunction = fn.taskFunction.bind(this) if ( this.taskFunctions.get(name) === this.taskFunctions.get(DEFAULT_TASK_NAME) ) { - this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) + this.taskFunctions.set(DEFAULT_TASK_NAME, fn) } - this.taskFunctions.set(name, boundFn) - this.sendTaskFunctionNamesToMainWorker() + this.taskFunctions.set(name, fn) + this.sendTaskFunctionsPropertiesToMainWorker() return { status: true } } catch (error) { return { status: false, error: error as Error } @@ -229,7 +239,7 @@ export abstract class AbstractWorker< ) } const deleteStatus = this.taskFunctions.delete(name) - this.sendTaskFunctionNamesToMainWorker() + this.sendTaskFunctionsPropertiesToMainWorker() return { status: deleteStatus } } catch (error) { return { status: false, error: error as Error } @@ -237,28 +247,38 @@ export abstract class AbstractWorker< } /** - * Lists the names of the worker's task functions. + * Lists the properties of the worker's task functions. * - * @returns The names of the worker's task functions. + * @returns The properties of the worker's task functions. */ - public listTaskFunctionNames (): string[] { - const names = [...this.taskFunctions.keys()] + public listTaskFunctionsProperties (): TaskFunctionProperties[] { let defaultTaskFunctionName = DEFAULT_TASK_NAME - for (const [name, fn] of this.taskFunctions) { + for (const [name, fnObj] of this.taskFunctions) { if ( name !== DEFAULT_TASK_NAME && - fn === this.taskFunctions.get(DEFAULT_TASK_NAME) + fnObj === this.taskFunctions.get(DEFAULT_TASK_NAME) ) { defaultTaskFunctionName = name break } } + const taskFunctionsProperties: TaskFunctionProperties[] = [] + for (const [name, fnObj] of this.taskFunctions) { + if (name === DEFAULT_TASK_NAME || name === defaultTaskFunctionName) { + continue + } + taskFunctionsProperties.push(buildTaskFunctionProperties(name, fnObj)) + } return [ - names[names.indexOf(DEFAULT_TASK_NAME)], - defaultTaskFunctionName, - ...names.filter( - name => name !== DEFAULT_TASK_NAME && name !== defaultTaskFunctionName - ) + buildTaskFunctionProperties( + DEFAULT_TASK_NAME, + this.taskFunctions.get(DEFAULT_TASK_NAME) + ), + buildTaskFunctionProperties( + defaultTaskFunctionName, + this.taskFunctions.get(defaultTaskFunctionName) + ), + ...taskFunctionsProperties ] } @@ -283,7 +303,7 @@ export abstract class AbstractWorker< } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.taskFunctions.set(DEFAULT_TASK_NAME, this.taskFunctions.get(name)!) - this.sendTaskFunctionNamesToMainWorker() + this.sendTaskFunctionsPropertiesToMainWorker() return { status: true } } catch (error) { return { status: false, error: error as Error } @@ -304,19 +324,27 @@ export abstract class AbstractWorker< */ protected messageListener (message: MessageValue): void { this.checkMessageWorkerId(message) - if (message.statistics != null) { + const { + statistics, + checkActive, + taskFunctionOperation, + taskId, + data, + kill + } = message + if (statistics != null) { // Statistics message received - this.statistics = message.statistics - } else if (message.checkActive != null) { + this.statistics = statistics + } else if (checkActive != null) { // Check active message received - message.checkActive ? this.startCheckActive() : this.stopCheckActive() - } else if (message.taskFunctionOperation != null) { + checkActive ? this.startCheckActive() : this.stopCheckActive() + } else if (taskFunctionOperation != null) { // Task function operation message received this.handleTaskFunctionOperationMessage(message) - } else if (message.taskId != null && message.data != null) { + } else if (taskId != null && data != null) { // Task message received this.run(message) - } else if (message.kill === true) { + } else if (kill === true) { // Kill message received this.handleKillMessage(message) } @@ -325,29 +353,34 @@ export abstract class AbstractWorker< protected handleTaskFunctionOperationMessage ( message: MessageValue ): void { - const { taskFunctionOperation, taskFunctionName, taskFunction } = message - if (taskFunctionName == null) { + const { taskFunctionOperation, taskFunctionProperties, taskFunction } = + message + if (taskFunctionProperties == null) { throw new Error( - 'Cannot handle task function operation message without a task function name' + 'Cannot handle task function operation message without task function properties' ) } let response: TaskFunctionOperationResult switch (taskFunctionOperation) { case 'add': - response = this.addTaskFunction( - taskFunctionName, + response = this.addTaskFunction(taskFunctionProperties.name, { // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func - new Function(`return ${taskFunction}`)() as TaskFunction< - Data, - Response - > - ) + taskFunction: new Function( + `return ${taskFunction}` + )() as TaskFunction, + ...(taskFunctionProperties.priority != null && { + priority: taskFunctionProperties.priority + }), + ...(taskFunctionProperties.strategy != null && { + strategy: taskFunctionProperties.strategy + }) + }) break case 'remove': - response = this.removeTaskFunction(taskFunctionName) + response = this.removeTaskFunction(taskFunctionProperties.name) break case 'default': - response = this.setDefaultTaskFunction(taskFunctionName) + response = this.setDefaultTaskFunction(taskFunctionProperties.name) break default: response = { status: false, error: new Error('Unknown task operation') } @@ -356,11 +389,11 @@ export abstract class AbstractWorker< this.sendToMainWorker({ taskFunctionOperation, taskFunctionOperationStatus: response.status, - taskFunctionName, + taskFunctionProperties, ...(!response.status && response.error != null && { workerError: { - name: taskFunctionName, + name: taskFunctionProperties.name, message: this.handleError(response.error as Error | string) } }) @@ -466,11 +499,11 @@ export abstract class AbstractWorker< ): void /** - * Sends task function names to the main worker. + * Sends task functions properties to the main worker. */ - protected sendTaskFunctionNamesToMainWorker (): void { + protected sendTaskFunctionsPropertiesToMainWorker (): void { this.sendToMainWorker({ - taskFunctionNames: this.listTaskFunctionNames() + taskFunctionsProperties: this.listTaskFunctionsProperties() }) } @@ -504,7 +537,7 @@ export abstract class AbstractWorker< }) return } - const fn = this.taskFunctions.get(taskFunctionName) + const fn = this.taskFunctions.get(taskFunctionName)?.taskFunction if (isAsyncFunction(fn)) { this.runAsync(fn as TaskAsyncFunction, task) } else { @@ -569,7 +602,7 @@ export abstract class AbstractWorker< }) return undefined }) - .catch(error => { + .catch((error: unknown) => { this.sendToMainWorker({ workerError: { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion