- this.workerChoiceStrategyType
- ) as IWorkerChoiceStrategy<Worker, Data, Response>
- if (
- workerChoiceStrategy.isDynamicPool &&
- !workerChoiceStrategy.pool.full &&
- workerChoiceStrategy.pool.findFreeWorkerKey() === -1
- ) {
- return this.createWorkerCallback()
+ 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) {
+ throw new Error(
+ `Worker node key chosen is null or undefined after ${retriesCount} retries`
+ )