From e221309acbe375b6e06fd05f355491f699d1202d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 19 Jul 2023 20:03:13 +0200 Subject: [PATCH] refactor: add helper to get worker info by worker MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 5481099f..10a2d514 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -910,7 +910,7 @@ export abstract class AbstractPool< void (this.destroyWorker(worker) as Promise) } }) - const workerInfo = this.getWorkerInfo(this.getWorkerNodeKey(worker)) + const workerInfo = this.getWorkerInfoByWorker(worker) workerInfo.dynamic = true if (this.workerChoiceStrategyContext.getStrategyPolicy().useDynamicWorker) { workerInfo.ready = true @@ -953,7 +953,7 @@ export abstract class AbstractPool< private sendWorkerStartupMessage (worker: Worker): void { this.sendToWorker(worker, { ready: false, - workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number + workerId: this.getWorkerInfoByWorker(worker).id as number }) } @@ -1006,9 +1006,9 @@ export abstract class AbstractPool< } private handleWorkerReadyResponse (message: MessageValue): void { - const worker = this.getWorkerById(message.workerId) - this.getWorkerInfo(this.getWorkerNodeKey(worker as Worker)).ready = - message.ready as boolean + this.getWorkerInfoByWorker( + this.getWorkerById(message.workerId) as Worker + ).ready = message.ready as boolean if (this.emitter != null && this.ready) { this.emitter.emit(PoolEvents.ready, this.info) } @@ -1051,7 +1051,7 @@ export abstract class AbstractPool< } /** - * Gets the worker information. + * Gets the worker information from the given worker node key. * * @param workerNodeKey - The worker node key. */ @@ -1059,6 +1059,15 @@ export abstract class AbstractPool< return this.workerNodes[workerNodeKey].info } + /** + * Gets the worker information from the given worker. + * + * @param worker - The worker. + */ + private getWorkerInfoByWorker (worker: Worker): WorkerInfo { + return this.workerNodes[this.getWorkerNodeKey(worker)].info + } + /** * Adds the given worker in the pool worker nodes. * @@ -1135,7 +1144,7 @@ export abstract class AbstractPool< elu: this.workerChoiceStrategyContext.getTaskStatisticsRequirements() .elu.aggregate }, - workerId: this.getWorkerInfo(this.getWorkerNodeKey(worker)).id as number + workerId: this.getWorkerInfoByWorker(worker).id as number }) } } -- 2.34.1