fix(525): address final-review findings (migration host-crash guard, multi-error 400s)

- 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.
This commit is contained in:
2026-07-21 13:18:48 +02:00
parent 56a18cd5dc
commit 8930972a0a
5 changed files with 43 additions and 8 deletions
@@ -39,7 +39,7 @@ public class CreateChannelHandler(
},
Left: e => Task.FromResult(Left<BaseError, CreateChannelResult>(e)));
},
Fail: errors => Task.FromResult(Left<BaseError, CreateChannelResult>(errors.Head)));
Fail: errors => Task.FromResult(Left<BaseError, CreateChannelResult>(errors.Join())));
}
// Resolve the incoming logo path into a value safe to persist. An external http(s) URL is
@@ -50,7 +50,7 @@ public class UpdateChannelHandler(
await ApplyUpdateRequest(dbContext, c, request, logoPath, cancellationToken)),
Left: e => Task.FromResult(Left<BaseError, ChannelViewModel>(e)));
},
Fail: errors => Task.FromResult(Left<BaseError, ChannelViewModel>(errors.Head)));
Fail: errors => Task.FromResult(Left<BaseError, ChannelViewModel>(errors.Join())));
},
None: () => Task.FromResult(
Left<BaseError, ChannelViewModel>(
@@ -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<BaseError, ChannelViewModel> 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()
{
@@ -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<TvContext>();
IRemoteLogoCacher cacher = scope.ServiceProvider.GetRequiredService<IRemoteLogoCacher>();
try
{
using IServiceScope scope = serviceScopeFactory.CreateScope();
await using TvContext dbContext = scope.ServiceProvider.GetRequiredService<TvContext>();
IRemoteLogoCacher cacher = scope.ServiceProvider.GetRequiredService<IRemoteLogoCacher>();
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(
+2 -1
View File
@@ -1178,7 +1178,8 @@ public class Startup
services.AddHostedService<LocalAdminSeedService>();
services.AddHostedService<DatabaseCleanerService>();
// 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<ExternalLogoMigratorService>();
services.AddHostedService<LoadLoggingLevelService>();
services.AddHostedService<CacheCleanerService>();