diff --git a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs index 5e52685ef..0ffc63e05 100644 --- a/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs +++ b/ErsatzTV.Application/ProgramSchedules/Commands/ReplaceProgramScheduleItemsHandler.cs @@ -64,7 +64,7 @@ public class ReplaceProgramScheduleItemsHandler( ProgramScheduleItem built = BuildItem(programSchedule, i, orderedItems[i]); if (existing.GetType() == built.GetType()) { - // same TPH subtype: copy all scalar values in place (BuildItem is the single source of + // same TPT subtype: copy all scalar values in place (BuildItem is the single source of // item construction, so no field is silently dropped) and rebuild the join rows, keeping // the item's id — and with it the fill-group index that would otherwise cascade away. built.Id = existing.Id; @@ -73,7 +73,7 @@ public class ReplaceProgramScheduleItemsHandler( } else { - // EF can't change a TPH row's type in place; this slot must be replaced (its fill-group + // EF can't change a TPT row's type in place; this slot must be replaced (its fill-group // index resets, which is acceptable — the item fundamentally changed). dbContext.Remove(existing); programSchedule.Items.Add(built); diff --git a/ErsatzTV.Application/Scheduling/Commands/ReplaceDecoTemplateItemsHandler.cs b/ErsatzTV.Application/Scheduling/Commands/ReplaceDecoTemplateItemsHandler.cs index 1188be323..52fccc29d 100644 --- a/ErsatzTV.Application/Scheduling/Commands/ReplaceDecoTemplateItemsHandler.cs +++ b/ErsatzTV.Application/Scheduling/Commands/ReplaceDecoTemplateItemsHandler.cs @@ -41,14 +41,15 @@ public class ReplaceDecoTemplateItemsHandler( // Deco/break/default-filler content is only (re)applied during a Reset build (a Continue keeps the // frozen DecoDefault filler items), and BlockKey change-detection has no deco dimension, so nothing // self-heals a deco-template edit — the editor returned 200 but built filler stayed stale until a - // manual Reset (#251). Enqueue a Reset for every playout that references this deco template. - // Post-commit enqueue uses CancellationToken.None so a late cancellation can't drop the rebuild after - // the data has already changed (audit #22 policy). + // manual Reset (#251). Enqueue a Reset for every playout that references this deco template. This + // whole post-commit invalidation runs with CancellationToken.None (audit #22 policy): once the edit + // is committed, a late request cancellation must not be able to abort the affected-playout query OR + // the enqueue and leave content stale. List playoutIds = await dbContext.PlayoutTemplates .Where(pt => pt.DecoTemplateId == decoTemplate.Id) .Select(pt => pt.PlayoutId) .Distinct() - .ToListAsync(cancellationToken); + .ToListAsync(CancellationToken.None); foreach (int playoutId in playoutIds) { diff --git a/ErsatzTV.Application/Scheduling/Commands/UpdateDecoHandler.cs b/ErsatzTV.Application/Scheduling/Commands/UpdateDecoHandler.cs index 584d5420a..95fe7a55c 100644 --- a/ErsatzTV.Application/Scheduling/Commands/UpdateDecoHandler.cs +++ b/ErsatzTV.Application/Scheduling/Commands/UpdateDecoHandler.cs @@ -185,23 +185,25 @@ public class UpdateDecoHandler( // Deco content (break/default filler/etc.) is only (re)applied during a Reset build, and BlockKey // change-detection has no deco dimension, so editing a deco left already-built filler/break content // stale until a manual Reset (#251). Enqueue a Reset for every playout that references this deco — - // directly (Playout.DecoId) or through a deco template that includes it. Post-commit enqueue uses - // CancellationToken.None so a late cancellation can't drop the rebuild (audit #22 policy). + // directly (Playout.DecoId) or through a deco template that includes it. This whole post-commit + // invalidation runs with CancellationToken.None (audit #22 policy): once the edit is committed, a + // late request cancellation must not be able to abort either the affected-playout queries OR the + // enqueue and leave content stale — the entire side effect has to complete. List directPlayoutIds = await dbContext.Playouts .Where(p => p.DecoId == request.DecoId) .Select(p => p.Id) - .ToListAsync(cancellationToken); + .ToListAsync(CancellationToken.None); List decoTemplateIds = await dbContext.DecoTemplateItems .Where(i => i.DecoId == request.DecoId) .Select(i => i.DecoTemplateId) .Distinct() - .ToListAsync(cancellationToken); + .ToListAsync(CancellationToken.None); List templatePlayoutIds = await dbContext.PlayoutTemplates .Where(pt => pt.DecoTemplateId.HasValue && decoTemplateIds.Contains(pt.DecoTemplateId.Value)) .Select(pt => pt.PlayoutId) - .ToListAsync(cancellationToken); + .ToListAsync(CancellationToken.None); foreach (int playoutId in directPlayoutIds.Concat(templatePlayoutIds).Distinct()) { diff --git a/ErsatzTV.Application/Scheduling/Commands/UpdateDefaultDecoHandler.cs b/ErsatzTV.Application/Scheduling/Commands/UpdateDefaultDecoHandler.cs index c57e30b14..de31cfd49 100644 --- a/ErsatzTV.Application/Scheduling/Commands/UpdateDefaultDecoHandler.cs +++ b/ErsatzTV.Application/Scheduling/Commands/UpdateDefaultDecoHandler.cs @@ -18,17 +18,22 @@ public class UpdateDefaultDecoHandler( { await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); - await dbContext.Playouts + int updated = await dbContext.Playouts .Where(p => p.Id == request.PlayoutId) .ExecuteUpdateAsync(u => u.SetProperty(p => p.DecoId, p => request.DecoId), cancellationToken); // Changing a playout's default deco only takes effect on a Reset build (deco content is applied // during Reset, and BlockKey has no deco dimension to self-heal) — same stale-until-Reset class - // as #251. Enqueue a Reset for the affected playout. CancellationToken.None so the rebuild isn't - // dropped after the assignment has already been persisted. - await channel.WriteAsync( - new BuildPlayout(request.PlayoutId, PlayoutBuildMode.Reset), - CancellationToken.None); + // as #251. Enqueue a Reset for the affected playout, but only if a row was actually updated: a + // request for a nonexistent playout must not queue a build for an id that isn't there (keeps the + // enqueued set equal to the affected set). CancellationToken.None so the rebuild isn't dropped + // after the assignment has already been persisted. + if (updated > 0) + { + await channel.WriteAsync( + new BuildPlayout(request.PlayoutId, PlayoutBuildMode.Reset), + CancellationToken.None); + } return Option.None; } diff --git a/ErsatzTV.Tests/Application/Scheduling/DecoInvalidationTests.cs b/ErsatzTV.Tests/Application/Scheduling/DecoInvalidationTests.cs index 36823540c..034fbac85 100644 --- a/ErsatzTV.Tests/Application/Scheduling/DecoInvalidationTests.cs +++ b/ErsatzTV.Tests/Application/Scheduling/DecoInvalidationTests.cs @@ -144,6 +144,17 @@ public class DecoInvalidationTests reset.ShouldAllBe(bp => bp.Mode == PlayoutBuildMode.Reset); } + [Test] + public async Task UpdateDefaultDeco_Should_Not_Enqueue_For_A_Nonexistent_Playout() + { + // no playout seeded — ExecuteUpdateAsync matches 0 rows + var handler = new UpdateDefaultDecoHandler(_db.Factory, _channel.Writer); + Option result = await handler.Handle(new UpdateDefaultDeco(PlayoutId: 999, DecoId: 1), CancellationToken.None); + + result.IsNone.ShouldBeTrue(); + DrainResetEnqueues().ShouldBeEmpty(); // enqueued set must equal the affected (empty) set + } + private static PlayoutTemplate MakePlayoutTemplate(int playoutId, int? decoTemplateId) => new() {