Files
ersatztv/ErsatzTV.Application/Scheduling/Commands/UpdateDefaultDecoHandler.cs
T
timothyandClaude Opus 4.8 51ac11e5aa
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 4s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 3m55s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 6m58s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
fix(review): close post-commit cancellation window + guard default-deco enqueue (Codex Medium/Low)
Independent Codex review of the fix diff surfaced two real findings the fork pass missed:

- Medium: the #251 affected-playout QUERIES in ReplaceDecoTemplateItemsHandler and
  UpdateDecoHandler still ran on the request `cancellationToken`, so a cancellation
  landing after SaveChanges committed but before those queries executed would throw
  before the CancellationToken.None enqueue — the edit committed but no playout Reset,
  re-opening the stale-content bug in that window. Run the entire post-commit
  invalidation (queries + enqueue) on CancellationToken.None so the side effect can't
  be half-aborted once the data has changed.
- Low: UpdateDefaultDecoHandler enqueued a Reset for request.PlayoutId even when
  ExecuteUpdateAsync matched 0 rows (nonexistent playout), creating a background build
  request for an id that isn't there. Guard the enqueue on rows-updated > 0 so the
  enqueued set equals the affected set. Added a regression test.

Also corrected the ReplaceProgramScheduleItemsHandler comments: the schedule-item
hierarchy is TPT (table-per-type), not TPH — the SetValues reconcile is safe either way
(same-runtime-type guard; no discriminator to corrupt), Codex confirmed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 15:18:38 +02:00

46 lines
1.9 KiB
C#

using System.Threading.Channels;
using ErsatzTV.Application.Playouts;
using ErsatzTV.Core;
using ErsatzTV.Core.Scheduling;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Scheduling;
public class UpdateDefaultDecoHandler(
IDbContextFactory<TvContext> dbContextFactory,
ChannelWriter<IBackgroundServiceRequest> channel)
: IRequestHandler<UpdateDefaultDeco, Option<BaseError>>
{
public async Task<Option<BaseError>> Handle(UpdateDefaultDeco request, CancellationToken cancellationToken)
{
try
{
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
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, 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<BaseError>.None;
}
catch (Exception ex)
{
return BaseError.New(ex.ToString());
}
}
}