feat(agent): add health check endpoint and Docker HEALTHCHECK

Migrate from Microsoft.NET.Sdk.Worker to Microsoft.NET.Sdk.Web to
enable HTTP health checks. Add /healthz endpoint via
AgentHealthCheck (reports healthy when registered with Central).
Configure Docker HEALTHCHECK with 30s interval and 15s start period.
This commit is contained in:
Arnaud Dartois committed 2026-07-18 14:09:32 +00:00
1 parent eafbab5e7a
commit 58f46fe3dd
5 files changed
+66 -8

No files matched your search

+4
View File
@@ -21,7 +21,11 @@ WORKDIR /app
RUN apk add --no-cache icu-libs libstdc++ ffmpeg
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
ENV ASPNETCORE_URLS=http://+:8080
COPY --from=build /app .
HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/healthz || exit 1
ENTRYPOINT ["dotnet", "Heimdall.Agent.dll"]
+6
View File
@@ -115,6 +115,12 @@ services:
HEIMDALL_CENTRAL_URL: ${HEIMDALL_CENTRAL_URL:-http://heimdall-central:8080}
HEIMDALL_SITE_TOKEN: ${HEIMDALL_SITE_TOKEN}
HEIMDALL_AGENT_NAME: ${HEIMDALL_AGENT_NAME:-agent-01}
healthcheck:
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/healthz"]
interval: 30s
timeout: 5s
retries: 3
start_period: 15s
depends_on:
- heimdall-central
restart: unless-stopped
+2 -5
View File
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
@@ -7,10 +7,7 @@
<UserSecretsId>dotnet-Heimdall.Agent-53531cc7-00d3-425d-b14f-1de1a7441ae3</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.8" />
</ItemGroup>
<!-- Microsoft.Extensions.Hosting and Microsoft.Extensions.Http are provided implicitly by Microsoft.NET.Sdk.Web -->
<ItemGroup>
<ProjectReference Include="..\Heimdall.Application\Heimdall.Application.csproj" />
+24 -3
View File
@@ -1,7 +1,10 @@
using Heimdall.Agent.Services;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.Diagnostics.HealthChecks;
var builder = Host.CreateApplicationBuilder(args);
var builder = WebApplication.CreateSlimBuilder(args);
// HTTP clients
builder.Services.AddHttpClient("Central", client =>
{
client.Timeout = TimeSpan.FromSeconds(10);
@@ -12,8 +15,26 @@ builder.Services.AddHttpClient("IpResolver", client =>
client.Timeout = TimeSpan.FromSeconds(5);
});
// Agent services
builder.Services.AddSingleton<AgentState>();
builder.Services.AddHostedService<AgentRegistrationService>();
var host = builder.Build();
host.Run();
// Health checks — used by Docker HEALTHCHECK
builder.Services.AddHealthChecks()
.AddCheck<AgentHealthCheck>("agent_health", tags: ["ready"]);
var app = builder.Build();
// Health endpoint (port 5001 is mapped in Dockerfile)
app.MapHealthChecks("/healthz", new HealthCheckOptions
{
Predicate = _ => true,
ResultStatusCodes =
{
[HealthStatus.Healthy] = StatusCodes.Status200OK,
[HealthStatus.Degraded] = StatusCodes.Status200OK,
[HealthStatus.Unhealthy] = StatusCodes.Status503ServiceUnavailable,
}
});
await app.RunAsync();
@@ -0,0 +1,30 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
namespace Heimdall.Agent.Services;
/// <summary>
/// Reports agent health based on registration state.
/// Healthy = registered with Central and able to communicate.
/// </summary>
public sealed class AgentHealthCheck(AgentState state) : IHealthCheck
{
public Task<HealthCheckResult> CheckHealthAsync(
HealthCheckContext context,
CancellationToken cancellationToken = default)
{
if (state.AgentId == Guid.Empty)
{
return Task.FromResult(HealthCheckResult.Unhealthy(
"Agent has not registered with Central yet"));
}
if (string.IsNullOrEmpty(state.ApiKey))
{
return Task.FromResult(HealthCheckResult.Degraded(
"Agent registered but has no API key configured"));
}
return Task.FromResult(HealthCheckResult.Healthy(
"Agent is registered and operational"));
}
}