internal async Task PrepareRepairTasks()

in src/PatchOrchestrationApplication/CoordinatorService/src/RepairManagerHelper.cs [559:638]


        internal async Task PrepareRepairTasks(CancellationToken cancellationToken)
        {
            NodeList nodeList = await this.fabricClient.QueryManager.GetNodeListAsync(null, null, this.DefaultTimeoutForOperation, cancellationToken);
            IList<RepairTask> claimedTaskList = await this.GetClaimedRepairTasks(nodeList, cancellationToken);

            switch (RmPolicy)
            {
                case TaskApprovalPolicy.NodeWise:
                {
                    RepairTaskList processingTaskList = await this.GetRepairTasksUnderProcessing(cancellationToken);
                    if (!processingTaskList.Any())
                    {
                        if (claimedTaskList.Any())
                        {
                                IList<RepairTask> completedTasks = await this.GetCompletedRepairTasks(nodeList, cancellationToken);
                                TimeSpan? timePastAfterCompletedTask = null;
                                if (completedTasks.Any())
                                {
                                    RepairTask lastCompletedTask = completedTasks.Aggregate((curMax, task) => (task.CompletedTimestamp > curMax.CompletedTimestamp ? task : curMax));
                                    timePastAfterCompletedTask = DateTime.UtcNow - lastCompletedTask?.CompletedTimestamp;
                                }


                            if (!timePastAfterCompletedTask.HasValue ||
                                 timePastAfterCompletedTask.Value > MinWaitTimeBetweenNodes)
                            {
                                RepairTask oldestClaimedTask = claimedTaskList.Aggregate(
                                    (curMin, task) => (task.CreatedTimestamp < curMin.CreatedTimestamp ? task : curMin));

                                ServiceEventSource.Current.VerboseMessage(
                                    "Out of {0} claimed tasks, Oldest repair task = {0} with node = {1} will be prepared",
                                        claimedTaskList.Count, oldestClaimedTask.TaskId, oldestClaimedTask.Target);

                                this.StartPreparingRepairTask(oldestClaimedTask);

                            }
                            else
                            {
                                ServiceEventSource.Current.VerboseMessage(
                                        "Waiting for another {0} to pass in order to start the next repair task.", MinWaitTimeBetweenNodes - timePastAfterCompletedTask.Value);

                            }
                        }
                    }
                    break;
                }
                case TaskApprovalPolicy.UpgradeDomainWise:
                {
                    string currentUpgradeDomain = await this.GetCurrentUpgradeDomainUnderProcessing(nodeList, cancellationToken);

                    ServiceEventSource.Current.VerboseMessage(String.Format("{0} repair tasks were found in claimed state", claimedTaskList.Count));
                    // Below line can be enabled for debugging
                    // rmHelper.PrintRepairTasks(claimedTaskList);

                    foreach (var claimedTask in claimedTaskList)
                    {
                        string udName = this.GetUpgradeDomainOfRepairTask(claimedTask, nodeList);

                        if (string.IsNullOrEmpty(currentUpgradeDomain))
                        {
                            currentUpgradeDomain = udName;
                        }

                        if (udName == currentUpgradeDomain)
                        {
                            this.StartPreparingRepairTask(claimedTask);
                        }
                    }
                    break;
                }

                default:
                {
                    string errorMessage = String.Format("Illegal RmPolicy found: {0}", RmPolicy);
                    ServiceEventSource.Current.ErrorMessage(errorMessage);
                    throw new InvalidOperationException(errorMessage);
                }
            }

        }