X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=349940faea20418b2bfa954561f7d258cc8ce3b2;hb=b3432a63039e7cb70c0448da5518690e457cd47e;hp=520be7cd0041ee29604a8f79ddf046b78677e702;hpb=c365b2d36d1f13f5920d0523b4172f4b3470e896;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 520be7cd..349940fa 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -29,7 +29,10 @@ export abstract class AbstractWorker< * Handler Id of the `aliveInterval` worker alive check. */ protected readonly aliveInterval?: NodeJS.Timeout - + /** + * Options for the worker. + */ + public readonly opts: WorkerOptions /** * Constructs a new poolifier worker. * @@ -43,8 +46,8 @@ export abstract class AbstractWorker< type: string, isMain: boolean, fn: (data: Data) => Response, - protected mainWorker: MainWorker | null, - public readonly opts: WorkerOptions = { + protected mainWorker: MainWorker | undefined | null, + opts: WorkerOptions = { /** * The kill behavior option on this Worker or its default value. */ @@ -57,6 +60,7 @@ export abstract class AbstractWorker< } ) { super(type) + this.opts = opts this.checkFunctionInput(fn) this.checkWorkerOptions(this.opts) this.lastTaskTimestamp = Date.now() @@ -70,25 +74,32 @@ export abstract class AbstractWorker< } this.mainWorker?.on('message', (value: MessageValue) => { - if (value?.data && value.id) { - // Here you will receive messages - if (this.opts.async) { - this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) - } else { - this.runInAsyncScope(this.run.bind(this), this, fn, value) - } - } else if (value.parent) { - // Save a reference of the main worker to communicate with it - // This will be received once - this.mainWorker = value.parent - } else if (value.kill) { - // Here is time to kill this worker, just clearing the interval - if (this.aliveInterval) clearInterval(this.aliveInterval) - this.emitDestroy() - } + this.messageListener(value, fn) }) } + protected messageListener ( + value: MessageValue, + fn: (data: Data) => Response + ): void { + if (value.data !== undefined && value.id !== undefined) { + // Here you will receive messages + if (this.opts.async) { + this.runInAsyncScope(this.runAsync.bind(this), this, fn, value) + } else { + this.runInAsyncScope(this.run.bind(this), this, fn, value) + } + } else if (value.parent !== undefined) { + // Save a reference of the main worker to communicate with it + // This will be received once + this.mainWorker = value.parent + } else if (value.kill !== undefined) { + // Here is time to kill this worker, just clearing the interval + if (this.aliveInterval) clearInterval(this.aliveInterval) + this.emitDestroy() + } + } + private checkWorkerOptions (opts: WorkerOptions) { this.opts.killBehavior = opts.killBehavior ?? DEFAULT_KILL_BEHAVIOR this.opts.maxInactiveTime = @@ -146,7 +157,7 @@ export abstract class AbstractWorker< * @returns Message of the error. */ protected handleError (e: Error | string): string { - return (e as unknown) as string + return e as string } /** @@ -160,10 +171,12 @@ export abstract class AbstractWorker< value: MessageValue ): void { try { + const startTaskTimestamp = Date.now() const res = fn(value.data) - this.sendToMainWorker({ data: res, id: value.id }) + const taskRunTime = Date.now() - startTaskTimestamp + this.sendToMainWorker({ data: res, id: value.id, taskRunTime }) } catch (e) { - const err = this.handleError(e) + const err = this.handleError(e as Error) this.sendToMainWorker({ error: err, id: value.id }) } finally { this.lastTaskTimestamp = Date.now() @@ -180,13 +193,15 @@ export abstract class AbstractWorker< fn: (data?: Data) => Promise, value: MessageValue ): void { + const startTaskTimestamp = Date.now() fn(value.data) .then(res => { - this.sendToMainWorker({ data: res, id: value.id }) + const taskRunTime = Date.now() - startTaskTimestamp + this.sendToMainWorker({ data: res, id: value.id, taskRunTime }) return null }) .catch(e => { - const err = this.handleError(e) + const err = this.handleError(e as Error) this.sendToMainWorker({ error: err, id: value.id }) }) .finally(() => {