diff --git a/src/Ombi.Core/Engine/MovieRequestEngine.cs b/src/Ombi.Core/Engine/MovieRequestEngine.cs index 51a1c14db..751cc9ebf 100644 --- a/src/Ombi.Core/Engine/MovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/MovieRequestEngine.cs @@ -318,7 +318,7 @@ namespace Ombi.Core.Engine request.Denied = true; request.DeniedReason = denyReason; // We are denying a request - NotificationHelper.Notify(request, NotificationType.RequestDeclined); + await NotificationHelper.Notify(request, NotificationType.RequestDeclined); await MovieRepository.Update(request); return new RequestEngineResult @@ -346,7 +346,7 @@ namespace Ombi.Core.Engine var canNotify = await RunSpecificRule(request, SpecificRules.CanSendNotification); if (canNotify.Success) { - NotificationHelper.Notify(request, NotificationType.RequestApproved); + await NotificationHelper.Notify(request, NotificationType.RequestApproved); } if (request.Approved) @@ -462,7 +462,7 @@ namespace Ombi.Core.Engine request.Available = true; request.MarkedAsAvailable = DateTime.Now; - NotificationHelper.Notify(request, NotificationType.RequestAvailable); + await NotificationHelper.Notify(request, NotificationType.RequestAvailable); await MovieRepository.Update(request); return new RequestEngineResult @@ -478,8 +478,8 @@ namespace Ombi.Core.Engine var result = await RunSpecificRule(model, SpecificRules.CanSendNotification); if (result.Success) - { - NotificationHelper.NewRequest(model); + { + await NotificationHelper.NewRequest(model); } await _requestLog.Add(new RequestLog diff --git a/src/Ombi.Core/Engine/MusicRequestEngine.cs b/src/Ombi.Core/Engine/MusicRequestEngine.cs index 8457de515..ce69cb0b3 100644 --- a/src/Ombi.Core/Engine/MusicRequestEngine.cs +++ b/src/Ombi.Core/Engine/MusicRequestEngine.cs @@ -314,7 +314,7 @@ namespace Ombi.Core.Engine request.Denied = true; request.DeniedReason = reason; // We are denying a request - NotificationHelper.Notify(request, NotificationType.RequestDeclined); + await NotificationHelper.Notify(request, NotificationType.RequestDeclined); await MusicRepository.Update(request); return new RequestEngineResult @@ -342,7 +342,7 @@ namespace Ombi.Core.Engine var canNotify = await RunSpecificRule(request, SpecificRules.CanSendNotification); if (canNotify.Success) { - NotificationHelper.Notify(request, NotificationType.RequestApproved); + await NotificationHelper.Notify(request, NotificationType.RequestApproved); } if (request.Approved) @@ -469,7 +469,7 @@ namespace Ombi.Core.Engine request.Available = true; request.MarkedAsAvailable = DateTime.Now; - NotificationHelper.Notify(request, NotificationType.RequestAvailable); + await NotificationHelper.Notify(request, NotificationType.RequestAvailable); await MusicRepository.Update(request); return new RequestEngineResult @@ -486,7 +486,7 @@ namespace Ombi.Core.Engine var result = await RunSpecificRule(model, SpecificRules.CanSendNotification); if (result.Success) { - NotificationHelper.NewRequest(model); + await NotificationHelper.NewRequest(model); } await _requestLog.Add(new RequestLog diff --git a/src/Ombi.Core/Engine/TvRequestEngine.cs b/src/Ombi.Core/Engine/TvRequestEngine.cs index 28ab90a89..74ba55d31 100644 --- a/src/Ombi.Core/Engine/TvRequestEngine.cs +++ b/src/Ombi.Core/Engine/TvRequestEngine.cs @@ -159,7 +159,6 @@ namespace Ombi.Core.Engine } await CheckForSubscription(shouldHide, allRequests); - allRequests.ForEach(async r => { }); return new RequestsViewModel { @@ -389,7 +388,7 @@ namespace Ombi.Core.Engine if (request.Approved) { - NotificationHelper.Notify(request, NotificationType.RequestApproved); + await NotificationHelper.Notify(request, NotificationType.RequestApproved); // Autosend await TvSender.Send(request); } @@ -412,7 +411,7 @@ namespace Ombi.Core.Engine request.Denied = true; request.DeniedReason = reason; await TvRepository.UpdateChild(request); - NotificationHelper.Notify(request, NotificationType.RequestDeclined); + await NotificationHelper.Notify(request, NotificationType.RequestDeclined); return new RequestEngineResult { Result = true @@ -500,7 +499,7 @@ namespace Ombi.Core.Engine } } await TvRepository.UpdateChild(request); - NotificationHelper.Notify(request, NotificationType.RequestAvailable); + await NotificationHelper.Notify(request, NotificationType.RequestAvailable); return new RequestEngineResult { Result = true, @@ -585,7 +584,7 @@ namespace Ombi.Core.Engine var sendRuleResult = await RunSpecificRule(model, SpecificRules.CanSendNotification); if (sendRuleResult.Success) { - NotificationHelper.NewRequest(model); + await NotificationHelper.NewRequest(model); } await _requestLog.Add(new RequestLog @@ -600,7 +599,7 @@ namespace Ombi.Core.Engine if (model.Approved) { // Autosend - NotificationHelper.Notify(model, NotificationType.RequestApproved); + await NotificationHelper.Notify(model, NotificationType.RequestApproved); var result = await TvSender.Send(model); if (result.Success) { diff --git a/src/Ombi.Core/Helpers/NotificationHelper.cs b/src/Ombi.Core/Helpers/NotificationHelper.cs index 1615b24f7..f94e8f0db 100644 --- a/src/Ombi.Core/Helpers/NotificationHelper.cs +++ b/src/Ombi.Core/Helpers/NotificationHelper.cs @@ -1,5 +1,6 @@ using System; -using Hangfire; +using System.Collections.Generic; +using System.Threading.Tasks; using Ombi.Core.Notifications; using Ombi.Helpers; using Ombi.Notifications.Models; @@ -9,13 +10,7 @@ namespace Ombi.Core { public class NotificationHelper : INotificationHelper { - public NotificationHelper(INotificationService service) - { - NotificationService = service; - } - private INotificationService NotificationService { get; } - - public void NewRequest(FullBaseRequest model) + public async Task NewRequest(FullBaseRequest model) { var notificationModel = new NotificationOptions { @@ -24,11 +19,13 @@ namespace Ombi.Core NotificationType = NotificationType.NewRequest, RequestType = model.RequestType }; - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); - + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); } - public void NewRequest(ChildRequests model) + public async Task NewRequest(ChildRequests model) { var notificationModel = new NotificationOptions { @@ -36,11 +33,14 @@ namespace Ombi.Core DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest, RequestType = model.RequestType - }; - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + }; + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); } - public void NewRequest(AlbumRequest model) + public async Task NewRequest(AlbumRequest model) { var notificationModel = new NotificationOptions { @@ -48,12 +48,15 @@ namespace Ombi.Core DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest, RequestType = model.RequestType - }; - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + }; + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); } - public void Notify(MovieRequests model, NotificationType type) + public async Task Notify(MovieRequests model, NotificationType type) { var notificationModel = new NotificationOptions { @@ -63,10 +66,13 @@ namespace Ombi.Core RequestType = model.RequestType, Recipient = model.RequestedUser?.Email ?? string.Empty }; - - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); } - public void Notify(ChildRequests model, NotificationType type) + public async Task Notify(ChildRequests model, NotificationType type) { var notificationModel = new NotificationOptions { @@ -76,10 +82,13 @@ namespace Ombi.Core RequestType = model.RequestType, Recipient = model.RequestedUser?.Email ?? string.Empty }; - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); } - public void Notify(AlbumRequest model, NotificationType type) + public async Task Notify(AlbumRequest model, NotificationType type) { var notificationModel = new NotificationOptions { @@ -90,7 +99,18 @@ namespace Ombi.Core Recipient = model.RequestedUser?.Email ?? string.Empty }; - BackgroundJob.Enqueue(() => NotificationService.Publish(notificationModel)); + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, notificationModel} + }); + } + + public async Task Notify(NotificationOptions model) + { + await OmbiQuartz.TriggerJob(nameof(INotificationService), "Notifications", new Dictionary + { + {JobDataKeys.NotificationOptions, model} + }); } } } \ No newline at end of file diff --git a/src/Ombi.Core/Senders/INotificationHelper.cs b/src/Ombi.Core/Senders/INotificationHelper.cs index 4ba47d761..70947a57e 100644 --- a/src/Ombi.Core/Senders/INotificationHelper.cs +++ b/src/Ombi.Core/Senders/INotificationHelper.cs @@ -1,16 +1,19 @@ -using Ombi.Core.Models.Requests; +using System.Threading.Tasks; +using Ombi.Core.Models.Requests; using Ombi.Helpers; +using Ombi.Notifications.Models; using Ombi.Store.Entities.Requests; namespace Ombi.Core { public interface INotificationHelper { - void NewRequest(FullBaseRequest model); - void NewRequest(ChildRequests model); - void NewRequest(AlbumRequest model); - void Notify(MovieRequests model, NotificationType type); - void Notify(ChildRequests model, NotificationType type); - void Notify(AlbumRequest model, NotificationType type); + Task NewRequest(FullBaseRequest model); + Task NewRequest(ChildRequests model); + Task NewRequest(AlbumRequest model); + Task Notify(MovieRequests model, NotificationType type); + Task Notify(ChildRequests model, NotificationType type); + Task Notify(AlbumRequest model, NotificationType type); + Task Notify(NotificationOptions model); } } \ No newline at end of file diff --git a/src/Ombi.Core/Senders/MovieSender.cs b/src/Ombi.Core/Senders/MovieSender.cs index 567df43b5..c9aa5e4f2 100644 --- a/src/Ombi.Core/Senders/MovieSender.cs +++ b/src/Ombi.Core/Senders/MovieSender.cs @@ -95,7 +95,7 @@ namespace Ombi.Core.Senders Type = RequestType.Movie, RetryCount = 0 }); - _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); + await _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); } } diff --git a/src/Ombi.Core/Senders/MusicSender.cs b/src/Ombi.Core/Senders/MusicSender.cs index 04544c6be..e4bf27855 100644 --- a/src/Ombi.Core/Senders/MusicSender.cs +++ b/src/Ombi.Core/Senders/MusicSender.cs @@ -65,7 +65,7 @@ namespace Ombi.Core.Senders Type = RequestType.Album, RetryCount = 0 }); - _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); + await _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); } } diff --git a/src/Ombi.Core/Senders/TvSender.cs b/src/Ombi.Core/Senders/TvSender.cs index 3a3e34745..5cf28fa8a 100644 --- a/src/Ombi.Core/Senders/TvSender.cs +++ b/src/Ombi.Core/Senders/TvSender.cs @@ -128,7 +128,7 @@ namespace Ombi.Core.Senders Type = RequestType.TvShow, RetryCount = 0 }); - _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); + await _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); } } diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index df0162c1a..c4e66c6f5 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -181,7 +181,8 @@ namespace Ombi.DependencyInjection public static void RegisterJobs(this IServiceCollection services) { - services.AddSingleton(provider => new IoCJobFactory(provider)); + services.AddSingleton(); + services.AddSingleton(); services.AddTransient(); services.AddTransient(); diff --git a/src/Ombi.Schedule/JobDataKeys.cs b/src/Ombi.Helpers/JobDataKeys.cs similarity index 52% rename from src/Ombi.Schedule/JobDataKeys.cs rename to src/Ombi.Helpers/JobDataKeys.cs index 46d2dee2a..e0e2f7451 100644 --- a/src/Ombi.Schedule/JobDataKeys.cs +++ b/src/Ombi.Helpers/JobDataKeys.cs @@ -1,7 +1,8 @@ -namespace Ombi.Schedule +namespace Ombi.Helpers { public class JobDataKeys { public const string RecentlyAddedSearch = "recentlyAddedSearch"; + public const string NotificationOptions = nameof(NotificationOptions); } } \ No newline at end of file diff --git a/src/Ombi.Helpers/Ombi.Helpers.csproj b/src/Ombi.Helpers/Ombi.Helpers.csproj index 5dedaff61..8af8b9861 100644 --- a/src/Ombi.Helpers/Ombi.Helpers.csproj +++ b/src/Ombi.Helpers/Ombi.Helpers.csproj @@ -14,6 +14,7 @@ + diff --git a/src/Ombi.Schedule/OmbiQuartz.cs b/src/Ombi.Helpers/OmbiQuartz.cs similarity index 90% rename from src/Ombi.Schedule/OmbiQuartz.cs rename to src/Ombi.Helpers/OmbiQuartz.cs index 715bb187b..7979bc2a7 100644 --- a/src/Ombi.Schedule/OmbiQuartz.cs +++ b/src/Ombi.Helpers/OmbiQuartz.cs @@ -1,11 +1,10 @@ using System.Collections.Generic; using System.Threading.Tasks; -using Ombi.Helpers; using Quartz; using Quartz.Impl; using Quartz.Spi; -namespace Ombi.Schedule +namespace Ombi.Helpers { public class OmbiQuartz { @@ -78,7 +77,12 @@ namespace Ombi.Schedule { await Scheduler.TriggerJob(new JobKey(jobName, group)); } - + + public static async Task TriggerJob(string jobName, string group, IDictionary data) + { + await Scheduler.TriggerJob(new JobKey(jobName, group), new JobDataMap(data)); + } + public static async Task Start() { await Scheduler.Start(); diff --git a/src/Ombi.Notifications/Interfaces/INotificationService.cs b/src/Ombi.Notifications/Interfaces/INotificationService.cs index f8731b4af..f6a3761c3 100644 --- a/src/Ombi.Notifications/Interfaces/INotificationService.cs +++ b/src/Ombi.Notifications/Interfaces/INotificationService.cs @@ -1,13 +1,12 @@ using System.Threading.Tasks; using Ombi.Notifications; using Ombi.Notifications.Models; +using Quartz; namespace Ombi.Core.Notifications { - public interface INotificationService + public interface INotificationService : IJob { - Task Publish(NotificationOptions model); - Task Publish(NotificationOptions model, Ombi.Settings.Settings.Models.Settings settings); - Task PublishTest(NotificationOptions model, Ombi.Settings.Settings.Models.Settings settings, INotification type); + } } \ No newline at end of file diff --git a/src/Ombi.Notifications/NotificationService.cs b/src/Ombi.Notifications/NotificationService.cs index c2985a21b..8a24382aa 100644 --- a/src/Ombi.Notifications/NotificationService.cs +++ b/src/Ombi.Notifications/NotificationService.cs @@ -7,78 +7,46 @@ using Microsoft.Extensions.Logging; using Ombi.Core.Notifications; using Ombi.Helpers; using Ombi.Notifications.Models; +using Quartz; namespace Ombi.Notifications { public class NotificationService : INotificationService { + private readonly IServiceProvider _provider; + public NotificationService(IServiceProvider provider, ILogger log) { + _provider = provider; Log = log; NotificationAgents = new List(); - - var baseSearchType = typeof(BaseNotification<>).Name; - - var ass = typeof(NotificationService).GetTypeInfo().Assembly; - - foreach (var ti in ass.DefinedTypes) - { - if (ti?.BaseType?.Name == baseSearchType) - { - var type = ti?.AsType(); - var ctors = type.GetConstructors(); - var ctor = ctors.FirstOrDefault(); - - var services = new List(); - foreach (var param in ctor.GetParameters()) - { - services.Add(provider.GetService(param.ParameterType)); - } - - var item = Activator.CreateInstance(type, services.ToArray()); - NotificationAgents.Add((INotification)item); - } - } + PopulateAgents(); } - + private List NotificationAgents { get; } private ILogger Log { get; } - /// ^ + /// /// Sends a notification to the user. This one is used in normal notification scenarios /// - /// The model. + /// The model. /// - public async Task Publish(NotificationOptions model) + public async Task Execute(IJobExecutionContext context) { - var notificationTasks = new List(); - + JobDataMap dataMap = context.MergedJobDataMap; + var model = (NotificationOptions)dataMap.Get(JobDataKeys.NotificationOptions); + foreach (var agent in NotificationAgents) { - notificationTasks.Add(NotifyAsync(agent,model)); + await NotifyAsync(agent, model); } - await Task.WhenAll(notificationTasks).ConfigureAwait(false); } - /// - /// Sends a notification to the user, this is usually for testing the settings. - /// - /// The model. - /// The settings. - /// - public async Task Publish(NotificationOptions model, Settings.Settings.Models.Settings settings) - { - var notificationTasks = NotificationAgents.Select(notification => NotifyAsync(notification, model, settings)); - - await Task.WhenAll(notificationTasks).ConfigureAwait(false); - } - - private async Task NotifyAsync(INotification notification, NotificationOptions model) { try { - await notification.NotifyAsync(model).ConfigureAwait(false); + await notification.NotifyAsync(model); } catch (Exception ex) { @@ -86,26 +54,31 @@ namespace Ombi.Notifications } } - - private async Task NotifyAsync(INotification notification, NotificationOptions model, Ombi.Settings.Settings.Models.Settings settings) + + private void PopulateAgents() { - if (model.RequestId == 0) - { - throw new ArgumentException("RequestId is not set"); - } - try - { - await notification.NotifyAsync(model, settings).ConfigureAwait(false); - } - catch (Exception ex) + var baseSearchType = typeof(BaseNotification<>).Name; + + var ass = typeof(NotificationService).GetTypeInfo().Assembly; + + foreach (var ti in ass.DefinedTypes) { - throw new InvalidOperationException(ex.Message); - } - } + if (ti?.BaseType?.Name == baseSearchType) + { + var type = ti?.AsType(); + var ctors = type.GetConstructors(); + var ctor = ctors.FirstOrDefault(); - public async Task PublishTest(NotificationOptions model, Ombi.Settings.Settings.Models.Settings settings, INotification type) - { - await type.NotifyAsync(model, settings); + var services = new List(); + foreach (var param in ctor.GetParameters()) + { + services.Add(_provider.GetService(param.ParameterType)); + } + + var item = Activator.CreateInstance(type, services.ToArray()); + NotificationAgents.Add((INotification)item); + } + } } } } \ No newline at end of file diff --git a/src/Ombi.Schedule/IocJobFactory.cs b/src/Ombi.Schedule/IocJobFactory.cs index 795c1fec5..abb458e39 100644 --- a/src/Ombi.Schedule/IocJobFactory.cs +++ b/src/Ombi.Schedule/IocJobFactory.cs @@ -7,26 +7,18 @@ namespace Ombi.Schedule { public class IoCJobFactory : IJobFactory { - private readonly IServiceProvider _factory; - - public IoCJobFactory(IServiceProvider factory) + private readonly IServiceProvider _serviceProvider; + public IoCJobFactory(IServiceProvider serviceProvider) { - _factory = factory; + _serviceProvider = serviceProvider; } public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) { - var scopeFactory = _factory.GetService(); - var scope = scopeFactory.CreateScope(); - var scopedContainer = scope.ServiceProvider; - - var implementation = scopedContainer.GetRequiredService(bundle.JobDetail.JobType) as IJob; - return implementation; + return _serviceProvider.GetRequiredService(); } public void ReturnJob(IJob job) { - var disposable = job as IDisposable; - disposable?.Dispose(); } } } \ No newline at end of file diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyAvaliabilityChecker.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyAvaliabilityChecker.cs index d713cab80..ade23329d 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyAvaliabilityChecker.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyAvaliabilityChecker.cs @@ -31,6 +31,7 @@ using System.Threading.Tasks; using Hangfire; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; +using Ombi.Core; using Ombi.Core.Notifications; using Ombi.Helpers; using Ombi.Notifications.Models; @@ -45,7 +46,7 @@ namespace Ombi.Schedule.Jobs.Emby public class EmbyAvaliabilityChecker : IEmbyAvaliabilityChecker { public EmbyAvaliabilityChecker(IEmbyContentRepository repo, ITvRequestRepository t, IMovieRequestRepository m, - INotificationService n, ILogger log) + INotificationHelper n, ILogger log) { _repo = repo; _tvRepo = t; @@ -57,7 +58,7 @@ namespace Ombi.Schedule.Jobs.Emby private readonly ITvRequestRepository _tvRepo; private readonly IMovieRequestRepository _movieRepo; private readonly IEmbyContentRepository _repo; - private readonly INotificationService _notificationService; + private readonly INotificationHelper _notificationService; private readonly ILogger _log; public async Task Execute(IJobExecutionContext job) @@ -100,14 +101,14 @@ namespace Ombi.Schedule.Jobs.Emby _log.LogDebug("MovieId: {0}, RequestUser: {1}", movie.Id, recipient); - BackgroundJob.Enqueue(() => _notificationService.Publish(new NotificationOptions + await _notificationService.Notify(new NotificationOptions { DateTime = DateTime.Now, NotificationType = NotificationType.RequestAvailable, RequestId = movie.Id, RequestType = RequestType.Movie, Recipient = recipient, - })); + }); } } await _movieRepo.Save(); @@ -191,14 +192,14 @@ namespace Ombi.Schedule.Jobs.Emby // We have fulfulled this request! child.Available = true; child.MarkedAsAvailable = DateTime.Now; - BackgroundJob.Enqueue(() => _notificationService.Publish(new NotificationOptions + await _notificationService.Notify(new NotificationOptions { DateTime = DateTime.Now, NotificationType = NotificationType.RequestAvailable, RequestId = child.Id, RequestType = RequestType.TvShow, Recipient = child.RequestedUser.Email - })); + }); } } diff --git a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs index 90236aef8..140d53b0a 100644 --- a/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs +++ b/src/Ombi.Schedule/Jobs/Emby/EmbyEpisodeSync.cs @@ -34,6 +34,7 @@ using Microsoft.Extensions.Logging; using Ombi.Api.Emby; using Ombi.Core.Settings; using Ombi.Core.Settings.Models.External; +using Ombi.Helpers; using Ombi.Store.Entities; using Ombi.Store.Repository; using Quartz; diff --git a/src/Ombi.Schedule/Jobs/Lidarr/LidarrAvailabilityChecker.cs b/src/Ombi.Schedule/Jobs/Lidarr/LidarrAvailabilityChecker.cs index 340164bd5..bdc697773 100644 --- a/src/Ombi.Schedule/Jobs/Lidarr/LidarrAvailabilityChecker.cs +++ b/src/Ombi.Schedule/Jobs/Lidarr/LidarrAvailabilityChecker.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Hangfire; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; +using Ombi.Core; using Ombi.Core.Notifications; using Ombi.Helpers; using Ombi.Notifications.Models; @@ -18,7 +19,7 @@ namespace Ombi.Schedule.Jobs.Lidarr public class LidarrAvailabilityChecker : ILidarrAvailabilityChecker { public LidarrAvailabilityChecker(IMusicRequestRepository requests, IRepository albums, ILogger log, - IBackgroundJobClient job, INotificationService notification) + IBackgroundJobClient job, INotificationHelper notification) { _cachedAlbums = albums; _requestRepository = requests; @@ -31,7 +32,7 @@ namespace Ombi.Schedule.Jobs.Lidarr private readonly IRepository _cachedAlbums; private readonly ILogger _logger; private readonly IBackgroundJobClient _job; - private readonly INotificationService _notificationService; + private readonly INotificationHelper _notificationService; public async Task Start() { @@ -59,14 +60,15 @@ namespace Ombi.Schedule.Jobs.Lidarr _logger.LogDebug("AlbumId: {0}, RequestUser: {1}", albumRequest.Id, recipient); - _job.Enqueue(() => _notificationService.Publish(new NotificationOptions + + await _notificationService.Notify(new NotificationOptions { DateTime = DateTime.Now, NotificationType = NotificationType.RequestAvailable, RequestId = albumRequest.Id, RequestType = RequestType.Album, Recipient = recipient, - })); + }); } } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs index a2b1a56cf..78fce3949 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexAvailabilityChecker.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Hangfire; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; +using Ombi.Core; using Ombi.Core.Notifications; using Ombi.Helpers; using Ombi.Notifications.Models; @@ -19,7 +20,7 @@ namespace Ombi.Schedule.Jobs.Plex public class PlexAvailabilityChecker : IPlexAvailabilityChecker { public PlexAvailabilityChecker(IPlexContentRepository repo, ITvRequestRepository tvRequest, IMovieRequestRepository movies, - INotificationService notification, IBackgroundJobClient background, ILogger log) + INotificationHelper notification, IBackgroundJobClient background, ILogger log) { _tvRepo = tvRequest; _repo = repo; @@ -32,7 +33,7 @@ namespace Ombi.Schedule.Jobs.Plex private readonly ITvRequestRepository _tvRepo; private readonly IMovieRequestRepository _movieRepo; private readonly IPlexContentRepository _repo; - private readonly INotificationService _notificationService; + private readonly INotificationHelper _notificationService; private readonly IBackgroundJobClient _backgroundJobClient; private readonly ILogger _log; @@ -126,7 +127,8 @@ namespace Ombi.Schedule.Jobs.Plex // We have ful-fulled this request! child.Available = true; child.MarkedAsAvailable = DateTime.Now; - await _notificationService.Publish(new NotificationOptions + + await _notificationService.Notify(new NotificationOptions { DateTime = DateTime.Now, NotificationType = NotificationType.RequestAvailable, @@ -170,7 +172,7 @@ namespace Ombi.Schedule.Jobs.Plex item.RequestId = movie.Id; _log.LogInformation("[PAC] - Movie request {0} is now available, sending notification", $"{movie.Title} - {movie.Id}"); - await _notificationService.Publish(new NotificationOptions + await _notificationService.Notify(new NotificationOptions { DateTime = DateTime.Now, NotificationType = NotificationType.RequestAvailable, diff --git a/src/Ombi.Schedule/OmbiScheduler.cs b/src/Ombi.Schedule/OmbiScheduler.cs index cfe3bbf27..ac095b354 100644 --- a/src/Ombi.Schedule/OmbiScheduler.cs +++ b/src/Ombi.Schedule/OmbiScheduler.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; +using Ombi.Core.Notifications; using Ombi.Core.Settings; +using Ombi.Helpers; using Ombi.Schedule.Jobs; using Ombi.Schedule.Jobs.Couchpotato; using Ombi.Schedule.Jobs.Emby; @@ -53,6 +55,7 @@ namespace Ombi.Schedule await AddEmby(s); await AddDvrApps(s); await AddSystem(s); + await AddNotifications(s); // Run Quartz await OmbiQuartz.Start(); @@ -93,5 +96,9 @@ namespace Ombi.Schedule await OmbiQuartz.Instance.AddJob(nameof(IEmbyAvaliabilityChecker), "Emby", null); await OmbiQuartz.Instance.AddJob(nameof(IEmbyUserImporter), "Emby", JobSettingsHelper.UserImporter(s)); } + private static async Task AddNotifications(JobSettings s) + { + await OmbiQuartz.Instance.AddJob(nameof(INotificationService), "Notifications", null); + } } } \ No newline at end of file diff --git a/src/Ombi.Schedule/QuartzJobRunner.cs b/src/Ombi.Schedule/QuartzJobRunner.cs new file mode 100644 index 000000000..2a8a39db0 --- /dev/null +++ b/src/Ombi.Schedule/QuartzJobRunner.cs @@ -0,0 +1,27 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Quartz; + +namespace Ombi.Schedule +{ + public class QuartzJobRunner : IJob + { + private readonly IServiceProvider _serviceProvider; + public QuartzJobRunner(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + public async Task Execute(IJobExecutionContext context) + { + using (var scope = _serviceProvider.CreateScope()) + { + var jobType = context.JobDetail.JobType; + var job = scope.ServiceProvider.GetRequiredService(jobType) as IJob; + + await job.Execute(context); + } + } + } +} \ No newline at end of file diff --git a/src/Ombi/Controllers/IssuesController.cs b/src/Ombi/Controllers/IssuesController.cs index 3c8e9c719..72d82fc25 100644 --- a/src/Ombi/Controllers/IssuesController.cs +++ b/src/Ombi/Controllers/IssuesController.cs @@ -27,7 +27,7 @@ namespace Ombi.Controllers public class IssuesController : ControllerBase { public IssuesController(IRepository categories, IRepository issues, IRepository comments, - UserManager userManager, INotificationService notify) + UserManager userManager, INotificationHelper notify) { _categories = categories; _issues = issues; @@ -40,7 +40,7 @@ namespace Ombi.Controllers private readonly IRepository _issues; private readonly IRepository _issueComments; private readonly UserManager _userManager; - private readonly INotificationService _notification; + private readonly INotificationHelper _notification; /// /// Get's all categories @@ -152,7 +152,7 @@ namespace Ombi.Controllers AddIssueNotificationSubstitutes(notificationModel, i, User.Identity.Name); - BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + await _notification.Notify(notificationModel); return i.Id; } @@ -239,7 +239,7 @@ namespace Ombi.Controllers notificationModel.Recipient = user.Email; } - BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + await _notification.Notify(notificationModel); return await _issueComments.Add(newComment); } @@ -292,7 +292,7 @@ namespace Ombi.Controllers }; AddIssueNotificationSubstitutes(notificationModel, issue, issue.UserReported?.UserAlias ?? string.Empty); - BackgroundJob.Enqueue(() => _notification.Publish(notificationModel)); + await _notification.Notify(notificationModel); }