Skip to content
58 changes: 48 additions & 10 deletions Scripts/Modules/API-Helper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,47 @@ function Invoke-AlpacaApiRequest {
[string] $Url,
[string] $Method = 'Get',
[Hashtable] $Headers,
[object] $Body
[object] $Body,
[int] $Retries = 0,
[System.Net.HttpStatusCode[]] $NoRetryStatusCodes = @()
)

Write-AlpacaDebug -Message "Invoking Alpaca-Api: $Url ($Method)"

try {
Invoke-RestMethod -Uri $Url -Method $Method -Headers $Headers -Body $Body -AllowInsecureRedirect
}
catch {
Resolve-AlpacaApiError -ErrorRecord $_
$NoRetryStatusCodes +=
[System.Net.HttpStatusCode]::BadRequest, # 400
[System.Net.HttpStatusCode]::Unauthorized, # 401
[System.Net.HttpStatusCode]::Forbidden, # 403
[System.Net.HttpStatusCode]::MethodNotAllowed, # 405
[System.Net.HttpStatusCode]::Conflict, # 409
[System.Net.HttpStatusCode]::UnprocessableEntity, # 422
[System.Net.HttpStatusCode]::NotImplemented # 501

$maxAttempts = $Retries + 1
foreach ($attempt in 1..$maxAttempts) {
Write-AlpacaDebug -Message "Invoking Alpaca-Api: $Url ($Method) - Attempt $attempt of $maxAttempts"
try {
return Invoke-RestMethod -Uri $Url -Method $Method -Headers $Headers -Body $Body -AllowInsecureRedirect
}
catch {
if ($attempt -lt $maxAttempts) {
if ($_.Exception -is [System.Net.Http.HttpRequestException] -and $_.Exception.StatusCode -in $NoRetryStatusCodes) {
Write-AlpacaDebug -Message "Not retrying for Http status code $([int]$_.Exception.StatusCode)"
}
else {
Comment thread
OleWunschmann marked this conversation as resolved.
Resolved
Write-AlpacaDebug -Message (Get-AlpacaApiErrorMessage -ErrorRecord $_)
$waitSeconds = [Math]::Pow(2, $attempt - 1)
Write-AlpacaDebug -Message "Retrying in $waitSeconds second(s)..."
Start-Sleep -Seconds $waitSeconds
continue
}
}

Resolve-AlpacaApiError -ErrorRecord $_
}
}
}
Export-ModuleMember -Function Invoke-AlpacaApiRequest

