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>
112 lines
4.7 KiB
C#
112 lines
4.7 KiB
C#
using System.Threading.Channels;
|
|
using ErsatzTV.Application.Playouts;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Core.Interfaces.Repositories;
|
|
using ErsatzTV.Core.Interfaces.Search;
|
|
using ErsatzTV.Core.Scheduling;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class UpdateCollectionHandler : IRequestHandler<UpdateCollection, Either<BaseError, Unit>>
|
|
{
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly IMediaCollectionRepository _mediaCollectionRepository;
|
|
private readonly ISearchTargets _searchTargets;
|
|
|
|
public UpdateCollectionHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
IMediaCollectionRepository mediaCollectionRepository,
|
|
ChannelWriter<IBackgroundServiceRequest> channel,
|
|
ISearchTargets searchTargets)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_mediaCollectionRepository = mediaCollectionRepository;
|
|
_channel = channel;
|
|
_searchTargets = searchTargets;
|
|
}
|
|
|
|
public async Task<Either<BaseError, Unit>> Handle(
|
|
UpdateCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Option<Collection> maybeCollection = await CollectionMustExist(dbContext, request, cancellationToken);
|
|
return await maybeCollection.Match(
|
|
Some: async collection =>
|
|
{
|
|
Validation<BaseError, Collection> validation = await Validate(dbContext, request, collection);
|
|
return await validation.Apply(c => ApplyUpdateRequest(dbContext, c, request, cancellationToken));
|
|
},
|
|
None: () => Task.FromResult<Either<BaseError, Unit>>(
|
|
new NotFoundError($"Collection {request.CollectionId} does not exist.")));
|
|
}
|
|
|
|
private async Task<Unit> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
Collection c,
|
|
UpdateCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
c.Name = request.Name;
|
|
foreach (bool useCustomPlaybackOrder in request.UseCustomPlaybackOrder)
|
|
{
|
|
c.UseCustomPlaybackOrder = useCustomPlaybackOrder;
|
|
}
|
|
|
|
// Force-write past a concurrent Version bump from the custom-order editor (this name/flag writer
|
|
// doesn't participate in If-Match, so the active token must not 500 a benign race) — #253/#269.
|
|
if (await dbContext.SaveChangesForcingVersion(cancellationToken) > 0 && request.UseCustomPlaybackOrder.IsSome)
|
|
{
|
|
// post-commit side effect runs on CancellationToken.None so a late request cancellation
|
|
// can't abort it after the commit landed (#254)
|
|
// refresh all playouts that use this collection
|
|
foreach (int playoutId in await _mediaCollectionRepository.PlayoutIdsUsingCollection(
|
|
request.CollectionId))
|
|
{
|
|
await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh), CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
_searchTargets.SearchTargetsChanged();
|
|
|
|
return Unit.Default;
|
|
}
|
|
|
|
private static async Task<Validation<BaseError, Collection>> Validate(
|
|
TvContext dbContext,
|
|
UpdateCollection request,
|
|
Collection collection) =>
|
|
(await ValidateName(dbContext, request)).Map(_ => collection);
|
|
|
|
private static Task<Option<Collection>> CollectionMustExist(
|
|
TvContext dbContext,
|
|
UpdateCollection updateCollection,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.Collections
|
|
.SelectOneAsync(c => c.Id, c => c.Id == updateCollection.CollectionId, cancellationToken)
|
|
.Map(identity);
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName(
|
|
TvContext dbContext,
|
|
UpdateCollection updateCollection)
|
|
{
|
|
Validation<BaseError, string> result1 = updateCollection.NotEmpty(c => c.Name)
|
|
.Bind(_ => updateCollection.NotLongerThan(50)(c => c.Name));
|
|
|
|
bool duplicateName = await dbContext.Collections
|
|
.AnyAsync(c => c.Id != updateCollection.CollectionId && c.Name == updateCollection.Name);
|
|
|
|
Validation<BaseError, Unit> result2 = duplicateName
|
|
? Fail<BaseError, Unit>("Collection name must be unique")
|
|
: Success<BaseError, Unit>(Unit.Default);
|
|
|
|
return (result1, result2).Apply((_, _) => updateCollection.Name);
|
|
}
|
|
}
|