Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 6s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Failing after 18m17s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Failing after 18m41s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been cancelled
Backend mutation-hardening cluster from the 2026-07-11 mutation-safety audit sweep (adversarial-reviewer #22/#23), the parallel-safe backend-isolated slice. audit#22 F4 — standardize post-commit enqueues on CancellationToken.None: 20 command handlers under MediaCollections/, ProgramSchedules/, Playouts/, Channels/ threaded the request cancellationToken into work that runs AFTER SaveChangesAsync commits (WriteAsync rebuild/refresh enqueues, mediator.Publish, reindex, cache Refresh, and post-commit lookups that gate an enqueue). A late client-disconnect then turns an already-durable commit into a thrown request AND drops the side effect. Generalizes the #251 deco-handler fix. Excludes BuildPlayoutHandler (worker/background token, not a client-disconnect token), the config/FFmpeg multi-upsert handlers (partial-commit case, separate follow-up), and response-projection reloads (correctly keep the request token). audit#22 F2 — DeleteChannelHandler/DeletePlayoutHandler now delete the channel guide {number}.xml through IFileSystem.File.Delete (observable under MockFileSystem) and BEFORE the commit (a post-commit delete orphans the xml on a crash; the xml is regenerable on demand, so pre-commit delete is the safe order). audit#23 F4 — ReplacePlayoutAlternateScheduleItemsHandler rejects an empty item list in the handler (not only the controller pre-guard) so a direct caller can't trip the Max()-on-empty crash. Docs: api-conventions.md §7a (post-commit token convention + boundaries), decisions.md entry (rationale, sweep scope, #253 PR2-4 coordination note). Tests: guide-cache-delete-through-FS for both delete handlers, empty-list guard. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
118 lines
4.8 KiB
C#
118 lines
4.8 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.Core.Search;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using ErsatzTV.Infrastructure.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ErsatzTV.Application.MediaCollections;
|
|
|
|
public class
|
|
UpdateSmartCollectionHandler : IRequestHandler<UpdateSmartCollection,
|
|
Either<BaseError, UpdateSmartCollectionResult>>
|
|
{
|
|
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly IMediaCollectionRepository _mediaCollectionRepository;
|
|
private readonly ISearchTargets _searchTargets;
|
|
private readonly ISmartCollectionCache _smartCollectionCache;
|
|
|
|
public UpdateSmartCollectionHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
IMediaCollectionRepository mediaCollectionRepository,
|
|
ChannelWriter<IBackgroundServiceRequest> channel,
|
|
ISearchTargets searchTargets,
|
|
ISmartCollectionCache smartCollectionCache)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_mediaCollectionRepository = mediaCollectionRepository;
|
|
_channel = channel;
|
|
_searchTargets = searchTargets;
|
|
_smartCollectionCache = smartCollectionCache;
|
|
}
|
|
|
|
public async Task<Either<BaseError, UpdateSmartCollectionResult>> Handle(
|
|
UpdateSmartCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Option<SmartCollection> maybeSmartCollection = await SmartCollectionMustExist(
|
|
dbContext,
|
|
request,
|
|
cancellationToken);
|
|
return await maybeSmartCollection.Match(
|
|
Some: async smartCollection =>
|
|
{
|
|
Validation<BaseError, SmartCollection> validation = await Validate(dbContext, request, smartCollection);
|
|
return await validation.Apply(c => ApplyUpdateRequest(dbContext, c, request, cancellationToken));
|
|
},
|
|
None: () => Task.FromResult<Either<BaseError, UpdateSmartCollectionResult>>(
|
|
new NotFoundError($"SmartCollection {request.Id} does not exist.")));
|
|
}
|
|
|
|
private async Task<UpdateSmartCollectionResult> ApplyUpdateRequest(
|
|
TvContext dbContext,
|
|
SmartCollection c,
|
|
UpdateSmartCollection request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
c.Query = request.Query;
|
|
c.Name = request.Name;
|
|
|
|
// rebuild playouts
|
|
if (await dbContext.SaveChangesAsync(cancellationToken) > 0)
|
|
{
|
|
_searchTargets.SearchTargetsChanged();
|
|
|
|
// post-commit side effects run on CancellationToken.None so a late request cancellation
|
|
// can't abort them after the commit landed (#254)
|
|
await _smartCollectionCache.Refresh(CancellationToken.None);
|
|
|
|
// refresh all playouts that use this smart collection
|
|
foreach (int playoutId in await _mediaCollectionRepository.PlayoutIdsUsingSmartCollection(request.Id))
|
|
{
|
|
await _channel.WriteAsync(new BuildPlayout(playoutId, PlayoutBuildMode.Refresh), CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
return new UpdateSmartCollectionResult(c.Id);
|
|
}
|
|
|
|
private static Task<Validation<BaseError, SmartCollection>> Validate(
|
|
TvContext dbContext,
|
|
UpdateSmartCollection request,
|
|
SmartCollection smartCollection) => ValidateName(dbContext, request)
|
|
.MapT(_ => smartCollection);
|
|
|
|
private static Task<Option<SmartCollection>> SmartCollectionMustExist(
|
|
TvContext dbContext,
|
|
UpdateSmartCollection updateCollection,
|
|
CancellationToken cancellationToken) =>
|
|
dbContext.SmartCollections
|
|
.SelectOneAsync(c => c.Id, c => c.Id == updateCollection.Id, cancellationToken)
|
|
.Map(identity);
|
|
|
|
private static async Task<Validation<BaseError, string>> ValidateName(
|
|
TvContext dbContext,
|
|
UpdateSmartCollection updateCollection)
|
|
{
|
|
Validation<BaseError, string> result1 = updateCollection.NotEmpty(c => c.Name)
|
|
.Bind(_ => updateCollection.NotLongerThan(50)(c => c.Name));
|
|
|
|
bool duplicateName = await dbContext.SmartCollections
|
|
.AnyAsync(c => c.Id != updateCollection.Id && c.Name == updateCollection.Name);
|
|
|
|
Validation<BaseError, Unit> result2 = duplicateName
|
|
? Fail<BaseError, Unit>("SmartCollection name must be unique")
|
|
: Success<BaseError, Unit>(Unit.Default);
|
|
|
|
return (result1, result2).Apply((_, _) => updateCollection.Name);
|
|
}
|
|
}
|