如何在AzureDevops托管管道上并行运行Powershell作业

时间:2020-02-18 23:02:41

标签: azure-devops azure-pipelines

-EDIT 03-

我正在编辑我的问题,因为评论太小。

我终于完成了任务。我必须编辑Publish-UpgradedServiceFabricApplication来接受“ compareApplicationName”和“ compareMode”。然后它将使用那些来确定将要升级的服务列表。在完成所有应用程序升级之前,我必须小心不要取消注册ApplicationType。

对不起,但是我的法律部门不允许我发布任何实际代码,声称它是公司的IP。

-EDIT 02-

用户错误 事实证明,Get-VstsInput从线程调用时不返回任何值。因此,所有评估都是错误的,该错误被线程吞没了。 我正在重写,只是将调用线程化为Publish-UpgradedServiceFabricApplication

-原始消息-

我有一个运行多个服务的服务结构集群。其中一些是动态创建的服务,因此在ServiceManifest.xml中未引用它们

我有一个自定义任务,可以按顺序更新动态创建的服务,但这需要几个小时。我正在尝试并行运行Publish-UpgradedServiceFabricApplication,以便可以一次运行所有升级作业。作业开始(我认为),但是任务永远不会等待它们完成才结束(从而结束PS作业)。

我尝试在作业状态上添加一个Do...While循环,但它仍会过早地结束任务。

在升级运行过程中,如何使任务保持活动状态?

-EDIT-

我觉得我需要提供一个示例,因为我的代码无法正常运行。在任务中给出以下代码:

    foreach($serviceapp in $servicefabricapp){
        if(-not ($serviceapp.ApplicationName.OriginalString.StartsWith("fabric:/DynamicService"))) {
            Write-Host "Application '$($serviceapp.ApplicationName.OriginalString)' does not start with 'fabric:/DynamicService' -SKIPPING"
            continue 
        }

        $foundTaxReturnApp = $true
        Write-Host "Starting upgrade for $($serviceapp.ApplicationName.OriginalString)"
        Start-Job -ScriptBlock $upgradeScriptBlock -ArgumentList $serviceapp.ApplicationName, $applicationPackagePath, $applicationParameterFile, $publishProfile, $isUpgrade, $upgradeParameters

        Start-Sleep 5
    }

    $jobCount = (Get-Job | Where-Object{ $_.state -eq 'Running'}).Count

    Do {
        Start-Sleep -Seconds 30
        Write-Host "Waiting for $jobCount jobs to finish..."
        $jobCount = (Get-Job | Where-Object{ $_.state -eq 'Running'}).Count
    } while ($jobCount -gt 0)

    Get-Job

    $jobCount = (Get-Job | Where-Object{ $_.state -eq 'Running'}).Count

    Write-Host "Finished waiting, $jobCount jobs running."

这是管道的输出。在“升级结构:/ DynamicService-2019OA20200131__16_21_39”与“等待0个作业完成...”之间没有时间。而且没有DynamicServices升级。

Application 'fabric:/MyFabricSystem' does not start with 'fabric:/DynamicService' -SKIPPING
Starting upgrade for fabric:/DynamicService
VERBOSE: Exporting function 'New-PSWorkflowSession'.
VERBOSE: Exporting alias 'nwsn'.

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
1      Job1            BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018CA20200128__13_35_06
3      Job3            BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018FA20200130__16_12_16
5      Job5            BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018GA20200130__16_12_16
7      Job7            BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018IA20200131__16_21_39
9      Job9            BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018IB20200131__16_21_39
11     Job11           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018KA20200121__15_08_01
13     Job13           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018OA20200131__16_21_39
15     Job15           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018PA20200131__16_21_39
17     Job17           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018SA20200130__16_12_16
19     Job19           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018XA20200127__16_43_45
21     Job21           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2018YA20200124__15_17_35
23     Job23           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2019GA20200130__16_12_16
25     Job25           BackgroundJob   Running       True            localhost            ...
Starting upgrade for fabric:/DynamicService-2019OA20200131__16_21_39
27     Job27           BackgroundJob   Running       True            localhost            ...
Waiting for 0 jobs to finish...
1      Job1            BackgroundJob   Completed     True            localhost            ...
3      Job3            BackgroundJob   Completed     True            localhost            ...
5      Job5            BackgroundJob   Completed     True            localhost            ...
7      Job7            BackgroundJob   Completed     True            localhost            ...
9      Job9            BackgroundJob   Completed     True            localhost            ...
11     Job11           BackgroundJob   Completed     True            localhost            ...
13     Job13           BackgroundJob   Completed     True            localhost            ...
15     Job15           BackgroundJob   Completed     True            localhost            ...
17     Job17           BackgroundJob   Completed     True            localhost            ...
19     Job19           BackgroundJob   Completed     True            localhost            ...
21     Job21           BackgroundJob   Completed     True            localhost            ...
23     Job23           BackgroundJob   Completed     True            localhost            ...
25     Job25           BackgroundJob   Completed     True            localhost            ...
27     Job27           BackgroundJob   Completed     True            localhost            ...
Finished waiting, 0 jobs running.
##[debug]Leaving D:\a\_tasks\DynamicServiceUpgrade_4ec0e9dc-0f0a-47ff-86e3-ca0ae22d21d9\0.0.31\taskmain.ps1.

1 个答案:

答案 0 :(得分:0)

在这种情况下,脚本可以正确执行。作业的概念是执行并继续前进。如果您想等待所有作业执行完毕后Get-Job | Wait-Job来完成作业。

如果您想执行更高级/更智能的操作,可以将其放入do-while循环中

Do {
 $Job = Get-Job
 if ($Job.Status -eq "Failed") {
  "Fail"
  Exit 1
 }
Sleep 10
} Until ($Job.Status -eq "Success")