X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fworker%2Fabstract-worker.ts;h=63d14b2ed2dbd2a592451a7d8363564f8d6c9fbe;hb=eb7bf7441d410ca5d9ff9bb08f191ef22399371c;hp=f42f2c473aaaf78e926e029b22b67d82c11b8d54;hpb=448ad5810fdecae4747d35d64b3f6e016ed264f1;p=poolifier.git diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index f42f2c47..63d14b2e 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -36,6 +36,10 @@ export abstract class AbstractWorker< Data = unknown, Response = unknown > extends AsyncResource { + /** + * Worker id. + */ + protected abstract id: number /** * Task function(s) processed by the worker when the pool's `execution` function is invoked. */ @@ -90,8 +94,8 @@ export abstract class AbstractWorker< (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2 ) this.checkAlive.bind(this)() + this.mainWorker?.on('message', this.messageListener.bind(this)) } - this.mainWorker?.on('message', this.messageListener.bind(this)) } private checkWorkerOptions (opts: WorkerOptions): void { @@ -202,7 +206,7 @@ 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. - * @returns Message of the error. + * @returns The error message. */ protected handleError (e: Error | string): string { return e as string @@ -225,12 +229,14 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, + workerId: this.id, id: message.id }) } catch (e) { const err = this.handleError(e as Error) this.sendToMainWorker({ taskError: { + workerId: this.id, message: err, data: message.data }, @@ -258,6 +264,7 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, + workerId: this.id, id: message.id }) return null @@ -266,6 +273,7 @@ export abstract class AbstractWorker< const err = this.handleError(e as Error) this.sendToMainWorker({ taskError: { + workerId: this.id, message: err, data: message.data },