+ public update (workerNodeKey: number): boolean {
+ return (
+ this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ ).update(workerNodeKey)
+ }
+
+ /**
+ * 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 the worker node key is null or undefined.
+ */
+ public execute (): number {
+ const workerNodeKey = (
+ this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ ).choose()
+ if (
+ workerNodeKey == null &&
+ this.choiceRetriesCount < (this.opts.choiceRetries as number)
+ ) {
+ this.choiceRetriesCount++
+ return this.execute()
+ } else if (workerNodeKey == null) {
+ throw new TypeError(
+ `Worker node key chosen is null or undefined after ${this.choiceRetriesCount} retries`
+ )
+ }
+ this.choiceRetriesCount = 0
+ return workerNodeKey
+ }
+
+ /**
+ * Removes the worker node key from the worker choice strategy in the context.
+ *
+ * @param workerNodeKey - The worker node key.
+ * @returns `true` if the removal is successful, `false` otherwise.
+ */
+ public remove (workerNodeKey: number): boolean {
+ return (
+ this.workerChoiceStrategies.get(
+ this.workerChoiceStrategy
+ ) as IWorkerChoiceStrategy
+ ).remove(workerNodeKey)
+ }
+
+ /**
+ * Sets the worker choice strategies in the context options.
+ *
+ * @param opts - The worker choice strategy options.
+ */
+ public setOptions (opts: WorkerChoiceStrategyOptions): void {
+ this.opts = { ...DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS, ...opts }
+ for (const workerChoiceStrategy of this.workerChoiceStrategies.values()) {
+ workerChoiceStrategy.setOptions(opts)
+ }