X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=6c6de861156e040285fc3a4ad4c50ba5e796f04f;hb=adee605399485348ae224e7e4c022f024373b0ef;hp=11a2263a5b5238ac4f728daa8ed97f6ca013c86f;hpb=001cc6a4bd787b8ec8efab54674789018a6b6690;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 11a2263a..6c6de861 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -100,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 { @@ -126,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. */ @@ -150,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) @@ -299,6 +326,7 @@ export abstract class AbstractWorker< DEFAULT_TASK_NAME, this.taskFunctions.get(name) as TaskFunction ) + this.sendTaskFunctionNamesToMainWorker() return { status: true } } catch (error) { return { status: false, error: error as Error } @@ -349,7 +377,7 @@ export abstract class AbstractWorker< protected handleTaskFunctionOperationMessage ( message: MessageValue ): void { - const { taskFunctionOperation, taskFunction, taskFunctionName } = message + const { taskFunctionOperation, taskFunctionName, taskFunction } = message let response!: TaskFunctionOperationReturnType if (taskFunctionOperation === 'add') { response = this.addTaskFunction( @@ -368,10 +396,14 @@ export abstract class AbstractWorker< this.sendToMainWorker({ taskFunctionOperation, taskFunctionOperationStatus: response.status, - workerError: { - name: taskFunctionName as string, - message: this.handleError(response.error as Error | string) - } + taskFunctionName, + ...(!response.status && + response?.error != null && { + workerError: { + name: taskFunctionName as string, + message: this.handleError(response.error as Error | string) + } + }) }) } @@ -579,7 +611,7 @@ export abstract class AbstractWorker< taskPerformance, taskId }) - return null + return undefined }) .catch(error => { this.sendToMainWorker({