fix(health): check the active storage provider
Heimdall Gitea / Build and tests (pull_request) Successful in 15m57s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 7m56s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 9m9s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / Build and tests (pull_request) Successful in 15m57s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan server image (pull_request) Successful in 7m56s
Heimdall Gitea / Build and scan agent image (pull_request) Successful in 9m9s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
This commit is contained in:
1 parent
cc6cbeccf5
commit
2d2e5bc211
7 files changed
+73
-51
No files matched your search
@@ -55,7 +55,9 @@ public static class DependencyInjection
|
||||
// Health checks (spec 13.2) — PostgreSQL + dépendances optionnelles.
|
||||
services.AddHealthChecks()
|
||||
.AddDbContextCheck<AppDbContext>("postgres", tags: ["ready", "live"])
|
||||
.AddCheck<Infrastructure.HealthChecks.AlarikHealthCheck>("alarik", tags: ["ready"])
|
||||
.AddCheck<Infrastructure.HealthChecks.StorageHealthCheck>(
|
||||
"storage", failureStatus: Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Degraded,
|
||||
tags: ["ready"], timeout: TimeSpan.FromSeconds(8))
|
||||
.AddCheck<Infrastructure.HealthChecks.AiHealthCheck>("codeproject-ai", tags: ["live"]);
|
||||
|
||||
// Identity
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace Heimdall.Infrastructure.HealthChecks;
|
||||
|
||||
/// <summary>
|
||||
/// Alarik/S3 health check (GET /health on the S3 endpoint). Reports Healthy
|
||||
/// when Alarik is not configured (optional dependency) and Degraded when the
|
||||
/// endpoint is configured but unreachable.
|
||||
/// </summary>
|
||||
public sealed class AlarikHealthCheck : IHealthCheck
|
||||
{
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
public AlarikHealthCheck(IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var endpoint = Environment.GetEnvironmentVariable("ALARIK_ENDPOINT")
|
||||
?? Environment.GetEnvironmentVariable("S3_ENDPOINT");
|
||||
if (string.IsNullOrEmpty(endpoint))
|
||||
return HealthCheckResult.Healthy("Alarik not configured");
|
||||
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient("Alarik");
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(30));
|
||||
|
||||
using var response = await client.GetAsync($"{endpoint.TrimEnd('/')}/health", cts.Token).ConfigureAwait(false);
|
||||
return response.IsSuccessStatusCode
|
||||
? HealthCheckResult.Healthy("Alarik reachable")
|
||||
: HealthCheckResult.Degraded($"Alarik returned {(int)response.StatusCode}");
|
||||
}
|
||||
catch (Exception ex) when (ex is not OperationCanceledException)
|
||||
{
|
||||
return HealthCheckResult.Degraded($"Alarik unreachable: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using Heimdall.Infrastructure.Storage;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
|
||||
namespace Heimdall.Infrastructure.HealthChecks;
|
||||
|
||||
/// <summary>Checks the storage provider currently selected in Heimdall settings.</summary>
|
||||
public sealed class StorageHealthCheck : IHealthCheck
|
||||
{
|
||||
private static readonly TimeSpan ProbeTimeout = TimeSpan.FromSeconds(5);
|
||||
private readonly StorageProviderFactory _storage;
|
||||
|
||||
public StorageHealthCheck(StorageProviderFactory storage)
|
||||
{
|
||||
_storage = storage;
|
||||
}
|
||||
|
||||
public async Task<HealthCheckResult> CheckHealthAsync(
|
||||
HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timeout.CancelAfter(ProbeTimeout);
|
||||
|
||||
try
|
||||
{
|
||||
var isHealthy = await _storage.HealthCheckAsync(timeout.Token).ConfigureAwait(false);
|
||||
return isHealthy
|
||||
? HealthCheckResult.Healthy("Active storage provider is available")
|
||||
: HealthCheckResult.Degraded("Active storage provider is unavailable or misconfigured");
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return HealthCheckResult.Degraded("Active storage health probe timed out");
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Avoid returning configuration, endpoint, or credential details in
|
||||
// the public readiness response.
|
||||
return HealthCheckResult.Degraded("Active storage provider is unavailable or misconfigured");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,12 +194,20 @@ public sealed class AlarikStorageProvider : IStorageProvider, IDisposable
|
||||
return false;
|
||||
try
|
||||
{
|
||||
await _client.ListBucketsAsync(ct).ConfigureAwait(false);
|
||||
await _client.ListObjectsV2Async(new ListObjectsV2Request
|
||||
{
|
||||
BucketName = _bucket,
|
||||
MaxKeys = 1,
|
||||
}, ct).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Alarik/S3 health check failed");
|
||||
_logger.LogWarning(ex, "S3 storage bucket health check failed");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,19 +195,29 @@ public sealed class LocalStorageProvider : IStorageProvider
|
||||
sorted.Length > pageSize ? objects[^1].Key : null));
|
||||
}
|
||||
|
||||
public Task<bool> HealthCheckAsync(CancellationToken ct = default)
|
||||
public async Task<bool> HealthCheckAsync(CancellationToken ct = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
EnsureNoLinks(_root);
|
||||
Directory.CreateDirectory(_root);
|
||||
EnsureNoLinks(_root);
|
||||
return Task.FromResult(true);
|
||||
|
||||
var probePath = Path.Combine(_root, $".heimdall-health-{Guid.NewGuid():N}.tmp");
|
||||
await using var probe = new FileStream(probePath, FileMode.CreateNew, FileAccess.Write,
|
||||
FileShare.None, bufferSize: 1, FileOptions.Asynchronous | FileOptions.DeleteOnClose);
|
||||
await probe.WriteAsync(new byte[] { 0 }.AsMemory(), ct).ConfigureAwait(false);
|
||||
await probe.FlushAsync(ct).ConfigureAwait(false);
|
||||
return true;
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Local storage health check failed");
|
||||
return Task.FromResult(false);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ public class HealthController : ControllerBase
|
||||
public IActionResult Health() => Ok(new { status = "healthy", timestamp = DateTime.UtcNow });
|
||||
|
||||
/// <summary>
|
||||
/// Readiness probe — checks dependencies (PostgreSQL, Alarik S3).
|
||||
/// Readiness probe — checks dependencies (PostgreSQL and active object storage provider).
|
||||
/// 503 when a required dependency is down.
|
||||
/// </summary>
|
||||
[HttpGet("/health/ready")]
|
||||
|
||||
Reference in new issue
Block a user