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();