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>
128 lines
4.7 KiB
C#
128 lines
4.7 KiB
C#
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Channels;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Errors;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Channel = ErsatzTV.Core.Domain.Channel;
|
|
|
|
namespace ErsatzTV.Application.Channels;
|
|
|
|
public class UpdateChannelNumbersHandler(
|
|
IDbContextFactory<TvContext> dbContextFactory,
|
|
ChannelWriter<IBackgroundServiceRequest> workerChannel)
|
|
: IRequestHandler<UpdateChannelNumbers, Option<BaseError>>
|
|
{
|
|
public async Task<Option<BaseError>> Handle(UpdateChannelNumbers request, CancellationToken cancellationToken)
|
|
{
|
|
Option<BaseError> validationError = ValidateRequest(request);
|
|
if (validationError.IsSome)
|
|
{
|
|
return validationError;
|
|
}
|
|
|
|
await using TvContext dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken);
|
|
|
|
try
|
|
{
|
|
var numberUpdates = request.Channels.ToDictionary(c => c.Id, c => c.Number);
|
|
List<int> channelIds = numberUpdates.Keys.ToList();
|
|
|
|
List<Channel> channelsToUpdate = await dbContext.Channels
|
|
.Where(c => channelIds.Contains(c.Id))
|
|
.ToListAsync(cancellationToken);
|
|
|
|
if (channelsToUpdate.Count != channelIds.Count)
|
|
{
|
|
var found = channelsToUpdate.Select(c => c.Id).ToHashSet();
|
|
int missingId = channelIds.First(id => !found.Contains(id));
|
|
return new NotFoundError($"Channel {missingId} does not exist.");
|
|
}
|
|
|
|
List<string> requestedNumbers = numberUpdates.Values.ToList();
|
|
bool numberConflict = await dbContext.Channels
|
|
.AnyAsync(
|
|
c => requestedNumbers.Contains(c.Number) && !channelIds.Contains(c.Id),
|
|
cancellationToken);
|
|
if (numberConflict)
|
|
{
|
|
return BaseError.New("Channel number must be unique");
|
|
}
|
|
|
|
// give every channel a non-conflicting number
|
|
foreach (var channel in channelsToUpdate)
|
|
{
|
|
channel.Number = $"-{channel.Id}";
|
|
}
|
|
|
|
// save those changes
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
// give every channel the proper new number
|
|
foreach (var channel in channelsToUpdate)
|
|
{
|
|
channel.Number = numberUpdates[channel.Id];
|
|
if (double.TryParse(channel.Number, CultureInfo.InvariantCulture, out double sortNumber))
|
|
{
|
|
channel.SortNumber = sortNumber;
|
|
}
|
|
else
|
|
{
|
|
return BaseError.New($"Failed to parse channel number {channel.Number}");
|
|
}
|
|
}
|
|
|
|
// save those changes
|
|
await dbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
// commit the transaction
|
|
await transaction.CommitAsync(cancellationToken);
|
|
|
|
// update channel list and xmltv
|
|
// 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 RefreshChannelList(), CancellationToken.None);
|
|
foreach (var channel in channelsToUpdate)
|
|
{
|
|
await workerChannel.WriteAsync(new RefreshChannelData(channel.Number), CancellationToken.None);
|
|
}
|
|
|
|
return Option<BaseError>.None;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BaseError.New("Failed to update channel numbers: " + ex.Message);
|
|
}
|
|
}
|
|
|
|
private static Option<BaseError> ValidateRequest(UpdateChannelNumbers request)
|
|
{
|
|
if (request.Channels.Count == 0)
|
|
{
|
|
return BaseError.New("At least one channel is required");
|
|
}
|
|
|
|
if (request.Channels.Select(c => c.Id).Distinct().Count() != request.Channels.Count)
|
|
{
|
|
return BaseError.New("Channel ids must be unique");
|
|
}
|
|
|
|
if (request.Channels.Select(c => c.Number).Distinct(StringComparer.Ordinal).Count() != request.Channels.Count)
|
|
{
|
|
return BaseError.New("Channel number must be unique");
|
|
}
|
|
|
|
foreach (ChannelSortViewModel channel in request.Channels)
|
|
{
|
|
if (!Regex.IsMatch(channel.Number, Channel.NumberValidator))
|
|
{
|
|
return BaseError.New("Invalid channel number; two decimals are allowed for subchannels");
|
|
}
|
|
}
|
|
|
|
return Option<BaseError>.None;
|
|
}
|
|
}
|