add library scan progress detail (#133)

* add library scan progress detail

* scan plex libraries on plex thread
This commit is contained in:
Jason Dove
2021-04-04 10:44:10 -05:00
committed by GitHub
parent c5ee5903b2
commit 6b44873474
39 changed files with 4637 additions and 263 deletions
+35 -21
View File
@@ -52,17 +52,24 @@ namespace ErsatzTV.Services
{
try
{
Task requestTask = request switch
Task requestTask;
switch (request)
{
TryCompletePlexPinFlow pinRequest => CompletePinFlow(pinRequest, cancellationToken),
SynchronizePlexMediaSources sourcesRequest => SynchronizeSources(
sourcesRequest,
cancellationToken),
SynchronizePlexLibraries synchronizePlexLibrariesRequest => SynchronizeLibraries(
synchronizePlexLibrariesRequest,
cancellationToken),
_ => throw new NotSupportedException($"Unsupported request type: {request.GetType().Name}")
};
case TryCompletePlexPinFlow pinRequest:
requestTask = CompletePinFlow(pinRequest, cancellationToken);
break;
case SynchronizePlexMediaSources sourcesRequest:
requestTask = SynchronizeSources(sourcesRequest, cancellationToken);
break;
case SynchronizePlexLibraries synchronizePlexLibrariesRequest:
requestTask = SynchronizeLibraries(synchronizePlexLibrariesRequest, cancellationToken);
break;
case ISynchronizePlexLibraryById synchronizePlexLibraryById:
requestTask = SynchronizePlexLibrary(synchronizePlexLibraryById, cancellationToken);
break;
default:
throw new NotSupportedException($"Unsupported request type: {request.GetType().Name}");
}
await requestTask;
}
@@ -109,17 +116,8 @@ namespace ErsatzTV.Services
Either<BaseError, bool> result = await mediator.Send(request, cancellationToken);
result.BiIter(
success =>
{
if (success)
{
_logger.LogInformation("Successfully authenticated with plex");
}
else
{
_logger.LogInformation("Plex authentication timeout");
}
},
success => _logger.LogInformation(
success ? "Successfully authenticated with plex" : "Plex authentication timeout"),
error => _logger.LogWarning("Unable to poll plex token: {Error}", error.Value));
}
@@ -138,5 +136,21 @@ namespace ErsatzTV.Services
request.PlexMediaSourceId,
error.Value));
}
private async Task SynchronizePlexLibrary(
ISynchronizePlexLibraryById request,
CancellationToken cancellationToken)
{
using IServiceScope scope = _serviceScopeFactory.CreateScope();
IMediator mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
Either<BaseError, string> result = await mediator.Send(request, cancellationToken);
result.BiIter(
name => _logger.LogDebug("Done synchronizing plex library {Name}", name),
error => _logger.LogWarning(
"Unable to synchronize plex library {LibraryId}: {Error}",
request.PlexLibraryId,
error.Value));
}
}
}
+10 -7
View File
@@ -21,19 +21,22 @@ namespace ErsatzTV.Services
{
public class SchedulerService : BackgroundService
{
private readonly ChannelWriter<IBackgroundServiceRequest> _channel;
private readonly IEntityLocker _entityLocker;
private readonly ILogger<SchedulerService> _logger;
private readonly ChannelWriter<IPlexBackgroundServiceRequest> _plexWorkerChannel;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ChannelWriter<IBackgroundServiceRequest> _workerChannel;
public SchedulerService(
IServiceScopeFactory serviceScopeFactory,
ChannelWriter<IBackgroundServiceRequest> channel,
ChannelWriter<IBackgroundServiceRequest> workerChannel,
ChannelWriter<IPlexBackgroundServiceRequest> plexWorkerChannel,
IEntityLocker entityLocker,
ILogger<SchedulerService> logger)
{
_serviceScopeFactory = serviceScopeFactory;
_channel = channel;
_workerChannel = workerChannel;
_plexWorkerChannel = plexWorkerChannel;
_entityLocker = entityLocker;
_logger = logger;
}
@@ -74,7 +77,7 @@ namespace ErsatzTV.Services
List<int> playoutIds = await dbContext.Playouts.Map(p => p.Id).ToListAsync(cancellationToken);
foreach (int playoutId in playoutIds)
{
await _channel.WriteAsync(new BuildPlayout(playoutId), cancellationToken);
await _workerChannel.WriteAsync(new BuildPlayout(playoutId), cancellationToken);
}
}
@@ -92,7 +95,7 @@ namespace ErsatzTV.Services
{
if (_entityLocker.LockLibrary(libraryId))
{
await _channel.WriteAsync(
await _workerChannel.WriteAsync(
new ScanLocalLibraryIfNeeded(libraryId),
cancellationToken);
}
@@ -112,7 +115,7 @@ namespace ErsatzTV.Services
{
if (_entityLocker.LockLibrary(library.Id))
{
await _channel.WriteAsync(
await _plexWorkerChannel.WriteAsync(
new SynchronizePlexLibraryByIdIfNeeded(library.Id),
cancellationToken);
}
@@ -120,6 +123,6 @@ namespace ErsatzTV.Services
}
private ValueTask RebuildSearchIndex(CancellationToken cancellationToken) =>
_channel.WriteAsync(new RebuildSearchIndex(), cancellationToken);
_workerChannel.WriteAsync(new RebuildSearchIndex(), cancellationToken);
}
}
-14
View File
@@ -5,7 +5,6 @@ using System.Threading.Tasks;
using ErsatzTV.Application;
using ErsatzTV.Application.MediaSources.Commands;
using ErsatzTV.Application.Playouts.Commands;
using ErsatzTV.Application.Plex.Commands;
using ErsatzTV.Application.Search.Commands;
using ErsatzTV.Core;
using LanguageExt;
@@ -70,19 +69,6 @@ namespace ErsatzTV.Services
scanLocalLibrary.LibraryId,
error.Value));
break;
case ISynchronizePlexLibraryById synchronizePlexLibraryById:
Either<BaseError, string> result = await mediator.Send(
synchronizePlexLibraryById,
cancellationToken);
result.BiIter(
name => _logger.LogDebug(
"Done synchronizing plex library {Name}",
name),
error => _logger.LogWarning(
"Unable to synchronize plex library {LibraryId}: {Error}",
synchronizePlexLibraryById.PlexLibraryId,
error.Value));
break;
case RebuildSearchIndex rebuildSearchIndex:
await mediator.Send(rebuildSearchIndex, cancellationToken);
break;