diff --git a/src/Ombi.Schedule.Tests/OmbiQuartzTests.cs b/src/Ombi.Schedule.Tests/OmbiQuartzTests.cs index a8136a5ce..6d00f8ef6 100644 --- a/src/Ombi.Schedule.Tests/OmbiQuartzTests.cs +++ b/src/Ombi.Schedule.Tests/OmbiQuartzTests.cs @@ -19,7 +19,7 @@ namespace Ombi.Schedule.Tests It.IsAny())); var sut = new QuartzMock(scheduleMock); - await QuartzMock.TriggerJob("ABC"); + //await QuartzMock.TriggerJob("ABC"); scheduleMock.Verify(x => x.TriggerJob(It.Is(j => j.Name == "ABC"), default(CancellationToken)), Times.Once); diff --git a/src/Ombi.Schedule/IocJobFactory.cs b/src/Ombi.Schedule/IocJobFactory.cs index d9a300eee..83ab5a974 100644 --- a/src/Ombi.Schedule/IocJobFactory.cs +++ b/src/Ombi.Schedule/IocJobFactory.cs @@ -1,4 +1,5 @@ using System; +using Microsoft.Extensions.DependencyInjection; using Quartz; using Quartz.Spi; @@ -14,7 +15,12 @@ namespace Ombi.Schedule } public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) { - return _factory.GetService(bundle.JobDetail.JobType) as IJob; + var scopeFactory = _factory.GetService(); + var scope = scopeFactory.CreateScope(); + var scopedContainer = scope.ServiceProvider; + + var implementation = scopedContainer.GetRequiredService(bundle.JobDetail.JobType) as IJob; + return implementation; } public void ReturnJob(IJob job) diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs index 3e1e750ae..3f64a6505 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyContentSync.cs @@ -55,11 +55,8 @@ namespace Ombi.Schedule.Jobs.Emby // Episodes - await OmbiQuartz.TriggerJob(nameof(IEmbyEpisodeSync)); - - await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata)); - //BackgroundJob.Enqueue(() => _episodeSync.Start()); - //BackgroundJob.Enqueue(() => _metadata.Start()); + await OmbiQuartz.TriggerJob(nameof(IEmbyEpisodeSync), "Emby"); + await OmbiQuartz.TriggerJob(nameof(IRefreshMetadata), "Emby"); } diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs index 00a1b7da4..e55c46d26 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs @@ -66,7 +66,7 @@ namespace Ombi.Schedule.Jobs.Emby } - await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker)); + await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker), "Emby"); } private async Task CacheEpisodes(EmbyServers server) diff --git a/src/Ombi.Schedule/Jobs/Ombi/MediaDatabaseRefresh.cs b/src/Ombi.Schedule/Jobs/Ombi/MediaDatabaseRefresh.cs index 82f4ec883..cadabba4e 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/MediaDatabaseRefresh.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/MediaDatabaseRefresh.cs @@ -59,7 +59,7 @@ namespace Ombi.Schedule.Jobs.Ombi await _embyRepo.ExecuteSql(episodeSQL); await _embyRepo.ExecuteSql(mainSql); - await OmbiQuartz.TriggerJob(nameof(IEmbyContentSync)); + await OmbiQuartz.TriggerJob(nameof(IEmbyContentSync), "Emby"); } catch (Exception e) { diff --git a/src/Ombi.Schedule/Jobs/Ombi/RefreshMetadata.cs b/src/Ombi.Schedule/Jobs/Ombi/RefreshMetadata.cs index a2cebb36c..e4f175855 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/RefreshMetadata.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/RefreshMetadata.cs @@ -89,12 +89,12 @@ namespace Ombi.Schedule.Jobs.Ombi { if (plexSettings.Enable) { - await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker)); + await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex"); } if (embySettings.Enable) { - await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker)); + await OmbiQuartz.TriggerJob(nameof(IEmbyAvaliabilityChecker), "Emby"); } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs index f1c35abdb..9144f2eae 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs @@ -104,19 +104,19 @@ namespace Ombi.Schedule.Jobs.Plex { Logger.LogInformation("Starting EP Cacher"); - await OmbiQuartz.TriggerJob(nameof(IPlexEpisodeSync)); + await OmbiQuartz.TriggerJob(nameof(IPlexEpisodeSync), "Plex"); } if ((processedContent?.HasProcessedContent ?? false) && recentlyAddedSearch) { // Just check what we send it - BackgroundJob.Enqueue(() => Metadata.ProcessPlexServerContent(processedContent.Content)); + await OmbiQuartz.TriggerJob(nameof(IMediaDatabaseRefresh), "System"); } if ((processedContent?.HasProcessedEpisodes ?? false) && recentlyAddedSearch) { - await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker)); + await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex"); } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs index 433051e43..7414294be 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexEpisodeSync.cs @@ -56,8 +56,7 @@ namespace Ombi.Schedule.Jobs.Plex } - await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker)); - //BackgroundJob.Enqueue(() => _availabilityChecker.Start()); // TODO + await OmbiQuartz.TriggerJob(nameof(IPlexAvailabilityChecker), "Plex"); } private async Task Cache(PlexServers settings) diff --git a/src/Ombi.Schedule/OmbiQuartz.cs b/src/Ombi.Schedule/OmbiQuartz.cs index 1eb509b08..a4bd28312 100644 --- a/src/Ombi.Schedule/OmbiQuartz.cs +++ b/src/Ombi.Schedule/OmbiQuartz.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; +using Ombi.Helpers; using Quartz; using Quartz.Impl; using Quartz.Spi; @@ -40,7 +41,7 @@ namespace Ombi.Schedule where T : IJob { var jobBuilder = JobBuilder.Create() - .WithIdentity(name, group); + .WithIdentity(new JobKey(name, group)); if (jobData != null) { foreach (var o in jobData) @@ -49,19 +50,30 @@ namespace Ombi.Schedule } } - var job = jobBuilder.Build(); - - ITrigger jobTrigger = TriggerBuilder.Create() - .WithIdentity(name + "Trigger", group) - .WithCronSchedule(cronExpression) - .Build(); + if(!cronExpression.HasValue()) + { + jobBuilder.StoreDurably(true); + } + + var job = jobBuilder.Build(); + if (cronExpression.HasValue()) + { + ITrigger jobTrigger = TriggerBuilder.Create() + .WithIdentity(name + "Trigger", group) + .WithCronSchedule(cronExpression) + .Build(); + await Scheduler.ScheduleJob(job, jobTrigger); + } + else + { + await Scheduler.AddJob(job, true); + } - await Scheduler.ScheduleJob(job, jobTrigger); } - public static async Task TriggerJob(string jobName) + public static async Task TriggerJob(string jobName, string group) { - await Scheduler.TriggerJob(new JobKey(jobName)); + await Scheduler.TriggerJob(new JobKey(jobName, group)); } public static async Task Start() diff --git a/src/Ombi.Schedule/OmbiScheduler.cs b/src/Ombi.Schedule/OmbiScheduler.cs index 0e000ad46..31f4282e6 100644 --- a/src/Ombi.Schedule/OmbiScheduler.cs +++ b/src/Ombi.Schedule/OmbiScheduler.cs @@ -84,11 +84,15 @@ namespace Ombi.Schedule await OmbiQuartz.Instance.AddJob(nameof(IPlexContentSync) + "RecentlyAdded", "Plex", JobSettingsHelper.PlexContent(s), new Dictionary { { "recentlyAddedSearch", "true" } }); await OmbiQuartz.Instance.AddJob(nameof(IPlexRecentlyAddedSync), "Plex", JobSettingsHelper.PlexRecentlyAdded(s)); await OmbiQuartz.Instance.AddJob(nameof(IPlexUserImporter), "Plex", JobSettingsHelper.UserImporter(s)); + await OmbiQuartz.Instance.AddJob(nameof(IPlexEpisodeSync), "Plex", null); + await OmbiQuartz.Instance.AddJob(nameof(IPlexAvailabilityChecker), "Plex", null); } private static async Task AddEmby(JobSettings s) { await OmbiQuartz.Instance.AddJob(nameof(IEmbyContentSync), "Emby", JobSettingsHelper.EmbyContent(s)); + await OmbiQuartz.Instance.AddJob(nameof(IEmbyEpisodeSync), "Emby", null); + await OmbiQuartz.Instance.AddJob(nameof(IEmbyAvaliabilityChecker), "Emby", null); await OmbiQuartz.Instance.AddJob(nameof(IEmbyUserImporter), "Emby", JobSettingsHelper.UserImporter(s)); } } diff --git a/src/Ombi/Controllers/JobController.cs b/src/Ombi/Controllers/JobController.cs index 42f774351..ccc6a6702 100644 --- a/src/Ombi/Controllers/JobController.cs +++ b/src/Ombi/Controllers/JobController.cs @@ -38,7 +38,7 @@ namespace Ombi.Controllers public async Task ForceUpdate() { - await OmbiQuartz.TriggerJob(nameof(IOmbiAutomaticUpdater)); + await OmbiQuartz.TriggerJob(nameof(IOmbiAutomaticUpdater), "System"); return true; } @@ -87,7 +87,7 @@ namespace Ombi.Controllers [HttpPost("plexuserimporter")] public async Task PlexUserImporter() { - await OmbiQuartz.TriggerJob(nameof(IPlexUserImporter)); + await OmbiQuartz.TriggerJob(nameof(IPlexUserImporter), "Plex"); return true; } @@ -98,7 +98,7 @@ namespace Ombi.Controllers [HttpPost("embyuserimporter")] public async Task EmbyUserImporter() { - await OmbiQuartz.TriggerJob(nameof(IEmbyUserImporter)); + await OmbiQuartz.TriggerJob(nameof(IEmbyUserImporter), "Emby"); return true; } @@ -109,8 +109,7 @@ namespace Ombi.Controllers [HttpPost("plexcontentcacher")] public bool StartPlexContentCacher() { - OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(IPlexContentSync) + - "Trigger"), new JobDataMap(new Dictionary { { "recentlyAddedSearch", "false" } })); + OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(IPlexContentSync), "Plex"), new JobDataMap(new Dictionary { { "recentlyAddedSearch", "false" } })); return true; } @@ -121,7 +120,7 @@ namespace Ombi.Controllers [HttpPost("plexrecentlyadded")] public bool StartRecentlyAdded() { - OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(PlexContentSync)), new JobDataMap(new Dictionary { { "recentlyAddedSearch", "true" } })); + OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(IPlexContentSync), "Plex"), new JobDataMap(new Dictionary { { "recentlyAddedSearch", "true" } })); return true; } @@ -132,7 +131,7 @@ namespace Ombi.Controllers [HttpPost("embycontentcacher")] public async Task StartEmbyContentCacher() { - await OmbiQuartz.TriggerJob(nameof(IEmbyContentSync)); + await OmbiQuartz.TriggerJob(nameof(IEmbyContentSync), "Emby"); return true; } @@ -143,7 +142,7 @@ namespace Ombi.Controllers [HttpPost("newsletter")] public async Task StartNewsletter() { - await OmbiQuartz.TriggerJob(nameof(INewsletterJob)); + await OmbiQuartz.TriggerJob(nameof(INewsletterJob), "Emby"); return true; } } diff --git a/src/Ombi/Controllers/SettingsController.cs b/src/Ombi/Controllers/SettingsController.cs index 399375f95..e8f4154b8 100644 --- a/src/Ombi/Controllers/SettingsController.cs +++ b/src/Ombi/Controllers/SettingsController.cs @@ -386,7 +386,7 @@ namespace Ombi.Controllers _cache.Remove(CacheKeys.RadarrRootProfiles); _cache.Remove(CacheKeys.RadarrQualityProfiles); - await OmbiQuartz.TriggerJob(nameof(IRadarrSync)); + await OmbiQuartz.TriggerJob(nameof(IRadarrSync), "DVR"); } return result; }