fix(agent): retry transient release metadata failures
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 25m52s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 8m34s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 14m24s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Build and scan NVIDIA Agent image (pull_request) Successful in 22m38s
Heimdall Gitea / CI policy (pull_request) Successful in 1s

This commit is contained in:
arnaud.dartois committed 2026-11-06 13:57:15 +01:00
1 parent 208732b4d9
commit 4e2ff8c2b6
4 files changed
+84 -3

No files matched your search

+21 -1
View File
@@ -41,7 +41,27 @@ fi
"$DOTNET_BIN" --list-runtimes | grep -q '^Microsoft.AspNetCore.App 10\.' || {
echo 'A compatible ASP.NET Core 10 runtime is required' >&2; exit 1;
}
curl -fsS "$CENTRAL_URL/api/updates/latest" -o "$TMP_DIR/release.json" || { echo "Failed to fetch release" >&2; exit 1; }
RELEASE_METADATA_URL="$CENTRAL_URL/api/updates/latest"
for attempt in 1 2 3; do
CURL_PROTOCOL=https
[[ "$CENTRAL_URL" == https://* ]] || CURL_PROTOCOL=http
HTTP_STATUS=$(curl -sS --proto "$CURL_PROTOCOL" \
--connect-timeout 10 --max-time 30 \
-w '%{http_code}' "$RELEASE_METADATA_URL" -o "$TMP_DIR/release.json") && CURL_STATUS=0 || CURL_STATUS=$?
if [[ "$CURL_STATUS" -eq 0 && "$HTTP_STATUS" =~ ^2[0-9][0-9]$ ]]; then
break
fi
if [[ "$CURL_STATUS" -ne 0 && "$HTTP_STATUS" == 000 ]] || [[ "$HTTP_STATUS" =~ ^(408|425|429|5[0-9][0-9])$ ]]; then
if [[ "$attempt" -lt 3 ]]; then
delay=$((attempt * 2))
echo "Release metadata request failed temporarily (attempt $attempt/3); retrying in ${delay}s." >&2
sleep "$delay"
continue
fi
fi
echo "Failed to fetch release metadata from Central (HTTP ${HTTP_STATUS:-000}; curl exit ${CURL_STATUS})." >&2
exit 1
done
if [[ -z "$UPDATE_PUBLIC_KEY" ]]; then
jq -er '.updatePublicKeyPem | select(type == "string" and length > 0 and length <= 4096)' "$TMP_DIR/release.json" > "$TMP_DIR/update-public-key.pem" || {
echo 'Central release metadata has no valid publisher public key' >&2; exit 1;
+1
View File
@@ -20,6 +20,7 @@ Use this skill when changing `install/install-agent.sh`, `install/install-agent.
- For updates, exercise both successful and rejected signatures before installation. Preserve the previous release until the candidate has reported the expected version and stayed healthy for the configured stability interval.
- Use a temporary install root and fake runtime for local failure/rollback tests. Never point these tests at `/usr/local`, a real systemd service, production credentials, or an installed Agent state directory.
- Report Linux and Windows real-host install/update/rollback as open until each OS has been exercised with a real service and the expected signing configuration.
- The Linux installer retries the release metadata request at most three times for transport failures, HTTP 408/425/429, and HTTP 5xx, with 2s then 4s waits. Other HTTP statuses fail immediately. Acceptance should verify transient-then-success, persistent-5xx bounded failure, permanent-4xx no-retry behavior, and that release signature and archive digest checks remain required.
## Central installer asset delivery
@@ -119,8 +119,9 @@ public class UpdatesController : ControllerBase
{
using var client = _httpClientFactory.CreateClient("Central");
client.Timeout = TimeSpan.FromSeconds(15);
var response = await client.GetAsync(sourceUri, ct);
if (!response.IsSuccessStatusCode) { _logger.LogWarning("Forgejo returned {Code}", response.StatusCode); return null; }
using var response = await GetReleaseResponseWithRetryAsync(client, sourceUri, ct);
if (response is null || !response.IsSuccessStatusCode)
return null;
var release = await response.Content.ReadFromJsonAsync<ForgejoRelease>(Json, ct);
if (release is null) return null;
var assets = new Dictionary<string, PlatformAsset>();
@@ -167,6 +168,39 @@ public class UpdatesController : ControllerBase
catch (Exception ex) { _logger.LogError(ex, "Failed to fetch release"); return null; }
}
private async Task<HttpResponseMessage?> GetReleaseResponseWithRetryAsync(HttpClient client, Uri sourceUri,
CancellationToken ct)
{
for (var attempt = 1; attempt <= 3; attempt++)
{
HttpResponseMessage response;
try
{
response = await client.GetAsync(sourceUri, ct);
}
catch (HttpRequestException ex) when (attempt < 3)
{
_logger.LogWarning(ex, "Forgejo release request failed temporarily on attempt {Attempt}/3", attempt);
await Task.Delay(TimeSpan.FromSeconds(attempt * 2), ct);
continue;
}
if (response.IsSuccessStatusCode || !IsTransientStatusCode(response.StatusCode) || attempt == 3)
return response;
_logger.LogWarning("Forgejo returned transient HTTP {Code} on attempt {Attempt}/3", response.StatusCode, attempt);
response.Dispose();
await Task.Delay(TimeSpan.FromSeconds(attempt * 2), ct);
}
return null;
}
private static bool IsTransientStatusCode(System.Net.HttpStatusCode statusCode) =>
statusCode is System.Net.HttpStatusCode.RequestTimeout or System.Net.HttpStatusCode.TooManyRequests ||
(int)statusCode >= 500;
private static async Task<AgentImageManifest?> FetchAgentImageManifestAsync(List<ForgejoAsset>? assets,
string assetName, HttpClient client, CancellationToken ct)
{
@@ -0,0 +1,26 @@
using System.Net;
using System.Net.Http;
using System.Reflection;
using Heimdall.Api.Controllers;
using Xunit;
namespace Heimdall.Web.Tests.Controllers;
public sealed class UpdatesControllerRetryTests
{
[Theory]
[InlineData(HttpStatusCode.RequestTimeout, true)]
[InlineData(HttpStatusCode.TooManyRequests, true)]
[InlineData(HttpStatusCode.BadGateway, true)]
[InlineData(HttpStatusCode.ServiceUnavailable, true)]
[InlineData(HttpStatusCode.NotFound, false)]
[InlineData(HttpStatusCode.BadRequest, false)]
public void IsTransientStatusCode_classifies_only_retryable_http_responses(HttpStatusCode code, bool expected)
{
var method = typeof(UpdatesController).GetMethod("IsTransientStatusCode",
BindingFlags.NonPublic | BindingFlags.Static);
Assert.NotNull(method);
Assert.Equal(expected, method.Invoke(null, [code]));
}
}