From e0ae6100cd739a48d38b4794c1e55437ac3b59eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 18 Apr 2023 20:22:00 +0200 Subject: [PATCH] refactor: untangle worker selection code from pool code 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 | 27 +++--------- src/pools/pool.ts | 20 --------- .../abstract-worker-choice-strategy.ts | 41 ++++++++++++++++++- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 67a22ffe..b299bc01 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -296,28 +296,11 @@ export abstract class AbstractPool< protected abstract get busy (): boolean protected internalBusy (): boolean { - return this.findFreeWorkerNodeKey() === -1 - } - - /** @inheritDoc */ - public findFreeWorkerNodeKey (): number { - return this.workerNodes.findIndex(workerNode => { - return workerNode.tasksUsage?.running === 0 - }) - } - - /** @inheritDoc */ - public findLastFreeWorkerNodeKey (): number { - // It requires node >= 18.0.0 - // return this.workerNodes.findLastIndex(workerNode => { - // return workerNode.tasksUsage?.running === 0 - // }) - for (let i = this.workerNodes.length - 1; i >= 0; i--) { - if (this.workerNodes[i].tasksUsage?.running === 0) { - return i - } - } - return -1 + return ( + this.workerNodes.findIndex(workerNode => { + return workerNode.tasksUsage?.running === 0 + }) === -1 + ) } /** @inheritDoc */ diff --git a/src/pools/pool.ts b/src/pools/pool.ts index bf2ac2c2..f6188d8e 100644 --- a/src/pools/pool.ts +++ b/src/pools/pool.ts @@ -140,26 +140,6 @@ export interface IPool< * - `'busy'`: Emitted when the pool is busy. */ readonly emitter?: PoolEmitter - /** - * Finds the first free worker node key based on the number of tasks the worker has applied. - * - * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. - * - * If no free worker is found, `-1` is returned. - * - * @returns A worker node key if there is one, `-1` otherwise. - */ - findFreeWorkerNodeKey: () => number - /** - * Finds the last free worker node key based on the number of tasks the worker has applied. - * - * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. - * - * If no free worker is found, `-1` is returned. - * - * @returns A worker node key if there is one, `-1` otherwise. - */ - findLastFreeWorkerNodeKey: () => number /** * Executes the function specified in the worker constructor with the task data input parameter. * diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index bddb8b76..7eced8d5 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -81,9 +81,46 @@ export abstract class AbstractWorkerChoiceStrategy< protected findFreeWorkerNodeKey (): number { if (this.toggleFindLastFreeWorkerNodeKey) { this.toggleFindLastFreeWorkerNodeKey = false - return this.pool.findLastFreeWorkerNodeKey() + return this.findLastFreeWorkerNodeKey() } this.toggleFindLastFreeWorkerNodeKey = true - return this.pool.findFreeWorkerNodeKey() + return this.findFirstFreeWorkerNodeKey() + } + + /** + * Finds the first free worker node key based on the number of tasks the worker has applied. + * + * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. + * + * If no free worker is found, `-1` is returned. + * + * @returns A worker node key if there is one, `-1` otherwise. + */ + private findFirstFreeWorkerNodeKey (): number { + return this.pool.workerNodes.findIndex(workerNode => { + return workerNode.tasksUsage?.running === 0 + }) + } + + /** + * Finds the last free worker node key based on the number of tasks the worker has applied. + * + * If a worker is found with `0` running tasks, it is detected as free and its worker node key is returned. + * + * If no free worker is found, `-1` is returned. + * + * @returns A worker node key if there is one, `-1` otherwise. + */ + private findLastFreeWorkerNodeKey (): number { + // It requires node >= 18.0.0 + // return this.workerNodes.findLastIndex(workerNode => { + // return workerNode.tasksUsage?.running === 0 + // }) + for (let i = this.pool.workerNodes.length - 1; i >= 0; i--) { + if (this.pool.workerNodes[i].tasksUsage?.running === 0) { + return i + } + } + return -1 } } -- 2.34.1