Move retry logic to the AzureDevOpsApi class

pull/3/head
MaksimZhukov 5 years ago
parent 93258f5088
commit b0414ba627

@ -3,14 +3,25 @@ class AzureDevOpsApi
[string] $BaseUrl [string] $BaseUrl
[string] $RepoOwner [string] $RepoOwner
[object] $AuthHeader [object] $AuthHeader
[UInt32] $RetryCount = 0
[UInt32] $RetryIntervalSec = 30
AzureDevOpsApi( AzureDevOpsApi(
[string] $TeamFoundationCollectionUri, [string] $TeamFoundationCollectionUri,
[string] $ProjectName, [string] $ProjectName,
[string] $AccessToken [string] $AccessToken,
[UInt32] $RetryCount,
[UInt32] $RetryIntervalSec
) { ) {
$this.BaseUrl = $this.BuildBaseUrl($TeamFoundationCollectionUri, $ProjectName) $this.BaseUrl = $this.BuildBaseUrl($TeamFoundationCollectionUri, $ProjectName)
$this.AuthHeader = $this.BuildAuth($AccessToken) $this.AuthHeader = $this.BuildAuth($AccessToken)
if ($RetryCount -gt 0) {
$this.RetryCount = $RetryCount
}
if ($RetryIntervalSec -gt 0) {
$this.RetryIntervalSec = $RetryIntervalSec
}
} }
[object] hidden BuildAuth([string]$AccessToken) { [object] hidden BuildAuth([string]$AccessToken) {
@ -73,17 +84,21 @@ class AzureDevOpsApi
$params.Body = $Body $params.Body = $Body
} }
$params.RetryIntervalSec = $this.RetryIntervalSec
$params.MaximumRetryCount = $this.RetryCount
return Invoke-RestMethod @params return Invoke-RestMethod @params
} }
} }
function Get-AzureDevOpsApi { function Get-AzureDevOpsApi {
param ( param (
[string] $TeamFoundationCollectionUri, [string] $TeamFoundationCollectionUri,
[string] $ProjectName, [string] $ProjectName,
[string] $AccessToken [string] $AccessToken,
[UInt32] $RetryCount,
[UInt32] $RetryIntervalSec
) )
return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken) return [AzureDevOpsApi]::New($TeamFoundationCollectionUri, $ProjectName, $AccessToken, $RetryCount, $RetryIntervalSec)
} }

@ -32,6 +32,7 @@ function Get-ToolVersions {
$versionsList = $releases.version $versionsList = $releases.version
} }
Write-Host "Versions to build: $versionsList"
return $versionsList return $versionsList
} }
@ -41,42 +42,17 @@ function Queue-Builds {
[Parameter(Mandatory)] [string[]] $ToolVersions, [Parameter(Mandatory)] [string[]] $ToolVersions,
[Parameter(Mandatory)] [string] $SourceBranch, [Parameter(Mandatory)] [string] $SourceBranch,
[Parameter(Mandatory)] [string] $SourceVersion, [Parameter(Mandatory)] [string] $SourceVersion,
[Parameter(Mandatory)] [UInt32] $DefinitionId, [Parameter(Mandatory)] [UInt32] $DefinitionId
[Parameter(Mandatory)] [UInt32] $RetryIntervalSec,
[Parameter(Mandatory)] [UInt32] $Retries
) )
[BuildInfo[]]$queuedBuilds = @() [BuildInfo[]]$queuedBuilds = @()
$ToolVersions | ForEach-Object { $ToolVersions | ForEach-Object {
$version = $_.Trim() $version = $_.Trim()
Write-Host "Queue build for $version..."
while ($Retries -gt 0) $queuedBuild = $AzureDevOpsApi.QueueBuild($version, $SourceBranch, $SourceVersion, $DefinitionId)
{ $buildInfo = Get-BuildInfo -AzureDevOpsApi $AzureDevOpsApi -Build $queuedBuild
try Write-Host "Queued build: $($buildInfo.Link)"
{
Write-Host "Queue build for $version..."
$queuedBuild = $AzureDevOpsApi.QueueBuild($version, $SourceBranch, $SourceVersion, $DefinitionId)
$buildInfo = Get-BuildInfo -AzureDevOpsApi $AzureDevOpsApi -Build $queuedBuild
Write-Host "Queued build: $($buildInfo.Link)"
break
}
catch
{
Write-Host "There is an error during build starting:`n $_"
$Retries--
if ($Retries -eq 0)
{
Write-Host "Build can't be queued, please try later"
exit 1
}
Write-Host "Waiting 30 seconds before retrying. Retries left: $Retries"
Start-Sleep -Seconds $RetryIntervalSec
}
}
$queuedBuilds += $buildInfo $queuedBuilds += $buildInfo
} }
@ -128,7 +104,9 @@ function Make-BuildsOutput {
$azureDevOpsApi = Get-AzureDevOpsApi -TeamFoundationCollectionUri $TeamFoundationCollectionUri ` $azureDevOpsApi = Get-AzureDevOpsApi -TeamFoundationCollectionUri $TeamFoundationCollectionUri `
-ProjectName $AzureDevOpsProjectName ` -ProjectName $AzureDevOpsProjectName `
-AccessToken $AzureDevOpsAccessToken -AccessToken $AzureDevOpsAccessToken `
-RetryCount $RetryCount `
-RetryIntervalSec $RetryIntervalSec
$toolVersionsList = Get-ToolVersions -ManifestLink $ManifestLink ` $toolVersionsList = Get-ToolVersions -ManifestLink $ManifestLink `
-RetryIntervalSec $RetryIntervalSec ` -RetryIntervalSec $RetryIntervalSec `
@ -139,9 +117,7 @@ $queuedBuilds = Queue-Builds -AzureDevOpsApi $azureDevOpsApi `
-ToolVersions $toolVersionsList ` -ToolVersions $toolVersionsList `
-SourceBranch $SourceBranch ` -SourceBranch $SourceBranch `
-SourceVersion $SourceVersion ` -SourceVersion $SourceVersion `
-DefinitionId $DefinitionId ` -DefinitionId $DefinitionId
-RetryIntervalSec $RetryIntervalSec `
-Retries $RetryCount
if ($WaitForBuilds) { if ($WaitForBuilds) {
Write-Host "`nWaiting results of builds ..." Write-Host "`nWaiting results of builds ..."

Loading…
Cancel
Save