Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 7s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Failing after 2m33s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 3m30s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Adversarial review caught a HIGH the fan-out introduced: activating Version as an
IsConcurrencyToken on Playout/Collection makes EF append `WHERE Version=@orig` to
EVERY root UPDATE, so a non-If-Match writer that saves via plain SaveChangesAsync
now throws DbUpdateConcurrencyException → 500 when a replace-all editor bumps the
row between its load and save. Realistic two-tab trigger (edit playout settings while
editing its alt-schedules; edit a collection's name while reordering) — a new crash,
previously silent last-write-wins.
Fix: shared ConcurrencyExtensions.SaveChangesForcingVersion — on a concurrency
failure it adopts the stored token as original+current (client-wins merge scoped to
the token, never reverting the concurrent bump) and retries, i.e. Phase-1 force-write
semantics for a missing If-Match. Applied to the exposed UPDATE writers:
UpdatePlayout, Update{Sequential,Scripted,ExternalJson}Playout, UpdateOnDemandCheckpoint,
UpdateCollection. Non-vacuous test proves the write lands and the bump survives.
Deletes + repo-mediated Add* writers (rarer / join-rows-only) re-scoped onto #269.
Refs #253 #269
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
90 lines
3.5 KiB
C#
90 lines
3.5 KiB
C#
using System.CommandLine.Parsing;
|
|
using System.IO.Abstractions;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Channels;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.Playouts;
|
|
|
|
public class
|
|
UpdateScriptedPlayoutHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> workerChannel,
|
|
IFileSystem fileSystem)
|
|
: IRequestHandler<UpdateScriptedPlayout,
|
|
Either<BaseError, PlayoutNameViewModel>>
|
|
{
|
|
public async Task<Either<BaseError, PlayoutNameViewModel>> Handle(
|
|
UpdateScriptedPlayout request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, Playout> validation = await Validate(dbContext, request, cancellationToken);
|
|
return await validation.Apply(playout => ApplyUpdateRequest(dbContext, request, playout, cancellationToken));
|
|
}
|
|
|
|
private async Task<PlayoutNameViewModel> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout request,
|
|
Playout playout,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
playout.ScheduleFile = request.ScheduleFile;
|
|
|
|
// Force-write past a concurrent Version bump from a replace-all editor (schedule-file writer
|
|
// doesn't participate in If-Match, so an active token must not 500 a benign race) — #253/#269.
|
|
if (await dbContext.SaveChangesForcingVersion(cancellationToken) > 0)
|
|
{
|
|
// post-commit side effect runs on CancellationToken.None so a late request cancellation
|
|
// can't abort it after the commit landed (#254)
|
|
await workerChannel.WriteAsync(new RefreshChannelData(playout.Channel.Number), CancellationToken.None);
|
|
}
|
|
|
|
return new PlayoutNameViewModel(
|
|
playout.Id,
|
|
playout.ScheduleKind,
|
|
playout.Channel.Name,
|
|
playout.Channel.Number,
|
|
playout.Channel.PlayoutMode,
|
|
playout.ProgramSchedule?.Name ?? string.Empty,
|
|
playout.ScheduleFile,
|
|
playout.DailyRebuildTime,
|
|
playout.BuildStatus,
|
|
playout.DecoId,
|
|
playout.Deco?.Name,
|
|
playout.Version);
|
|
}
|
|
|
|
private async Task<Validation<BaseError, Playout>> Validate(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout request,
|
|
CancellationToken cancellationToken) =>
|
|
(ValidateScheduleFile(request), await PlayoutMustExist(dbContext, request, cancellationToken))
|
|
.Apply((_, playout) => playout);
|
|
|
|
private Validation<BaseError, string> ValidateScheduleFile(UpdateScriptedPlayout request)
|
|
{
|
|
var args = CommandLineParser.SplitCommandLine(request.ScheduleFile).ToList();
|
|
string scriptFile = args[0];
|
|
if (!fileSystem.File.Exists(scriptFile))
|
|
{
|
|
return BaseError.New("Scripted schedule does not exist!");
|
|
}
|
|
|
|
return request.ScheduleFile;
|
|
}
|
|
|
|
private static Task<Validation<BaseError, Playout>> PlayoutMustExist(
|
|
TvContext dbContext,
|
|
UpdateScriptedPlayout updatePlayout,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Playouts
|
|
.Include(p => p.Channel)
|
|
.SelectOneAsync(p => p.Id, p => p.Id == updatePlayout.PlayoutId, cancellationToken)
|
|
.Map(o => o.ToValidation<BaseError>("Playout does not exist."));
|
|
}
|