X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fleast-used-worker-choice-strategy.ts;h=c750c290d1d7f3c5039698a9be04a2971b623781;hb=edf8ce4e11eb0105038fcc236a2049c16b6175d2;hp=e8a7218e160879bcaa97399d78a3b39ff7857bc1;hpb=20016c79549983d09d30b70852ec7fae515d4156;p=poolifier.git diff --git a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts index e8a7218e..c750c290 100644 --- a/src/pools/selection-strategies/least-used-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/least-used-worker-choice-strategy.ts @@ -41,8 +41,9 @@ export class LeastUsedWorkerChoiceStrategy< } /** @inheritDoc */ - public choose (): number { - return this.leastUsedNextWorkerNodeKey() + public choose (): number | undefined { + this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey() + return this.nextWorkerNodeKey } /** @inheritDoc */ @@ -50,25 +51,23 @@ export class LeastUsedWorkerChoiceStrategy< return true } - private leastUsedNextWorkerNodeKey (): number { + private leastUsedNextWorkerNodeKey (): number | undefined { let minNumberOfTasks = Infinity + let chosenWorkerNodeKey: number | undefined for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) { const workerTaskStatistics = workerNode.usage.tasks const workerTasks = workerTaskStatistics.executed + workerTaskStatistics.executing + workerTaskStatistics.queued - if (this.isWorkerNodeReady(workerNodeKey) && workerTasks === 0) { - this.nextWorkerNodeKey = workerNodeKey + if (workerTasks === 0) { + chosenWorkerNodeKey = workerNodeKey break - } else if ( - this.isWorkerNodeReady(workerNodeKey) && - workerTasks < minNumberOfTasks - ) { + } else if (workerTasks < minNumberOfTasks) { minNumberOfTasks = workerTasks - this.nextWorkerNodeKey = workerNodeKey + chosenWorkerNodeKey = workerNodeKey } } - return this.nextWorkerNodeKey + return chosenWorkerNodeKey } }