}
/** @inheritDoc */
- public choose (): number {
+ public choose (): number | undefined {
+ this.setPreviousWorkerNodeKey(this.nextWorkerNodeKey)
+ this.nextWorkerNodeKey = this.leastUsedNextWorkerNodeKey()
+ return this.nextWorkerNodeKey
+ }
+
+ /** @inheritDoc */
+ public remove (): boolean {
+ return true
+ }
+
+ private leastUsedNextWorkerNodeKey (): number | undefined {
+ let chosenWorkerNodeKey: number | undefined
let minNumberOfTasks = Infinity
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
const workerTaskStatistics = workerNode.usage.tasks
workerTaskStatistics.executing +
workerTaskStatistics.queued
if (workerTasks === 0) {
- this.nextWorkerNodeId = workerNodeKey
+ chosenWorkerNodeKey = workerNodeKey
break
} else if (workerTasks < minNumberOfTasks) {
minNumberOfTasks = workerTasks
- this.nextWorkerNodeId = workerNodeKey
+ chosenWorkerNodeKey = workerNodeKey
}
}
- return this.nextWorkerNodeId
- }
-
- /** @inheritDoc */
- public remove (): boolean {
- return true
+ return chosenWorkerNodeKey
}
}