From: Jérôme Benoit Date: Wed, 13 Sep 2023 20:29:25 +0000 (+0200) Subject: refactor: cleanup workerId usage in messaging X-Git-Tag: v2.7.0~1^2~37 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=895b5341fc326345504591f709cf2fda7e11257a;hp=-c;p=poolifier.git refactor: cleanup workerId usage in messaging Signed-off-by: Jérôme Benoit --- 895b5341fc326345504591f709cf2fda7e11257a diff --git a/src/worker/abstract-worker.ts b/src/worker/abstract-worker.ts index 107d7238..11a2263a 100644 --- a/src/worker/abstract-worker.ts +++ b/src/worker/abstract-worker.ts @@ -371,8 +371,7 @@ export abstract class AbstractWorker< workerError: { name: taskFunctionName as string, message: this.handleError(response.error as Error | string) - }, - workerId: this.id + } }) } @@ -386,11 +385,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() @@ -400,9 +399,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() } @@ -454,7 +453,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 }) } } @@ -485,8 +484,7 @@ export abstract class AbstractWorker< */ protected sendTaskFunctionNamesToMainWorker (): void { this.sendToMainWorker({ - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } @@ -516,7 +514,6 @@ export abstract class AbstractWorker< message: `Task function '${name as string}' not found`, data }, - workerId: this.id, taskId }) return @@ -546,7 +543,6 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, - workerId: this.id, taskId }) } catch (error) { @@ -556,7 +552,6 @@ export abstract class AbstractWorker< message: this.handleError(error as Error | string), data }, - workerId: this.id, taskId }) } finally { @@ -582,7 +577,6 @@ export abstract class AbstractWorker< this.sendToMainWorker({ data: res, taskPerformance, - workerId: this.id, taskId }) return null @@ -594,7 +588,6 @@ export abstract class AbstractWorker< message: this.handleError(error as Error | string), data }, - workerId: this.id, taskId }) }) diff --git a/src/worker/cluster-worker.ts b/src/worker/cluster-worker.ts index 1f9895d6..201a516c 100644 --- a/src/worker/cluster-worker.ts +++ b/src/worker/cluster-worker.ts @@ -48,14 +48,12 @@ export class ClusterWorker< this.getMainWorker().on('message', this.messageListener.bind(this)) this.sendToMainWorker({ ready: true, - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } catch { this.sendToMainWorker({ ready: false, - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } } @@ -68,6 +66,6 @@ export class ClusterWorker< /** @inheritDoc */ protected sendToMainWorker (message: MessageValue): void { - this.getMainWorker().send(message) + this.getMainWorker().send({ ...message, workerId: this.id }) } } diff --git a/src/worker/thread-worker.ts b/src/worker/thread-worker.ts index ede0b3b5..ec6b6a0a 100644 --- a/src/worker/thread-worker.ts +++ b/src/worker/thread-worker.ts @@ -62,14 +62,12 @@ export class ThreadWorker< this.port.on('message', this.messageListener.bind(this)) this.sendToMainWorker({ ready: true, - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } catch { this.sendToMainWorker({ ready: false, - taskFunctionNames: this.listTaskFunctionNames(), - workerId: this.id + taskFunctionNames: this.listTaskFunctionNames() }) } } @@ -89,7 +87,7 @@ export class ThreadWorker< /** @inheritDoc */ protected sendToMainWorker (message: MessageValue): void { - this.port.postMessage(message) + this.port.postMessage({ ...message, workerId: this.id }) } /** @inheritDoc */