Scan queue handler now returns a QueueLibraryScanResult enum (Queued|NotFound|SyncDisabled|AlreadyScanning) instead of a lying bool; LibrariesController.ScanLibrary maps them to 202/404/422/409 with ProblemDetails. Guard the lock->enqueue with the EnqueueWithTraktLock compensating-unlock pattern. ScannerService now releases every library/collection lock in a finally so a handler exception can't leak the lock. Plex "Shows" scheduler batch (one lock, two messages) now has only the trailing SynchronizePlexNetworks carry the single release (Unlock flag), mirroring the scheduler Trakt tail-token precedent. Guard the other lock->enqueue producers (Create/UpdateLocalLibrary, UpdateTraktList) with compensating unlock. SPA drops the PENDING_GRACE_TICKS heuristic now that the POST reports 202/409/404/422 directly: 202 -> pending+poll, 409 -> reconcile (no error toast), 404/422 -> surface error. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
86 lines
3.3 KiB
C#
86 lines
3.3 KiB
C#
using System.Threading.Channels;
|
|
using ErsatzTV.Application.MediaSources;
|
|
using ErsatzTV.Core;
|
|
using ErsatzTV.Core.Domain;
|
|
using ErsatzTV.Core.Interfaces.Locking;
|
|
using ErsatzTV.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using static ErsatzTV.Application.Libraries.Mapper;
|
|
|
|
namespace ErsatzTV.Application.Libraries;
|
|
|
|
public class CreateLocalLibraryHandler : LocalLibraryHandlerBase,
|
|
IRequestHandler<CreateLocalLibrary, Either<BaseError, LocalLibraryViewModel>>
|
|
{
|
|
private readonly IDbContextFactory<TvContext> _dbContextFactory;
|
|
private readonly IEntityLocker _entityLocker;
|
|
private readonly ChannelWriter<IScannerBackgroundServiceRequest> _scannerWorkerChannel;
|
|
|
|
public CreateLocalLibraryHandler(
|
|
ChannelWriter<IScannerBackgroundServiceRequest> scannerWorkerChannel,
|
|
IEntityLocker entityLocker,
|
|
IDbContextFactory<TvContext> dbContextFactory)
|
|
{
|
|
_scannerWorkerChannel = scannerWorkerChannel;
|
|
_entityLocker = entityLocker;
|
|
_dbContextFactory = dbContextFactory;
|
|
}
|
|
|
|
public async Task<Either<BaseError, LocalLibraryViewModel>> Handle(
|
|
CreateLocalLibrary request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
await using TvContext dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);
|
|
Validation<BaseError, LocalLibrary> validation = await Validate(dbContext, request);
|
|
return await validation.Apply(localLibrary => PersistLocalLibrary(dbContext, localLibrary));
|
|
}
|
|
|
|
private async Task<LocalLibraryViewModel> PersistLocalLibrary(
|
|
TvContext dbContext,
|
|
LocalLibrary localLibrary)
|
|
{
|
|
await dbContext.LocalLibraries.AddAsync(localLibrary);
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
if (_entityLocker.LockLibrary(localLibrary.Id))
|
|
{
|
|
try
|
|
{
|
|
await _scannerWorkerChannel.WriteAsync(new ForceScanLocalLibrary(localLibrary.Id));
|
|
}
|
|
catch
|
|
{
|
|
// the scanner only unlocks when it receives the message; if the enqueue fails
|
|
// after we acquired the lock, release it here or it is held forever.
|
|
_entityLocker.UnlockLibrary(localLibrary.Id);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return ProjectToViewModel(localLibrary);
|
|
}
|
|
|
|
private static Task<Validation<BaseError, LocalLibrary>> Validate(
|
|
TvContext dbContext,
|
|
CreateLocalLibrary request) =>
|
|
MediaSourceMustExist(dbContext, request)
|
|
.BindT(localLibrary => NameMustBeValid(request, localLibrary))
|
|
.BindT(localLibrary => PathsMustBeValid(dbContext, localLibrary));
|
|
|
|
private static Task<Validation<BaseError, LocalLibrary>> MediaSourceMustExist(
|
|
TvContext dbContext,
|
|
CreateLocalLibrary request) =>
|
|
dbContext.LocalMediaSources
|
|
.OrderBy(lms => lms.Id)
|
|
.FirstOrDefaultAsync()
|
|
.Map(Optional)
|
|
.MapT(lms => new LocalLibrary
|
|
{
|
|
Name = request.Name,
|
|
Paths = request.Paths.Map(p => new LibraryPath { Path = p }).ToList(),
|
|
MediaKind = request.MediaKind,
|
|
MediaSourceId = lms.Id
|
|
})
|
|
.Map(o => o.ToValidation<BaseError>("LocalMediaSource does not exist."));
|
|
}
|