private bool HandleWUOperationStates()

in src/PatchOrchestrationApplication/NodeAgentNTService/src/Manager/WindowsUpdateManager.cs [341:489]


        private bool HandleWUOperationStates(CancellationToken cancellationToken)
        {
            TimeSpan utilityTaskTimeOut = TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes);
            NodeAgentSfUtilityExitCodes wuOperationState = this._nodeAgentSfUtility.GetWuOperationState(utilityTaskTimeOut);
            _eventSource.InfoMessage("Current WU Operation State : {0}", wuOperationState);

            bool reschedule = false;
            switch (wuOperationState)
            {                
                case NodeAgentSfUtilityExitCodes.None:
                case NodeAgentSfUtilityExitCodes.OperationCompleted:
                    {
                        this.UpdateLastOperationStartTimeStamp(DateTime.UtcNow);
                        OperationResultCode searchResult = SearchUpdates(cancellationToken);
                        reschedule = (searchResult != OperationResultCode.orcSucceeded ? true : reschedule);

                        if (this._wuCollectionWrapper != null)
                        {
                            if (this._wuCollectionWrapper.Collection.Count == 0)
                            {
                                _eventSource.InfoMessage("No Windows Update available. Completing the operation.");
                                //Complete operation.
                                this._nodeAgentSfUtility.UpdateSearchAndDownloadStatus(
                                    NodeAgentSfUtilityExitCodes.OperationCompleted,
                                    this._operationResultFormatter.CreateSearchAndDownloadDummyResult(this.lastUpdateOperationStartTimeStamp),
                                    utilityTaskTimeOut
                                );

                                break;
                            }
                            string wUStatusUpdate = string.Format("Windows update download started.");
                            this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, wUStatusUpdate, HealthState.Ok, -1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));

                            OperationResultCode downloadResult = DownloadUpdates(cancellationToken);
                            reschedule = (downloadResult != OperationResultCode.orcSucceeded ? true : reschedule);

                            WindowsUpdateOperationResult searchAndDownloadResult = this._operationResultFormatter.FormatSearchAndDownloadResult(downloadResult, this._wuCollectionWrapper, this.lastUpdateOperationStartTimeStamp);
                            _eventSource.InfoMessage("Search and download result: {0}", searchAndDownloadResult);

                            this._nodeAgentSfUtility.UpdateSearchAndDownloadStatus(NodeAgentSfUtilityExitCodes.DownloadCompleted, searchAndDownloadResult, utilityTaskTimeOut);

                            string WUDownloadComplete = string.Format("Windows updates downloaded, waiting for installation approval from Repair Manager.");
                            this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, WUDownloadComplete, HealthState.Ok, -1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));

                            NodeAgentSfUtilityExitCodes exitCodes = this.WaitForInstallationApproval(cancellationToken);
                            if (exitCodes.Equals(NodeAgentSfUtilityExitCodes.Failure))
                            {
                                _eventSource.ErrorMessage("Not able to move from DownloadCompleted state to InstallationApproved state.");
                                reschedule = true;
                                break;
                            }

                            this.UpdateLastOperationStartTimeStamp(DateTime.UtcNow);
                            wUStatusUpdate = string.Format("Windows update installation in progress.");
                            this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, wUStatusUpdate, HealthState.Ok, -1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));

                            this._nodeAgentSfUtility.UpdateInstallationStatus(NodeAgentSfUtilityExitCodes.InstallationInProgress, null, utilityTaskTimeOut);

                            OperationResultCode installResult = InstallUpdates(cancellationToken);
                            reschedule = (installResult != OperationResultCode.orcSucceeded ? true : reschedule);

                            WindowsUpdateOperationResult installationResult = this._operationResultFormatter.FormatInstallationResult(installResult, this._wuCollectionWrapper, this.lastUpdateOperationStartTimeStamp);
                            _eventSource.InfoMessage("Installation result: {0}", installationResult);

                            this._nodeAgentSfUtility.UpdateInstallationStatus(NodeAgentSfUtilityExitCodes.InstallationCompleted, installationResult, utilityTaskTimeOut);
                        }

                        break;
                    }
                case NodeAgentSfUtilityExitCodes.DownloadCompleted:
                case NodeAgentSfUtilityExitCodes.InstallationApproved:
                    {
                        if(wuOperationState == NodeAgentSfUtilityExitCodes.DownloadCompleted)
                        {
                            string WUDownloadComplete = string.Format("Windows updates downloaded, waiting for installation approval from Repair Manager.");
                            this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, WUDownloadComplete, HealthState.Ok,-1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));
                        }

                        NodeAgentSfUtilityExitCodes exitCodes = this.WaitForInstallationApproval(cancellationToken);
                        if (exitCodes.Equals(NodeAgentSfUtilityExitCodes.Failure))
                        {
                            _eventSource.ErrorMessage("Not able to move from DownloadCompleted state to InstallationApproved state.");
                            reschedule = true;
                            break;
                        }
                        this.UpdateLastOperationStartTimeStamp(DateTime.UtcNow);
                        string wUStatusUpdate = string.Format("Windows update installation in progress.");
                        this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, wUStatusUpdate, HealthState.Ok,-1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));

                        OperationResultCode searchResult = SearchUpdates(cancellationToken);
                        reschedule = (searchResult != OperationResultCode.orcSucceeded ? true : reschedule);

                        if (this._wuCollectionWrapper != null)
                        {
                            if (this._wuCollectionWrapper.Collection.Count == 0)
                            {
                                string msg =
                                    "Installation approved but no updates found to install. Completing the operation.";
                                this._nodeAgentSfUtility.ReportHealth(WUOperationStatus, msg, HealthState.Warning, -1, TimeSpan.FromMinutes(this._serviceSettings.OperationTimeOutInMinutes));
                                _eventSource.WarningMessage(msg);
                                //Complete operation.
                                this._nodeAgentSfUtility.UpdateInstallationStatus(
                                    NodeAgentSfUtilityExitCodes.OperationCompleted,
                                    this._operationResultFormatter.CreateInstallationDummyResult(this.lastUpdateOperationStartTimeStamp),
                                    utilityTaskTimeOut
                                );
                                break;
                            }

                            this._nodeAgentSfUtility.UpdateInstallationStatus(NodeAgentSfUtilityExitCodes.InstallationInProgress, null, utilityTaskTimeOut);

                            OperationResultCode installResult = InstallUpdates(cancellationToken);
                            reschedule = (installResult != OperationResultCode.orcSucceeded ? true : reschedule);

                            WindowsUpdateOperationResult installationResult = this._operationResultFormatter.FormatInstallationResult(installResult, this._wuCollectionWrapper, this.lastUpdateOperationStartTimeStamp);
                            _eventSource.InfoMessage("Installation result: {0}", installationResult);

                            this._nodeAgentSfUtility.UpdateInstallationStatus(NodeAgentSfUtilityExitCodes.InstallationCompleted, installationResult, utilityTaskTimeOut);
                        }
                        break;
                    }
                case NodeAgentSfUtilityExitCodes.InstallationInProgress:
                    {                        
                        OperationResultCode searchResult = SearchUpdates(cancellationToken);
                        reschedule = (searchResult != OperationResultCode.orcSucceeded ? true : reschedule);

                        if (this._wuCollectionWrapper != null)
                        {
                            if (this._wuCollectionWrapper.Collection.Count == 0)
                            {
                                //this is possible when installation is completed but NT service is killed before updating "InstallationCompleted" status.
                                break;
                            }

                            OperationResultCode installResult = InstallUpdates(cancellationToken);
                            reschedule = (installResult != OperationResultCode.orcSucceeded ? true : reschedule);

                            WindowsUpdateOperationResult installationResult = this._operationResultFormatter.FormatInstallationResult(installResult, this._wuCollectionWrapper, this.lastUpdateOperationStartTimeStamp);
                            _eventSource.InfoMessage("Installation result: {0}", installationResult);

                            this._nodeAgentSfUtility.UpdateInstallationStatus(NodeAgentSfUtilityExitCodes.InstallationCompleted, installationResult, utilityTaskTimeOut);
                        }
                        break;
                    }
                default:
                    break;
            }
            return reschedule;
        }