IWorkerChoiceStrategy
>
- /**
- * The number of times the worker choice strategy in the context has been retried.
- */
- private retriesCount = 0
-
/**
* Worker choice strategy context constructor.
*
* Executes the worker choice strategy in the context algorithm.
*
* @returns The key of the worker node.
- * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined .
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
*/
public execute (): number {
- const workerNodeKey = (
- this.workerChoiceStrategies.get(
- this.workerChoiceStrategy
- ) as IWorkerChoiceStrategy
- ).choose()
- if (
+ const workerChoiceStrategy = this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ if (!workerChoiceStrategy.hasPoolWorkerNodesReady()) {
+ // wait for a worker node to be ready without blocking the event loop
+ }
+ return this.executeStrategy(workerChoiceStrategy)
+ }
+
+ /**
+ * Executes the given worker choice strategy.
+ *
+ * @param workerChoiceStrategy - The worker choice strategy.
+ * @returns The key of the worker node.
+ * @throws {@link https://nodejs.org/api/errors.html#class-error} If after configured retries the worker node key is null or undefined.
+ */
+ private executeStrategy (workerChoiceStrategy: IWorkerChoiceStrategy): number {
+ let workerNodeKey: number | undefined
+ let chooseCount = 0
+ let retriesCount = 0
+ do {
+ workerNodeKey = workerChoiceStrategy.choose()
+ if (workerNodeKey == null && chooseCount > 0) {
+ retriesCount++
+ }
+ chooseCount++
+ } while (
workerNodeKey == null &&
- (this.retriesCount < (this.opts.retries as number) ||
- this.opts.retries === Infinity)
- ) {
- this.retriesCount++
- return this.execute()
- } else if (workerNodeKey == null) {
+ retriesCount < (this.opts.retries as number)
+ )
+ if (workerNodeKey == null) {
throw new Error(
- `Worker node key chosen is null or undefined after ${this.retriesCount} retries`
+ `Worker node key chosen is null or undefined after ${retriesCount} retries`
)
}
- this.retriesCount = 0
return workerNodeKey
}