Files
Arnaud Dartois d774d28f2a
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and scan server image (pull_request) Canceled after 0s
Heimdall Gitea / Build and scan agent image (pull_request) Canceled after 0s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Canceled after 0s
Heimdall Gitea / Publish versioned release (pull_request) Canceled after 0s
Heimdall Gitea / Build and tests (pull_request) Canceled after 7m33s
Heimdall Gitea / CI policy (pull_request) Failing after 2s
refactor(settings): manage integrations through Central UI
2026-10-26 06:26:00 +01:00

102 lines
4.1 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Infrastructure.Settings;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Heimdall.Infrastructure.HealthChecks;
/// <summary>
/// CodeProject.AI / Ollama health check (AI backend). Reports Healthy when AI
/// is not configured (optional dependency), Degraded when configured but
/// unreachable — drives the /health/live degradationLevel.
/// URLs lues depuis les AppSettings (ai.cpaiUrl / ai.ollamaUrl, ticket #80)
/// avec fallback env.
/// </summary>
public sealed class AiHealthCheck : IHealthCheck
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IServiceProvider _services;
public AiHealthCheck(IHttpClientFactory httpClientFactory, IServiceProvider services)
{
_httpClientFactory = httpClientFactory;
_services = services;
}
public async Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context, CancellationToken cancellationToken = default)
{
var (cpai, ollama) = await ResolveUrlsAsync(cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(cpai))
{
try
{
var client = _httpClientFactory.CreateClient("Ai");
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(30));
using var response = await client.GetAsync($"{cpai.TrimEnd('/')}/v1/status/ping", cts.Token).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return HealthCheckResult.Healthy("CodeProject.AI reachable");
return HealthCheckResult.Degraded($"CodeProject.AI returned {(int)response.StatusCode}");
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (OperationCanceledException)
{
return HealthCheckResult.Degraded("CodeProject.AI health probe timed out");
}
catch (Exception ex)
{
return HealthCheckResult.Degraded($"CodeProject.AI unreachable: {ex.Message}");
}
}
if (!string.IsNullOrEmpty(ollama))
{
try
{
var client = _httpClientFactory.CreateClient("Ai");
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(TimeSpan.FromSeconds(60));
using var response = await client.GetAsync($"{ollama.TrimEnd('/')}/api/tags", cts.Token).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
return HealthCheckResult.Healthy("Ollama reachable");
return HealthCheckResult.Degraded($"Ollama returned {(int)response.StatusCode}");
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
throw;
}
catch (OperationCanceledException)
{
return HealthCheckResult.Degraded("Ollama health probe timed out");
}
catch (Exception ex)
{
return HealthCheckResult.Degraded($"Ollama unreachable: {ex.Message}");
}
}
return HealthCheckResult.Healthy("AI backend not configured");
}
/// <summary>Les URL de service IA sont lues dans les AppSettings globaux.</summary>
private async Task<(string? Cpai, string? Ollama)> ResolveUrlsAsync(CancellationToken ct)
{
using var scope = _services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<IApplicationDbContext>();
var cpai = await AppSettingReader.GetAsync(db, "ai.cpaiUrl", ct).ConfigureAwait(false)
?? Environment.GetEnvironmentVariable("CPAI_URL");
var ollama = await AppSettingReader.GetAsync(db, "ai.ollamaUrl", ct).ConfigureAwait(false);
return (cpai, ollama);
}
}