- const workerNodeKey = (
- this.workerChoiceStrategies.get(
- this.workerChoiceStrategy
- ) as IWorkerChoiceStrategy
- ).choose()
- if (
- workerNodeKey == null &&
- this.retriesCount < (this.opts.retries as number)
- ) {
- this.retriesCount++
- return this.execute()
- } else if (workerNodeKey == null) {
+ const workerChoiceStrategy = this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ let workerNodeKey: number | undefined
+ const maxExecutionCount = 10000
+ let executionCount = 0
+ let chooseCount = 0
+ let retriesCount = 0
+ do {
+ if (workerChoiceStrategy.hasPoolWorkerNodesReady()) {
+ workerNodeKey = workerChoiceStrategy.choose()
+ if (chooseCount > 0) {
+ retriesCount++
+ }
+ chooseCount++
+ }
+ executionCount++
+ } while (
+ executionCount < maxExecutionCount &&
+ (!workerChoiceStrategy.hasPoolWorkerNodesReady() ||
+ (workerNodeKey == null && retriesCount < (this.opts.retries as number)))
+ )
+ if (executionCount >= maxExecutionCount) {
+ throw new RangeError(
+ `Worker choice strategy consecutive executions has exceeded the maximum of ${maxExecutionCount}`
+ )
+ }
+ if (workerNodeKey == null) {