mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-15 09:42:56 -07:00
Converted the Plex Jobs to use Quartz
This commit is contained in:
parent
85ff2c4017
commit
9ce0b9e70e
11 changed files with 199 additions and 43 deletions
|
@ -59,6 +59,7 @@ using Ombi.Schedule.Jobs.Plex.Interfaces;
|
|||
using Ombi.Schedule.Jobs.SickRage;
|
||||
using Ombi.Schedule.Processor;
|
||||
using Ombi.Store.Entities;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Ombi.DependencyInjection
|
||||
{
|
||||
|
@ -167,6 +168,7 @@ namespace Ombi.DependencyInjection
|
|||
|
||||
public static void RegisterJobs(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IJobFactory, IoCJobFactory>(provider => new IoCJobFactory(provider));
|
||||
services.AddTransient<IBackgroundJobClient, BackgroundJobClient>();
|
||||
|
||||
services.AddTransient<IPlexContentSync, PlexContentSync>();
|
||||
|
@ -187,7 +189,7 @@ namespace Ombi.DependencyInjection
|
|||
services.AddTransient<ISickRageSync, SickRageSync>();
|
||||
services.AddTransient<IRefreshMetadata, RefreshMetadata>();
|
||||
services.AddTransient<INewsletterJob, NewsletterJob>();
|
||||
services.AddTransient<IPlexRecentlyAddedSync, PlexRecentlyAddedSync>();
|
||||
//services.AddTransient<IPlexRecentlyAddedSync, PlexRecentlyAddedSync>();
|
||||
services.AddTransient<ILidarrAlbumSync, LidarrAlbumSync>();
|
||||
services.AddTransient<ILidarrArtistSync, LidarrArtistSync>();
|
||||
services.AddTransient<ILidarrAvailabilityChecker, LidarrAvailabilityChecker>();
|
||||
|
|
26
src/Ombi.Schedule/IocJobFactory.cs
Normal file
26
src/Ombi.Schedule/IocJobFactory.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using System;
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Ombi.Schedule
|
||||
{
|
||||
public class IoCJobFactory : IJobFactory
|
||||
{
|
||||
private readonly IServiceProvider _factory;
|
||||
|
||||
public IoCJobFactory(IServiceProvider factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
|
||||
{
|
||||
return _factory.GetService(bundle.JobDetail.JobType) as IJob;
|
||||
}
|
||||
|
||||
public void ReturnJob(IJob job)
|
||||
{
|
||||
var disposable = job as IDisposable;
|
||||
disposable?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -63,8 +63,8 @@ namespace Ombi.Schedule
|
|||
RecurringJob.AddOrUpdate(() => _embyContentSync.Start(), JobSettingsHelper.EmbyContent(s));
|
||||
RecurringJob.AddOrUpdate(() => _sonarrSync.Start(), JobSettingsHelper.Sonarr(s));
|
||||
RecurringJob.AddOrUpdate(() => _radarrSync.CacheContent(), JobSettingsHelper.Radarr(s));
|
||||
RecurringJob.AddOrUpdate(() => _plexContentSync.CacheContent(false), JobSettingsHelper.PlexContent(s));
|
||||
RecurringJob.AddOrUpdate(() => _plexRecentlyAddedSync.Start(), JobSettingsHelper.PlexRecentlyAdded(s));
|
||||
//RecurringJob.AddOrUpdate(() => _plexContentSync.Execute(null), JobSettingsHelper.PlexContent(s));
|
||||
//RecurringJob.AddOrUpdate(() => _plexRecentlyAddedSync.Start(), JobSettingsHelper.PlexRecentlyAdded(s));
|
||||
RecurringJob.AddOrUpdate(() => _cpCache.Start(), JobSettingsHelper.CouchPotato(s));
|
||||
RecurringJob.AddOrUpdate(() => _srSync.Start(), JobSettingsHelper.SickRageSync(s));
|
||||
RecurringJob.AddOrUpdate(() => _refreshMetadata.Start(), JobSettingsHelper.RefreshMetadata(s));
|
||||
|
@ -87,7 +87,6 @@ namespace Ombi.Schedule
|
|||
|
||||
if (disposing)
|
||||
{
|
||||
_plexContentSync?.Dispose();
|
||||
_radarrSync?.Dispose();
|
||||
_updater?.Dispose();
|
||||
_plexUserImporter?.Dispose();
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
using System.Threading.Tasks;
|
||||
using Quartz;
|
||||
|
||||
namespace Ombi.Schedule.Jobs
|
||||
{
|
||||
public interface IPlexContentSync : IBaseJob
|
||||
public interface IPlexContentSync : IJob
|
||||
{
|
||||
Task CacheContent(bool recentlyAddedSearch = false);
|
||||
}
|
||||
}
|
|
@ -42,6 +42,7 @@ using Ombi.Schedule.Jobs.Plex.Interfaces;
|
|||
using Ombi.Schedule.Jobs.Plex.Models;
|
||||
using Ombi.Store.Entities;
|
||||
using Ombi.Store.Repository;
|
||||
using Quartz;
|
||||
|
||||
namespace Ombi.Schedule.Jobs.Plex
|
||||
{
|
||||
|
@ -68,8 +69,11 @@ namespace Ombi.Schedule.Jobs.Plex
|
|||
private IRefreshMetadata Metadata { get; }
|
||||
private IPlexAvailabilityChecker Checker { get; }
|
||||
|
||||
public async Task CacheContent(bool recentlyAddedSearch = false)
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
JobDataMap dataMap = context.JobDetail.JobDataMap;
|
||||
var recentlyAddedSearch = dataMap.GetBooleanValueFromString("recentlyAddedSearch");
|
||||
|
||||
var plexSettings = await Plex.GetSettingsAsync();
|
||||
if (!plexSettings.Enable)
|
||||
{
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Hangfire;
|
||||
//using System;
|
||||
//using System.Threading.Tasks;
|
||||
//using Hangfire;
|
||||
|
||||
namespace Ombi.Schedule.Jobs.Plex
|
||||
{
|
||||
public class PlexRecentlyAddedSync : IPlexRecentlyAddedSync
|
||||
{
|
||||
public PlexRecentlyAddedSync(IPlexContentSync sync)
|
||||
{
|
||||
_sync = sync;
|
||||
}
|
||||
//namespace Ombi.Schedule.Jobs.Plex
|
||||
//{
|
||||
// public class PlexRecentlyAddedSync : IPlexRecentlyAddedSync
|
||||
// {
|
||||
// public PlexRecentlyAddedSync(IPlexContentSync sync)
|
||||
// {
|
||||
// _sync = sync;
|
||||
// }
|
||||
|
||||
private readonly IPlexContentSync _sync;
|
||||
// private readonly IPlexContentSync _sync;
|
||||
|
||||
public void Start()
|
||||
{
|
||||
BackgroundJob.Enqueue(() => _sync.CacheContent(true));
|
||||
}
|
||||
// public void Start()
|
||||
// {
|
||||
// BackgroundJob.Enqueue(() => _sync.CacheContent(true));
|
||||
// }
|
||||
|
||||
private bool _disposed;
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
// private bool _disposed;
|
||||
// protected virtual void Dispose(bool disposing)
|
||||
// {
|
||||
// if (_disposed)
|
||||
// return;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
_sync?.Dispose();
|
||||
}
|
||||
_disposed = true;
|
||||
}
|
||||
// if (disposing)
|
||||
// {
|
||||
// _sync?.Dispose();
|
||||
// }
|
||||
// _disposed = true;
|
||||
// }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
// public void Dispose()
|
||||
// {
|
||||
// Dispose(true);
|
||||
// GC.SuppressFinalize(this);
|
||||
// }
|
||||
// }
|
||||
//}
|
|
@ -16,6 +16,7 @@
|
|||
<PackageReference Include="Hangfire.MemoryStorage.Core" Version="1.4.0" />
|
||||
<PackageReference Include="Hangfire.RecurringJobExtensions" Version="1.1.6" />
|
||||
<PackageReference Include="Hangfire.SQLite" Version="1.4.2" />
|
||||
<PackageReference Include="Quartz" Version="3.0.6" />
|
||||
<PackageReference Include="Serilog" Version="2.6.0-dev-00892" />
|
||||
<PackageReference Include="SharpCompress" Version="0.18.2" />
|
||||
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
|
||||
|
|
70
src/Ombi.Schedule/OmbiQuartz.cs
Normal file
70
src/Ombi.Schedule/OmbiQuartz.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using System.Collections.Generic;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Ombi.Schedule
|
||||
{
|
||||
public class OmbiQuartz
|
||||
{
|
||||
private IScheduler _scheduler;
|
||||
|
||||
public static IScheduler Scheduler => Instance._scheduler;
|
||||
|
||||
// Singleton
|
||||
private static OmbiQuartz _instance;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton
|
||||
/// </summary>
|
||||
public static OmbiQuartz Instance => _instance ?? (_instance = new OmbiQuartz());
|
||||
|
||||
private OmbiQuartz()
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
private async void Init()
|
||||
{
|
||||
_scheduler = await new StdSchedulerFactory().GetScheduler();
|
||||
}
|
||||
|
||||
public IScheduler UseJobFactory(IJobFactory jobFactory)
|
||||
{
|
||||
Scheduler.JobFactory = jobFactory;
|
||||
return Scheduler;
|
||||
}
|
||||
|
||||
|
||||
public async void AddJob<T>(string name, string group, string cronExpression, Dictionary<string, string> jobData = null)
|
||||
where T : IJob
|
||||
{
|
||||
var jobBuilder = JobBuilder.Create<T>()
|
||||
.WithIdentity(name, group);
|
||||
if (jobData != null)
|
||||
{
|
||||
foreach (var o in jobData)
|
||||
{
|
||||
jobBuilder.UsingJobData(o.Key, o.Value);
|
||||
}
|
||||
}
|
||||
|
||||
var job = jobBuilder.Build();
|
||||
|
||||
|
||||
|
||||
ITrigger jobTrigger = TriggerBuilder.Create()
|
||||
.WithIdentity(name + "Trigger", group)
|
||||
.StartNow()
|
||||
.WithCronSchedule(cronExpression)
|
||||
.Build();
|
||||
|
||||
await Scheduler.ScheduleJob(job, jobTrigger);
|
||||
}
|
||||
|
||||
public static async void Start()
|
||||
{
|
||||
await Scheduler.Start();
|
||||
}
|
||||
}
|
||||
}
|
49
src/Ombi.Schedule/OmbiScheduler.cs
Normal file
49
src/Ombi.Schedule/OmbiScheduler.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Ombi.Core.Settings;
|
||||
using Ombi.Schedule.Jobs.Plex;
|
||||
using Ombi.Settings.Settings.Models;
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Ombi.Schedule
|
||||
{
|
||||
public static class OmbiScheduler
|
||||
{
|
||||
//public void Setup()
|
||||
//{
|
||||
// CreateJobDefinitions();
|
||||
//}
|
||||
|
||||
//public void CreateJobDefinitions()
|
||||
//{
|
||||
// var contentSync = JobBuilder.Create<PlexContentSync>()
|
||||
// .UsingJobData("recentlyAddedSearch", false)
|
||||
// .WithIdentity(nameof(PlexContentSync), "Plex")
|
||||
// .Build();
|
||||
|
||||
// var recentlyAdded = JobBuilder.Create<PlexContentSync>()
|
||||
// .UsingJobData("recentlyAddedSearch", true)
|
||||
// .WithIdentity("PlexRecentlyAdded", "Plex")
|
||||
// .Build();
|
||||
//}
|
||||
|
||||
public static void UseQuartz(this IApplicationBuilder app)
|
||||
{
|
||||
// Job Factory through IOC container
|
||||
var jobFactory = (IJobFactory)app.ApplicationServices.GetService(typeof(IJobFactory));
|
||||
var service = (ISettingsService<JobSettings>)app.ApplicationServices.GetService(typeof(ISettingsService<JobSettings>));
|
||||
var s = service.GetSettings();
|
||||
// Set job factory
|
||||
OmbiQuartz.Instance.UseJobFactory(jobFactory);
|
||||
|
||||
// Run configuration
|
||||
OmbiQuartz.Instance.AddJob<PlexContentSync>(nameof(PlexContentSync), "Plex", JobSettingsHelper.PlexContent(s), new Dictionary<string, string>{{ "recentlyAddedSearch", "false" } });
|
||||
OmbiQuartz.Instance.AddJob<PlexContentSync>(nameof(PlexContentSync), "Plex", JobSettingsHelper.PlexContent(s), new Dictionary<string, string> { { "recentlyAddedSearch", "true" } });
|
||||
|
||||
// Run Quartz
|
||||
OmbiQuartz.Start();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Hangfire;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
@ -6,10 +7,12 @@ using Microsoft.Extensions.Caching.Memory;
|
|||
using Ombi.Api.Service;
|
||||
using Ombi.Attributes;
|
||||
using Ombi.Helpers;
|
||||
using Ombi.Schedule;
|
||||
using Ombi.Schedule.Jobs;
|
||||
using Ombi.Schedule.Jobs.Emby;
|
||||
using Ombi.Schedule.Jobs.Ombi;
|
||||
using Ombi.Schedule.Jobs.Plex;
|
||||
using Quartz;
|
||||
|
||||
namespace Ombi.Controllers
|
||||
{
|
||||
|
@ -117,7 +120,7 @@ namespace Ombi.Controllers
|
|||
[HttpPost("plexcontentcacher")]
|
||||
public bool StartPlexContentCacher()
|
||||
{
|
||||
BackgroundJob.Enqueue(() => _plexContentSync.CacheContent(false));
|
||||
OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(PlexContentSync)), new JobDataMap(new Dictionary<string, string> { { "recentlyAddedSearch", "false" } }));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -128,7 +131,7 @@ namespace Ombi.Controllers
|
|||
[HttpPost("plexrecentlyadded")]
|
||||
public bool StartRecentlyAdded()
|
||||
{
|
||||
BackgroundJob.Enqueue(() => _plexContentSync.CacheContent(true));
|
||||
OmbiQuartz.Scheduler.TriggerJob(new JobKey(nameof(PlexContentSync)), new JobDataMap(new Dictionary<string, string> { { "recentlyAddedSearch", "true" } }));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -147,6 +147,8 @@ namespace Ombi
|
|||
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
|
||||
});
|
||||
|
||||
app.UseQuartz();
|
||||
|
||||
var ctx = serviceProvider.GetService<IOmbiContext>();
|
||||
loggerFactory.AddSerilog();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue