using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; var builder = WebApplication.CreateBuilder(args); if (!builder.Environment.IsDevelopment()) throw new InvalidOperationException("The manual readiness control is development-only"); builder.Services.AddSingleton(); builder.Services.AddHealthChecks().AddCheck("classroom-state", tags: new[] { "ready" }); var app = builder.Build(); app.MapHealthChecks("/health/live", new HealthCheckOptions { Predicate = _ => false }); app.MapHealthChecks("/health/ready", new HealthCheckOptions { Predicate = registration => registration.Tags.Contains("ready") }); app.MapPost("/lab/ready", (ReadyState state) => { state.Ready = true; return TypedResults.NoContent(); }); app.Run(); public sealed class ReadyState { private int ready; public bool Ready { get => Volatile.Read(ref ready) == 1; set => Volatile.Write(ref ready, value ? 1 : 0); } } public sealed class ReadyCheck(ReadyState state) : IHealthCheck { public Task CheckHealthAsync(HealthCheckContext context, CancellationToken token = default) { token.ThrowIfCancellationRequested(); return Task.FromResult(state.Ready ? HealthCheckResult.Healthy("Classroom switch is ready") : HealthCheckResult.Unhealthy("Classroom switch is not ready")); } }