diff --git a/ErsatzTV/Services/SchedulerService.cs b/ErsatzTV/Services/SchedulerService.cs index 2a5d94c4b..98d2808ff 100644 --- a/ErsatzTV/Services/SchedulerService.cs +++ b/ErsatzTV/Services/SchedulerService.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using ErsatzTV.Application; using ErsatzTV.Application.MediaSources.Commands; using ErsatzTV.Application.Playouts.Commands; +using ErsatzTV.Application.Plex.Commands; +using ErsatzTV.Core.Domain; using ErsatzTV.Core.Interfaces.Locking; using ErsatzTV.Infrastructure.Data; using Microsoft.EntityFrameworkCore; @@ -54,6 +56,7 @@ namespace ErsatzTV.Services { await BuildPlayouts(cancellationToken); await ScanLocalMediaSources(cancellationToken); + await ScanPlexMediaSources(cancellationToken); } private async Task BuildPlayouts(CancellationToken cancellationToken) @@ -87,21 +90,26 @@ namespace ErsatzTV.Services cancellationToken); } } + } + + private async Task ScanPlexMediaSources(CancellationToken cancellationToken) + { + using IServiceScope scope = _serviceScopeFactory.CreateScope(); + TvContext dbContext = scope.ServiceProvider.GetRequiredService(); + + List plexLibraries = await dbContext.PlexLibraries + .Filter(l => l.ShouldSyncItems) + .ToListAsync(cancellationToken); - // List plexLibraries = await dbContext.PlexLibraries - // .Filter(l => l.ShouldSyncItems) - // .ToListAsync(cancellationToken); - // - // foreach (PlexLibrary library in plexLibraries) - // { - // // TODO: this locking won't work... - // // if (_entityLocker.LockMediaSource(library.PlexMediaSourceId)) - // // { - // // await _channel.WriteAsync( - // // new SynchronizePlexLibraryByIdIfNeeded(library.PlexMediaSourceId, library.Id), - // // cancellationToken); - // // } - // } + foreach (PlexLibrary library in plexLibraries) + { + if (_entityLocker.LockLibrary(library.Id)) + { + await _channel.WriteAsync( + new SynchronizePlexLibraryByIdIfNeeded(library.Id), + cancellationToken); + } + } } } }