X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fpools%2Fselection-strategies%2Fabstract-worker-choice-strategy.ts;h=b5b77ff6e261519d2bbef3154ca701e059d77a16;hb=9a0613e995004ae9e2faa287a8dd92c2854141f1;hp=285a68c20c856efb15b084f94b593fc7d2a89dc2;hpb=b6b3245344bd453ea91fa3d74acd5145f70d84fd;p=poolifier.git diff --git a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts index 285a68c2..b5b77ff6 100644 --- a/src/pools/selection-strategies/abstract-worker-choice-strategy.ts +++ b/src/pools/selection-strategies/abstract-worker-choice-strategy.ts @@ -4,7 +4,7 @@ import type { IPool } from '../pool' import type { IWorker } from '../worker' import type { IWorkerChoiceStrategy, - TaskStatistics, + TaskStatisticsRequirements, WorkerChoiceStrategyOptions } from './selection-strategies-types' @@ -25,7 +25,7 @@ export abstract class AbstractWorkerChoiceStrategy< */ private toggleFindLastFreeWorkerNodeKey: boolean = false /** @inheritDoc */ - public readonly taskStatistics: TaskStatistics = { + public readonly taskStatisticsRequirements: TaskStatisticsRequirements = { runTime: false, avgRunTime: false, medRunTime: false, @@ -49,21 +49,33 @@ export abstract class AbstractWorkerChoiceStrategy< } protected setTaskStatistics (opts: WorkerChoiceStrategyOptions): void { - if (this.taskStatistics.avgRunTime && opts.medRunTime === true) { - this.taskStatistics.avgRunTime = false - this.taskStatistics.medRunTime = opts.medRunTime as boolean + if ( + this.taskStatisticsRequirements.avgRunTime && + opts.medRunTime === true + ) { + this.taskStatisticsRequirements.avgRunTime = false + this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean } - if (this.taskStatistics.medRunTime && opts.medRunTime === false) { - this.taskStatistics.avgRunTime = true - this.taskStatistics.medRunTime = opts.medRunTime as boolean + if ( + this.taskStatisticsRequirements.medRunTime && + opts.medRunTime === false + ) { + this.taskStatisticsRequirements.avgRunTime = true + this.taskStatisticsRequirements.medRunTime = opts.medRunTime as boolean } - if (this.taskStatistics.avgWaitTime && opts.medWaitTime === true) { - this.taskStatistics.avgWaitTime = false - this.taskStatistics.medWaitTime = opts.medWaitTime as boolean + if ( + this.taskStatisticsRequirements.avgWaitTime && + opts.medWaitTime === true + ) { + this.taskStatisticsRequirements.avgWaitTime = false + this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean } - if (this.taskStatistics.medWaitTime && opts.medWaitTime === false) { - this.taskStatistics.avgWaitTime = true - this.taskStatistics.medWaitTime = opts.medWaitTime as boolean + if ( + this.taskStatisticsRequirements.medWaitTime && + opts.medWaitTime === false + ) { + this.taskStatisticsRequirements.avgWaitTime = true + this.taskStatisticsRequirements.medWaitTime = opts.medWaitTime as boolean } } @@ -102,30 +114,30 @@ export abstract class AbstractWorkerChoiceStrategy< /** * Gets the worker task runtime. - * If the required statistics are `avgRunTime`, the average runtime is returned. - * If the required statistics are `medRunTime`, the median runtime is returned. + * If the task statistics require `avgRunTime`, the average runtime is returned. + * If the task statistics require `medRunTime`, the median runtime is returned. * * @param workerNodeKey - The worker node key. * @returns The worker task runtime. */ protected getWorkerTaskRunTime (workerNodeKey: number): number { - return this.taskStatistics.medRunTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medRunTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgRunTime + return this.taskStatisticsRequirements.medRunTime + ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median + : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average } /** * Gets the worker task wait time. - * If the required statistics are `avgWaitTime`, the average wait time is returned. - * If the required statistics are `medWaitTime`, the median wait time is returned. + * If the task statistics require `avgWaitTime`, the average wait time is returned. + * If the task statistics require `medWaitTime`, the median wait time is returned. * * @param workerNodeKey - The worker node key. * @returns The worker task wait time. */ protected getWorkerWaitTime (workerNodeKey: number): number { - return this.taskStatistics.medWaitTime - ? this.pool.workerNodes[workerNodeKey].tasksUsage.medWaitTime - : this.pool.workerNodes[workerNodeKey].tasksUsage.avgWaitTime + return this.taskStatisticsRequirements.medWaitTime + ? this.pool.workerNodes[workerNodeKey].workerUsage.runTime.median + : this.pool.workerNodes[workerNodeKey].workerUsage.runTime.average } protected computeDefaultWorkerWeight (): number { @@ -150,7 +162,7 @@ export abstract class AbstractWorkerChoiceStrategy< */ private findFirstFreeWorkerNodeKey (): number { return this.pool.workerNodes.findIndex(workerNode => { - return workerNode.tasksUsage.running === 0 + return workerNode.workerUsage.tasks.executing === 0 }) } @@ -166,14 +178,16 @@ export abstract class AbstractWorkerChoiceStrategy< private findLastFreeWorkerNodeKey (): number { // It requires node >= 18.0.0: // return this.workerNodes.findLastIndex(workerNode => { - // return workerNode.tasksUsage.running === 0 + // return workerNode.workerUsage.tasks.executing === 0 // }) for ( let workerNodeKey = this.pool.workerNodes.length - 1; workerNodeKey >= 0; workerNodeKey-- ) { - if (this.pool.workerNodes[workerNodeKey].tasksUsage.running === 0) { + if ( + this.pool.workerNodes[workerNodeKey].workerUsage.tasks.executing === 0 + ) { return workerNodeKey } }