Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 6s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 16s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 13m16s
Build ErsatzTV Image / decisions.md append-only (pull_request) Failing after 12m23s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Failing after 14m6s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 14m44s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 19m19s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 19m28s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
GET /api/v1/health re-ran all 14 health checks on every request, 4 of which shell out to ffmpeg/ffprobe via CliWrap — so each poll spawned ~4 subprocesses. The existing HealthCheckSummary cache was write-only. Cache the full result list for 30s inside HealthCheckService keyed on a new "healthcheck.results" entry; a non-forced call returns it on a hit, skipping the checks and the (subscriber-less) summary publish. Add a `bool forceRefresh` first parameter to IHealthCheckService.PerformHealthChecks: the API poll path reads the cache, while startup (RunHealthChecksService) and the troubleshooting support bundle force a fresh run. Refresh surface: GET /api/v1/health gains an optional `[FromQuery] bool refresh` (additive, follows the ?deep= exemplar); the SPA "Refresh health" button calls /api/v1/health?refresh=true, the initial/poll load does not. Tests: HealthCheckService cache-hit vs force-bypass (mutually opposing, non-vacuous), handler+controller refresh-flag threading, SPA refresh URL. Docs: decisions.md 2026-07-19 (#431), api-conventions §2; regenerated v1.json. fixes #431 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
111 lines
4.4 KiB
C#
111 lines
4.4 KiB
C#
using ErsatzTV.Core.Health;
|
|
using ErsatzTV.Core.Health.Checks;
|
|
using MediatR;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace ErsatzTV.Infrastructure.Health;
|
|
|
|
public class HealthCheckService : IHealthCheckService
|
|
{
|
|
private const string SummaryCacheKey = "healthcheck.summary";
|
|
private const string ResultsCacheKey = "healthcheck.results";
|
|
|
|
// Health checks shell out to ffmpeg/ffprobe (4 of the 14 checks) on every run, so a bare
|
|
// GET /api/v1/health spawns ~4 subprocesses per request. Cache the full result list for a
|
|
// short window so repeated polls (a status widget, an MCP client, monitoring) reuse it; an
|
|
// explicit refresh (forceRefresh) bypasses and repopulates. See docs/decisions.md 2026-07-19 (#431).
|
|
private static readonly TimeSpan CacheTtl = TimeSpan.FromSeconds(30);
|
|
|
|
private readonly List<IHealthCheck> _checks; // ReSharper disable SuggestBaseTypeForParameterInConstructor
|
|
private readonly IMemoryCache _memoryCache;
|
|
private readonly IMediator _mediator;
|
|
private readonly ILogger<HealthCheckService> _logger;
|
|
|
|
public HealthCheckService(
|
|
IMacOsConfigFolderHealthCheck macOsConfigFolderHealthCheck,
|
|
IFFmpegVersionHealthCheck ffmpegVersionHealthCheck,
|
|
IFFmpegCapabilitiesHealthCheck ffmpegCapabilitiesHealthCheck,
|
|
IFFmpegReportsHealthCheck ffmpegReportsHealthCheck,
|
|
IHardwareAccelerationHealthCheck hardwareAccelerationHealthCheck,
|
|
IMovieMetadataHealthCheck movieMetadataHealthCheck,
|
|
IEpisodeMetadataHealthCheck episodeMetadataHealthCheck,
|
|
IZeroDurationHealthCheck zeroDurationHealthCheck,
|
|
IFileNotFoundHealthCheck fileNotFoundHealthCheck,
|
|
IUnavailableHealthCheck unavailableHealthCheck,
|
|
IVaapiDriverHealthCheck vaapiDriverHealthCheck,
|
|
IUnifiedDockerHealthCheck unifiedDockerHealthCheck,
|
|
IDowngradeHealthCheck downgradeHealthCheck,
|
|
IEmptyScheduleHealthCheck emptyScheduleHealthCheck,
|
|
IMemoryCache memoryCache,
|
|
IMediator mediator,
|
|
ILogger<HealthCheckService> logger)
|
|
{
|
|
_memoryCache = memoryCache;
|
|
_mediator = mediator;
|
|
_logger = logger;
|
|
_checks =
|
|
[
|
|
downgradeHealthCheck,
|
|
macOsConfigFolderHealthCheck,
|
|
unifiedDockerHealthCheck,
|
|
ffmpegVersionHealthCheck,
|
|
ffmpegCapabilitiesHealthCheck,
|
|
ffmpegReportsHealthCheck,
|
|
hardwareAccelerationHealthCheck,
|
|
movieMetadataHealthCheck,
|
|
episodeMetadataHealthCheck,
|
|
zeroDurationHealthCheck,
|
|
fileNotFoundHealthCheck,
|
|
unavailableHealthCheck,
|
|
emptyScheduleHealthCheck,
|
|
vaapiDriverHealthCheck
|
|
];
|
|
}
|
|
|
|
public async Task<List<HealthCheckResult>> PerformHealthChecks(bool forceRefresh, CancellationToken cancellationToken)
|
|
{
|
|
if (!forceRefresh && _memoryCache.TryGetValue(ResultsCacheKey, out List<HealthCheckResult> cached) && cached is not null)
|
|
{
|
|
return cached;
|
|
}
|
|
|
|
List<HealthCheckResult> result = await _checks.Map(c =>
|
|
{
|
|
var failedResult = new HealthCheckResult(
|
|
c.Title,
|
|
HealthCheckStatus.Fail,
|
|
"Health check failure; see logs",
|
|
"Health check failure",
|
|
None);
|
|
return TryAsync(() => c.Check(cancellationToken)).IfFail(ex => LogAndReturn(ex, failedResult));
|
|
})
|
|
.SequenceParallel()
|
|
.Map(results => results.ToList());
|
|
|
|
var summary = new HealthCheckSummary(
|
|
result.Count(x => x.Status is HealthCheckStatus.Warning),
|
|
result.Count(x => x.Status is HealthCheckStatus.Fail));
|
|
|
|
_memoryCache.Set(ResultsCacheKey, result, CacheTtl);
|
|
_memoryCache.Set(SummaryCacheKey, summary);
|
|
|
|
await _mediator.Publish(summary, cancellationToken);
|
|
|
|
return result;
|
|
}
|
|
|
|
public HealthCheckSummary GetHealthCheckSummary() =>
|
|
_memoryCache.Get<HealthCheckSummary>(SummaryCacheKey) ?? new HealthCheckSummary(0, 0);
|
|
|
|
private HealthCheckResult LogAndReturn(Exception ex, HealthCheckResult failedResult)
|
|
{
|
|
if (ex is not OperationCanceledException)
|
|
{
|
|
_logger.LogWarning(ex, "Failed to run health check {Title}", failedResult.Title);
|
|
}
|
|
|
|
return failedResult;
|
|
}
|
|
}
|