Files
ersatztv/ErsatzTV.Application/Plex/Commands/CallPlexShowScannerHandler.cs
T
Jason DoveandGitHub 626048f9c3 change how scanner and main process communicate (#2555)
* report scanner progress using api

* process scanner search index updates through api

* update changelog

* update dependencies
2025-10-21 15:08:49 -05:00

96 lines
3.4 KiB
C#

using System.Globalization;
using ErsatzTV.Application.Libraries;
using ErsatzTV.Core;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Metadata;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.FFmpeg.Runtime;
using ErsatzTV.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace ErsatzTV.Application.Plex;
public class CallPlexShowScannerHandler : CallLibraryScannerHandler<SynchronizePlexShowById>,
IRequestHandler<SynchronizePlexShowById, Either<BaseError, string>>
{
private readonly IScannerProxyService _scannerProxyService;
public CallPlexShowScannerHandler(
IDbContextFactory<TvContext> dbContextFactory,
IConfigElementRepository configElementRepository,
IScannerProxyService scannerProxyService,
IRuntimeInfo runtimeInfo)
: base(dbContextFactory, configElementRepository, runtimeInfo)
{
_scannerProxyService = scannerProxyService;
}
Task<Either<BaseError, string>> IRequestHandler<SynchronizePlexShowById, Either<BaseError, string>>.Handle(
SynchronizePlexShowById request,
CancellationToken cancellationToken) => Handle(request, cancellationToken);
private async Task<Either<BaseError, string>> Handle(
SynchronizePlexShowById request,
CancellationToken cancellationToken)
{
Validation<BaseError, ScanParameters> validation = await Validate(request, cancellationToken);
return await validation.Match(
parameters => PerformScan(parameters, request, cancellationToken),
error =>
{
foreach (ScanIsNotRequired scanIsNotRequired in error.OfType<ScanIsNotRequired>())
{
return Task.FromResult<Either<BaseError, string>>(scanIsNotRequired);
}
return Task.FromResult<Either<BaseError, string>>(error.Join());
});
}
private async Task<Either<BaseError, string>> PerformScan(
ScanParameters parameters,
SynchronizePlexShowById request,
CancellationToken cancellationToken)
{
Option<Guid> maybeScanId = _scannerProxyService.StartScan(request.PlexLibraryId);
foreach (var scanId in maybeScanId)
{
try
{
var arguments = new List<string>
{
"scan-plex-show",
request.PlexLibraryId.ToString(CultureInfo.InvariantCulture),
request.ShowId.ToString(CultureInfo.InvariantCulture),
GetBaseUrl(scanId)
};
if (request.DeepScan)
{
arguments.Add("--deep");
}
return await base.PerformScan(parameters, arguments, cancellationToken);
}
finally
{
_scannerProxyService.EndScan(scanId);
}
}
return BaseError.New($"Library {request.PlexLibraryId} is already scanning");
}
protected override Task<Tuple<string, DateTimeOffset>> GetLastScan(
TvContext dbContext,
SynchronizePlexShowById request,
CancellationToken cancellationToken) =>
Task.FromResult(new Tuple<string, DateTimeOffset>(string.Empty, DateTimeOffset.MinValue));
protected override bool ScanIsRequired(
DateTimeOffset lastScan,
int libraryRefreshInterval,
SynchronizePlexShowById request) =>
true;
}