X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=f0ef59327c6099bc3719be1d84b537b0496fdd22;hb=37b32d84b2a0d8f8addad8c5e6a306c7dd22d58d;hp=5b3c3a3f06596b00fad84e691875b438325bc600;hpb=edd5088275648b8ae534dcd75ba23443fe947954;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 5b3c3a3f..f0ef5932 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -99,11 +99,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 { @@ -125,7 +152,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. */ @@ -149,7 +176,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) @@ -460,11 +487,11 @@ export abstract class AbstractWorker< /** * Handles an error and convert it to a string so it can be sent back to the main worker. * - * @param e - The error raised by the worker. + * @param error - The error raised by the worker. * @returns The error message. */ - protected handleError (e: Error | string): string { - return e instanceof Error ? e.message : e + protected handleError (error: Error | string): string { + return error instanceof Error ? error.message : error } /** @@ -516,12 +543,11 @@ export abstract class AbstractWorker< workerId: this.id, taskId }) - } catch (e) { - const errorMessage = this.handleError(e as Error | string) + } catch (error) { this.sendToMainWorker({ taskError: { name: name as string, - message: errorMessage, + message: this.handleError(error as Error | string), data }, workerId: this.id, @@ -555,12 +581,11 @@ export abstract class AbstractWorker< }) return null }) - .catch(e => { - const errorMessage = this.handleError(e as Error | string) + .catch(error => { this.sendToMainWorker({ taskError: { name: name as string, - message: errorMessage, + message: this.handleError(error as Error | string), data }, workerId: this.id,