From 675bb8097982bce5bbe64e6332340373aa4aed3f Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 10 Oct 2022 14:33:02 +0200 Subject: [PATCH] Fix task runtime statistics usage calculation logic 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 | 12 +++++++----- src/pools/pool.ts | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index e204e124..0136f048 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -226,7 +226,7 @@ export abstract class AbstractPool< } /** - * Shut down given worker. + * Shutdowns given worker. * * @param worker A worker within `workers`. */ @@ -283,7 +283,7 @@ export abstract class AbstractPool< } /** - * Choose a worker for the next task. + * Chooses a worker for the next task. * * The default implementation uses a round robin algorithm to distribute the load. * @@ -294,7 +294,7 @@ export abstract class AbstractPool< } /** - * Send a message to the given worker. + * Sends a message to the given worker. * * @param worker The worker which should receive the message. * @param message The message. @@ -457,9 +457,11 @@ export abstract class AbstractPool< .requiredStatistics.runTime === true ) { const tasksUsage = this.workersTasksUsage.get(worker) - if (tasksUsage !== undefined && tasksUsage.run !== 0) { + if (tasksUsage !== undefined) { tasksUsage.runTime += taskRunTime ?? 0 - tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run + if (tasksUsage.run !== 0) { + tasksUsage.avgRunTime = tasksUsage.runTime / tasksUsage.run + } this.workersTasksUsage.set(worker, tasksUsage) } else { throw new Error(WORKER_NOT_FOUND_TASKS_USAGE_MAP) diff --git a/src/pools/pool.ts b/src/pools/pool.ts index 8da89156..6f677da8 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -53,7 +53,7 @@ export interface IPool { */ execute(data: Data): Promise /** - * Shut down every current worker in this pool. + * Shutdowns every current worker in this pool. */ destroy(): Promise /** -- 2.34.1