Service Fabric Start-ServiceFabricApplicationUpgrade - 如何让 powershell 在继续之前等待升级成功

Posted

技术标签:

【中文标题】Service Fabric Start-ServiceFabricApplicationUpgrade - 如何让 powershell 在继续之前等待升级成功【英文标题】:Service Fabric Start-ServiceFabricApplicationUpgrade - How to make powershell wait for successful upgrade before continuing 【发布时间】:2021-07-02 08:59:25 【问题描述】:

我正在放弃使用 Azure devops 管道“升级 Service Fabric 应用程序”任务,并将其替换为纯 PowerShell 方法。

Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback

问题是当我使用 devops 管道任务时,它会等待升级完成并在控制台窗口中显示输出。当我使用我的自定义 powershell 时,它会立即执行并移至下一个管道任务,即使在我的集群中我可以看到升级刚刚开始。如何让它显示输出并等待成功,然后再继续?

【问题讨论】:

【参考方案1】:

此命令Get-Service Fabric Application Upgrade 将获取 Service Fabric 应用程序升级的状态,因此我们可以在以下 PowerShell 脚本中使用它来获取此升级的结果。

## Start monitored application upgrade
try

    Write-Host "Start upgrading application..." 
    Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback

catch

    Write-Host ("Error starting upgrade. " + $_)

    Write-Host "Unregister application..."
    Unregister-ServiceFabricApplicationType -ApplicationTypeName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -Force
    throw


do

    Write-Host "Waiting for upgrade..."
    Start-Sleep -Seconds 3
    $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName)
 while ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
    
if($upgradeStatus.UpgradeState -eq "RollingForwardCompleted")

    Write-Host "Upgrade completed successfully."

elseif($upgradeStatus.UpgradeState -eq "RollingBackCompleted")

    Write-Error "Upgrade was Rolled back."

elseif($upgradeStatus.UpgradeState -eq "Failed")

    Write-Error "Upgrade Failed."

因此,此 PowerShell 任务将等待此升级的结果,然后再移动到下一个管道任务。

【讨论】:

【参考方案2】:

试试下面的方法,它会等待脚本块完成并运行下一行。

$job = Start-Job -ScriptBlock
    Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/$(ServiceFabricApplicationName) -ApplicationTypeVersion $BuildNumber -ApplicationParameter $settings -Monitored -FailureAction Rollback
 
Wait-Job $job.Name

【讨论】:

以上是关于Service Fabric Start-ServiceFabricApplicationUpgrade - 如何让 powershell 在继续之前等待升级成功的主要内容,如果未能解决你的问题,请参考以下文章

Service Fabric 反向代理 - 404 - FABRIC_E_SERVICE_DOES_NOT_EXIST

Azure Service Fabric 与 Azure Service Fabric Mesh

aZURE Service Fabric

从 Powershell 部署 Service Fabric 应用程序,无需 Service Fabric SDK

微服务架构春天 微软Service Fabric开源

Service Fabric Actors:使用最新版本的 Service Fabric nuget 包部署 Actors 时出现的问题