function Wait-AlkamiServiceFabricUpgrades { <# .SYNOPSIS Waits until the specified package deployments are complete in Service Fabric. Throws an error if any of the packages failed to deploy and were rolled back. #> [CmdletBinding()] param( [Parameter(Mandatory=$true)] [object[]]$packages, [Parameter(Mandatory=$true)] [string]$environmentName, [Parameter(Mandatory=$false)] [int]$timeoutMinutes = 30, [Parameter(Mandatory=$false)] [int]$sleepIntervalSeconds = 15 ) $packages = $packages | Where-Object { ![string]::IsNullOrWhiteSpace($_.Version) }; if(($null -eq $packages) -or ($packages.Count -eq 0)) { Write-Host "$loglead : No packages were specified. Returning."; return; } $loglead = (Get-LogLeadName); Write-Host "$loglead : Waiting for upgrades of $($packages.Count) packages to complete."; $completedStates = @("RollingForwardCompleted", "RollingBackCompleted", "Failed"); $successState = $completedStates[0]; # Keep looping until all of the requested deployments are finished, or the deployment lasts longer than the timeout period. $stopwatch = [system.diagnostics.stopwatch]::StartNew(); while($true) { # Grab all of the upgrade status objects for each of the packages from Service Fabric. $packageUpgradeStates = @(); foreach($package in $packages) { # Look for the upgrade of the service. Write-Verbose "$loglead : Getting upgrade status for package $($package.Name)"; if($package.IsReliableService) { $applicationName = Format-AlkamiServiceFabricApplicationName -name $package.ApplicationTypeName -version $package.Version; } else { $applicationName = Format-AlkamiServiceFabricApplicationName -name $package.Name -version $package.Version -environmentName $environmentName; } $applicationName = "fabric:/$applicationName"; $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $applicationName; $packageUpgradeStates += $upgradeStatus; } # If there are still deployments running sleep, and the loop goes on $incompleteDeployments = $packageUpgradeStates | Where-Object { $completedStates -notcontains $_.UpgradeState }; if($stopwatch.Elapsed.TotalMinutes -gt $timeoutMinutes) { Write-Error "$loglead : Service Fabric Upgrades have exceeded timeout window of $timeoutMinutes minutes. Exiting."; break; } elseif($null -ne $incompleteDeployments) { Write-Host "$loglead : Service Fabric Upgrades are still in progress for $($incompleteDeployments.Count) packages. Waiting $($sleepIntervalSeconds)s."; Start-Sleep -Seconds $sleepIntervalSeconds; continue; } # If we've made it here the deployments are complete. # Verify that all of the deployments finished successfully, and are the correct versions. $failure = $false; foreach($state in $packageUpgradeStates) { if($state.UpgradeState -ne $successState) { Write-Error "$loglead : Upgrade of package $($state.ApplicationTypeName) was unsuccessful. Status: $($state.UpgradeState)" -ErrorAction Continue; $failure = $true; } } if($failure) { Write-Error "$loglead : One or more Service Fabric package upgrades were not successful. Investigate!"; } # All of the deployments are complete. Break the loop. break; } Write-Host "$loglead : Package upgrades are complete."; }