From 8930972a0a1a4cca693104a87d191ca47c7cd22c Mon Sep 17 00:00:00 2001 From: Timothy Date: Tue, 21 Jul 2026 12:16:57 +0200 Subject: [PATCH] fix(525): address final-review findings (migration host-crash guard, multi-error 400s) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Medium-1: wrap ExternalLogoMigratorService.ExecuteAsync in try/catch — a DB exception (e.g. a channel deleted mid-migration -> DbUpdateConcurrencyException) no longer trips BackgroundServiceExceptionBehavior.StopHost and kills the app; it logs and self-heals on the next boot. Caller-cancel path handled separately. - Low-2: CreateChannelHandler/UpdateChannelHandler validation failure now returns errors.Join() (all accumulated errors) not errors.Head (first only), restoring the repo-wide convention; regression test added. - Low-4: corrected the Startup registration comment (migrator self-awaits WaitForDatabase; order is not load-bearing). Final whole-branch review: MERGEABLE @ 6d5f6b24 (fable). Carried Minors adjudicated acceptable-defer. --- .../Channels/Commands/CreateChannelHandler.cs | 2 +- .../Channels/Commands/UpdateChannelHandler.cs | 2 +- .../Channels/UpdateChannelHandlerTests.cs | 18 +++++++++++++ .../RunOnce/ExternalLogoMigratorService.cs | 26 +++++++++++++++---- ErsatzTV/Startup.cs | 3 ++- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/ErsatzTV.Application/Channels/Commands/CreateChannelHandler.cs b/ErsatzTV.Application/Channels/Commands/CreateChannelHandler.cs index 3375cfec1..8342508ac 100644 --- a/ErsatzTV.Application/Channels/Commands/CreateChannelHandler.cs +++ b/ErsatzTV.Application/Channels/Commands/CreateChannelHandler.cs @@ -39,7 +39,7 @@ public class CreateChannelHandler( }, Left: e => Task.FromResult(Left(e))); }, - Fail: errors => Task.FromResult(Left(errors.Head))); + Fail: errors => Task.FromResult(Left(errors.Join()))); } // Resolve the incoming logo path into a value safe to persist. An external http(s) URL is diff --git a/ErsatzTV.Application/Channels/Commands/UpdateChannelHandler.cs b/ErsatzTV.Application/Channels/Commands/UpdateChannelHandler.cs index 296a3e574..248376d00 100644 --- a/ErsatzTV.Application/Channels/Commands/UpdateChannelHandler.cs +++ b/ErsatzTV.Application/Channels/Commands/UpdateChannelHandler.cs @@ -50,7 +50,7 @@ public class UpdateChannelHandler( await ApplyUpdateRequest(dbContext, c, request, logoPath, cancellationToken)), Left: e => Task.FromResult(Left(e))); }, - Fail: errors => Task.FromResult(Left(errors.Head))); + Fail: errors => Task.FromResult(Left(errors.Join()))); }, None: () => Task.FromResult( Left( diff --git a/ErsatzTV.Tests/Application/Channels/UpdateChannelHandlerTests.cs b/ErsatzTV.Tests/Application/Channels/UpdateChannelHandlerTests.cs index 0339557cd..9b8633280 100644 --- a/ErsatzTV.Tests/Application/Channels/UpdateChannelHandlerTests.cs +++ b/ErsatzTV.Tests/Application/Channels/UpdateChannelHandlerTests.cs @@ -117,6 +117,24 @@ public class UpdateChannelHandlerTests : ChannelHandlerTestBase error.Value.ShouldContain("FFmpegProfile"); } + // the applicative validation accumulates every failure; the 400 body must carry all of them, + // not just the first (regression guard for the #525 handler refactor — errors.Join, not .Head). + [Test] + public async Task Should_Report_All_Validation_Errors_Not_Just_The_First() + { + await SeedFFmpegProfile(); + await SeedChannel(1, "5"); + + Either result = + await MakeHandler().Handle( + MakeUpdate(1, number: "5", group: "", ffmpegProfileId: 999), + CancellationToken.None); + + BaseError error = LeftOf(result); + error.Value.ShouldContain("group"); + error.Value.ShouldContain("FFmpegProfile"); + } + [Test] public async Task Should_Reject_Mirror_Transition_When_Channel_Has_Playout() { diff --git a/ErsatzTV/Services/RunOnce/ExternalLogoMigratorService.cs b/ErsatzTV/Services/RunOnce/ExternalLogoMigratorService.cs index 14b0fa2ce..1074de631 100644 --- a/ErsatzTV/Services/RunOnce/ExternalLogoMigratorService.cs +++ b/ErsatzTV/Services/RunOnce/ExternalLogoMigratorService.cs @@ -33,13 +33,29 @@ public class ExternalLogoMigratorService( logger.LogInformation("Migrating external URL channel logos to the image cache"); - using IServiceScope scope = serviceScopeFactory.CreateScope(); - await using TvContext dbContext = scope.ServiceProvider.GetRequiredService(); - IRemoteLogoCacher cacher = scope.ServiceProvider.GetRequiredService(); + try + { + using IServiceScope scope = serviceScopeFactory.CreateScope(); + await using TvContext dbContext = scope.ServiceProvider.GetRequiredService(); + IRemoteLogoCacher cacher = scope.ServiceProvider.GetRequiredService(); - await MigrateAsync(dbContext, cacher, logger, stoppingToken); + await MigrateAsync(dbContext, cacher, logger, stoppingToken); - logger.LogInformation("Done migrating external URL channel logos to the image cache"); + logger.LogInformation("Done migrating external URL channel logos to the image cache"); + } + catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) + { + // shutdown mid-migration — the single trailing SaveChangesAsync never ran, so no partial + // persist; the next boot retries idempotently. + } + catch (Exception ex) + { + // this is a run-once BackgroundService: an escaping exception trips the default + // StopHost behavior and kills the app. A logo migration must never do that — the fetch + // races (e.g. a channel deleted mid-run -> DbUpdateConcurrencyException) are transient + // and self-heal on the next boot. Log and let the host keep serving. + logger.LogError(ex, "Failed migrating external URL channel logos to the image cache; will retry next start"); + } } internal static async Task MigrateAsync( diff --git a/ErsatzTV/Startup.cs b/ErsatzTV/Startup.cs index 35040b7c9..730e6ab68 100644 --- a/ErsatzTV/Startup.cs +++ b/ErsatzTV/Startup.cs @@ -1178,7 +1178,8 @@ public class Startup services.AddHostedService(); services.AddHostedService(); // One-time migration of existing external-URL channel logos into the image cache (ersatztv#525). - // Runs after the schema (migrator) and cleanup exist. + // It awaits SystemStartup.WaitForDatabase itself, so the schema is guaranteed; registration + // order relative to the other hosted services is not load-bearing (it touches only logo artwork). services.AddHostedService(); services.AddHostedService(); services.AddHostedService();