Exemplo n.º 1
0
    private async Task <List <PlexMediaSource> > SynchronizeSources(
        SynchronizePlexMediaSources request,
        CancellationToken cancellationToken)
    {
        using IServiceScope scope = _serviceScopeFactory.CreateScope();
        IMediator mediator = scope.ServiceProvider.GetRequiredService <IMediator>();

        Either <BaseError, List <PlexMediaSource> > result = await mediator.Send(request, cancellationToken);

        return(result.Match(
                   sources =>
        {
            if (sources.Any())
            {
                _logger.LogInformation("Successfully synchronized plex media sources");
            }

            return sources;
        },
                   error =>
        {
            _logger.LogWarning(
                "Unable to synchronize plex media sources: {Error}",
                error.Value);
            return new List <PlexMediaSource>();
        }));
    }
Exemplo n.º 2
0
        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (!File.Exists(FileSystemLayout.PlexSecretsPath))
            {
                await File.WriteAllTextAsync(FileSystemLayout.PlexSecretsPath, "{}", cancellationToken);
            }

            _logger.LogInformation(
                "Plex service started; secrets are at {PlexSecretsPath}",
                FileSystemLayout.PlexSecretsPath);

            // synchronize sources on startup
            await SynchronizeSources(new SynchronizePlexMediaSources(), cancellationToken);

            await foreach (IPlexBackgroundServiceRequest request in _channel.ReadAllAsync(cancellationToken))
            {
                try
                {
                    Task requestTask = request switch
                    {
                        TryCompletePlexPinFlow pinRequest => CompletePinFlow(pinRequest, cancellationToken),
                        SynchronizePlexMediaSources sourcesRequest => SynchronizeSources(
                            sourcesRequest,
                            cancellationToken),
                        SynchronizePlexLibraries synchronizePlexLibrariesRequest => SynchronizeLibraries(
                            synchronizePlexLibrariesRequest,
                            cancellationToken),
                        _ => throw new NotSupportedException($"Unsupported request type: {request.GetType().Name}")
                    };

                    await requestTask;
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex, "Failed to process plex background service request");
                }
            }
        }
 public Task <Either <BaseError, List <PlexMediaSource> > > Handle(
     SynchronizePlexMediaSources request,
     CancellationToken cancellationToken) => _plexTvApiClient.GetServers().BindAsync(SynchronizeAllServers);