function Resolve-AlpacaApiError {
function Get-AlpacaApiErrorMessage {
Param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.ErrorRecord] $ErrorRecord
Expand Down Expand Up @@ -105,7 +131,7 @@ function Resolve-AlpacaApiError {
}

$errorMessage = "Alpaca-API request failed"

$errorContext = @($problemDetails.status, $problemDetails.title, $problemDetails.instance) | Where-Object { $_ }
if ($errorContext) {
$errorMessage += " ($($errorContext -join " "))"
Expand All @@ -115,6 +141,18 @@ function Resolve-AlpacaApiError {
$errorMessage += "`n$($problemDetails.detail)"
}

return $errorMessage
}
Export-ModuleMember -Function Get-AlpacaApiErrorMessage

function Resolve-AlpacaApiError {
Param(
[Parameter(Mandatory = $true)]
[System.Management.Automation.ErrorRecord] $ErrorRecord
)

$errorMessage = Get-AlpacaApiErrorMessage -ErrorRecord $ErrorRecord

$updatedErrorRecord = [System.Management.Automation.ErrorRecord]::new($ErrorRecord, $ErrorRecord.Exception)
$updatedErrorRecord.ErrorDetails = [System.Management.Automation.ErrorDetails]::new($errorMessage)
throw $updatedErrorRecord
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Get-AlpacaAppInfo.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function Get-AlpacaAppInfo {

$apiUrl = Get-AlpacaEndpointUrlWithParam -Controller "Container" -Endpoint "Exec" -Ressource $ContainerName -RouteSuffix "appInfo"
Write-AlpacaOutput "Connecting to $apiUrl"
$result = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'Get' -Headers $headers
$result = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'Get' -Headers $headers -Retries 3
return $result
}
}
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Get-AlpacaConfigSyncStatus.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function Get-AlpacaConfigSyncStatus {
$headers = Get-AlpacaAuthenticationHeaders -Token $Token
$apiUrl = Get-AlpacaEndpointUrlWithParam -Controller "GitHub" -Endpoint "ConfigSync"
try {
return Invoke-AlpacaApiRequest -Url $apiUrl -Method Get -Headers $headers
return Invoke-AlpacaApiRequest -Url $apiUrl -Method Get -Headers $headers -Retries 3
}
catch {
Write-AlpacaError "Failed to get config sync status from Alpaca backend: $_"
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Get-AlpacaDependencyApps.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function Get-AlpacaDependencyApps {
$resource = "$($owner)/$($repository)/$($project -replace '^\.$', '_')"
$apiUrl = Get-AlpacaEndpointUrlWithParam -Controller "GitHub" -Endpoint "Project" -Ressource $resource -QueryParams $queryParams

$containerConfig = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'GET' -Headers $headers
$containerConfig = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'GET' -Headers $headers -Retries 3
$artifacts = $containerConfig.containerConfigurations[0].artifacts
$artifacts = $artifacts | Where-Object { $_.target -eq 'App' }
$artifacts = $artifacts | Where-Object { $_.IgnoreIn -ne 'Build' }
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/New-AlpacaContainer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function New-AlpacaContainer {
}
}
$body = $request | ConvertTo-Json -Depth 10
$response = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'POST' -Headers $headers -Body $body
$response = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'POST' -Headers $headers -Body $body -Retries 3

$container = [pscustomobject]@{
Project = $Project
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Remove-AlpacaContainer.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Remove-AlpacaContainer {

$apiUrl = Get-AlpacaEndpointUrlWithParam -Controller "Container" -Endpoint "Container" -Ressource $Container.Id

Invoke-AlpacaApiRequest -Url $apiUrl -Method 'DELETE' -Headers $headers | Out-Null
Invoke-AlpacaApiRequest -Url $apiUrl -Method 'DELETE' -Headers $headers -Retries 3 -NoRetryStatusCodes @([System.Net.HttpStatusCode]::NotFound) | Out-Null

Comment thread
OleWunschmann marked this conversation as resolved.
Resolved
Write-AlpacaOutput "Container '$($Container.Id)' deleted"
} catch {
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Sync-AlpacaConfigs.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function Sync-AlpacaConfigs {
$apiUrl = Get-AlpacaEndpointUrlWithParam -Controller "GitHub" -Endpoint "ConfigSync"

$body = @{ secrets = $Secrets; variables = $Variables } | ConvertTo-Json -Depth 10
Invoke-AlpacaApiRequest -Url $apiUrl -Method Post -Headers $headers -Body $body | Out-Null
Invoke-AlpacaApiRequest -Url $apiUrl -Method Post -Headers $headers -Body $body -Retries 3 | Out-Null

Write-AlpacaOutput "Synced configs to Alpaca backend"
}
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Modules/Wait-AlpacaContainerImageReady.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function Wait-AlpacaContainerImageReady {
$attemps = 1
do {
try {
$containerResult = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'GET' -Headers $headers
$containerResult = Invoke-AlpacaApiRequest -Url $apiUrl -Method 'GET' -Headers $headers -Retries 3
}
catch {
Write-AlpacaError "Error while getting container status: $_"
Expand Down
11 changes: 3 additions & 8 deletions Scripts/Overrides/RunAlPipeline/PublishBcContainerApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ else {


$publishedAppInfos = Get-Variable -Name alpacaPublishedAppInfos -ValueOnly -Scope Script -ErrorAction Ignore
if (! $publishedAppInfos) {
try {
$publishedAppInfos = Get-AlpacaAppInfo -Token $env:_token -ContainerName $env:ALPACA_CONTAINER_ID
}
catch {
Write-AlpacaOutput "Error occurred while getting published app infos: $_"
$publishedAppInfos = @()
}
if ($null -eq $publishedAppInfos) {
$publishedAppInfos = Get-AlpacaAppInfo -Token $env:_token -ContainerName $env:ALPACA_CONTAINER_ID
Set-Variable -Name alpacaPublishedAppInfos -Value $publishedAppInfos -Scope Script
}

$compilerFolder = (GetCompilerFolder)
Expand Down
Loading