X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=bc62ade55ef15efac79c8c9e8835a45cb697aa40;hb=08416934cb21e9dca8281c9e7121d3863cc742bf;hp=1b64dc4f4cfff7c769ade8ef2bbd100efa1885ab;hpb=6703b9f4492e347500111c42ffddbd8341c8f262;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 1b64dc4f..bc62ade5 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -18,15 +18,11 @@ import { KillBehaviors, type WorkerOptions } from './worker-options' import type { TaskAsyncFunction, TaskFunction, + TaskFunctionOperationReturnType, TaskFunctions, TaskSyncFunction } from './task-functions' -interface TaskFunctionOperationReturnType { - status: boolean - error?: Error -} - const DEFAULT_MAX_INACTIVE_TIME = 60000 const DEFAULT_WORKER_OPTIONS: WorkerOptions = { /** @@ -104,11 +100,38 @@ export abstract class AbstractWorker< } private checkWorkerOptions (opts: WorkerOptions): void { + if (opts != null && !isPlainObject(opts)) { + throw new TypeError('opts worker options parameter is not a plain object') + } + if ( + opts?.killBehavior != null && + !Object.values(KillBehaviors).includes(opts.killBehavior) + ) { + throw new TypeError( + `killBehavior option '${opts.killBehavior}' is not valid` + ) + } + if ( + opts?.maxInactiveTime != null && + !Number.isSafeInteger(opts.maxInactiveTime) + ) { + throw new TypeError('maxInactiveTime option is not an integer') + } + if (opts?.maxInactiveTime != null && opts.maxInactiveTime < 5) { + throw new TypeError( + 'maxInactiveTime option is not a positive integer greater or equal than 5' + ) + } + if (opts?.killHandler != null && typeof opts.killHandler !== 'function') { + throw new TypeError('killHandler option is not a function') + } + if (opts?.async != null) { + throw new Error('async option is deprecated') + } this.opts = { ...DEFAULT_WORKER_OPTIONS, ...opts } - delete this.opts.async } - private checkValidTaskFunction ( + private checkValidTaskFunctionEntry ( name: string, fn: TaskFunction ): void { @@ -130,7 +153,7 @@ export abstract class AbstractWorker< } /** - * Checks if the `taskFunctions` parameter is passed to the constructor. + * Checks if the `taskFunctions` parameter is passed to the constructor and valid. * * @param taskFunctions - The task function(s) parameter that should be checked. */ @@ -154,7 +177,7 @@ export abstract class AbstractWorker< } else if (isPlainObject(taskFunctions)) { let firstEntry = true for (const [name, fn] of Object.entries(taskFunctions)) { - this.checkValidTaskFunction(name, fn) + this.checkValidTaskFunctionEntry(name, fn) const boundFn = fn.bind(this) if (firstEntry) { this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) @@ -217,7 +240,7 @@ export abstract class AbstractWorker< this.taskFunctions.set(DEFAULT_TASK_NAME, boundFn) } this.taskFunctions.set(name, boundFn) - this.sendTaskFunctionsListToMainWorker() + this.sendTaskFunctionNamesToMainWorker() return { status: true } } catch (error) { return { status: false, error: error as Error } @@ -247,7 +270,7 @@ export abstract class AbstractWorker< ) } const deleteStatus = this.taskFunctions.delete(name) - this.sendTaskFunctionsListToMainWorker() + this.sendTaskFunctionNamesToMainWorker() return { status: deleteStatus } } catch (error) { return { status: false, error: error as Error } @@ -355,9 +378,7 @@ export abstract class AbstractWorker< ): void { const { taskFunctionOperation, taskFunction, taskFunctionName } = message let response!: TaskFunctionOperationReturnType - if (taskFunctionOperation === 'has') { - response = this.hasTaskFunction(taskFunctionName as string) - } else if (taskFunctionOperation === 'add') { + if (taskFunctionOperation === 'add') { response = this.addTaskFunction( taskFunctionName as string, // eslint-disable-next-line @typescript-eslint/no-implied-eval, no-new-func @@ -377,8 +398,7 @@ export abstract class AbstractWorker< workerError: { name: taskFunctionName as string, message: this.handleError(response.error as Error | string) - }, - workerId: this.id + } }) } @@ -392,11 +412,11 @@ export abstract class AbstractWorker< if (isAsyncFunction(this.opts.killHandler)) { (this.opts.killHandler?.() as Promise) .then(() => { - this.sendToMainWorker({ kill: 'success', workerId: this.id }) + this.sendToMainWorker({ kill: 'success' }) return null }) .catch(() => { - this.sendToMainWorker({ kill: 'failure', workerId: this.id }) + this.sendToMainWorker({ kill: 'failure' }) }) .finally(() => { this.emitDestroy() @@ -406,9 +426,9 @@ export abstract class AbstractWorker< try { // eslint-disable-next-line @typescript-eslint/no-invalid-void-type this.opts.killHandler?.() as void - this.sendToMainWorker({ kill: 'success', workerId: this.id }) + this.sendToMainWorker({ kill: 'success' }) } catch { - this.sendToMainWorker({ kill: 'failure', workerId: this.id }) + this.sendToMainWorker({ kill: 'failure' }) } finally { this.emitDestroy() } @@ -460,7 +480,7 @@ export abstract class AbstractWorker< performance.now() - this.lastTaskTimestamp > (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) ) { - this.sendToMainWorker({ kill: this.opts.killBehavior, workerId: this.id }) + this.sendToMainWorker({ kill: this.opts.killBehavior }) } } @@ -487,12 +507,11 @@ export abstract class AbstractWorker< ): void /** - * Sends the list of task function names to the main worker. + * Sends task function names to the main worker. */ - protected sendTaskFunctionsListToMainWorker (): void { + protected sendTaskFunctionNamesToMainWorker (): void { this.sendToMainWorker({ - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } @@ -522,7 +541,6 @@ export abstract class AbstractWorker< message: `Task function '${name as string}' not found`, data }, - workerId: this.id, taskId }) return @@ -552,7 +570,6 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, - workerId: this.id, taskId }) } catch (error) { @@ -562,7 +579,6 @@ export abstract class AbstractWorker< message: this.handleError(error as Error | string), data }, - workerId: this.id, taskId }) } finally { @@ -588,7 +604,6 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, - workerId: this.id, taskId }) return null @@ -600,7 +615,6 @@ export abstract class AbstractWorker< message: this.handleError(error as Error | string), data }, - workerId: this.id, taskId }) })