diff --git a/src/Ombi.Core/Senders/MovieSender.cs b/src/Ombi.Core/Senders/MovieSender.cs index 8b89ef7bb..844caced3 100644 --- a/src/Ombi.Core/Senders/MovieSender.cs +++ b/src/Ombi.Core/Senders/MovieSender.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -19,7 +20,7 @@ namespace Ombi.Core.Senders { public MovieSender(ISettingsService radarrSettings, IRadarrApi api, ILogger log, ISettingsService dogSettings, IDogNzbApi dogApi, ISettingsService cpSettings, - ICouchPotatoApi cpApi, IRepository userProfiles) + ICouchPotatoApi cpApi, IRepository userProfiles, IRepository requestQueue, INotificationHelper notify) { RadarrSettings = radarrSettings; RadarrApi = api; @@ -29,6 +30,8 @@ namespace Ombi.Core.Senders CouchPotatoSettings = cpSettings; CouchPotatoApi = cpApi; _userProfiles = userProfiles; + _requestQueuRepository = requestQueue; + _notificationHelper = notify; } private ISettingsService RadarrSettings { get; } @@ -39,39 +42,52 @@ namespace Ombi.Core.Senders private ISettingsService CouchPotatoSettings { get; } private ICouchPotatoApi CouchPotatoApi { get; } private readonly IRepository _userProfiles; + private readonly IRepository _requestQueuRepository; + private readonly INotificationHelper _notificationHelper; public async Task Send(MovieRequests model) { - var cpSettings = await CouchPotatoSettings.GetSettingsAsync(); - //var watcherSettings = await WatcherSettings.GetSettingsAsync(); - var radarrSettings = await RadarrSettings.GetSettingsAsync(); - if (radarrSettings.Enabled) + try { - return await SendToRadarr(model, radarrSettings); - } - var dogSettings = await DogNzbSettings.GetSettingsAsync(); - if (dogSettings.Enabled) - { - await SendToDogNzb(model, dogSettings); - return new SenderResult + var cpSettings = await CouchPotatoSettings.GetSettingsAsync(); + //var watcherSettings = await WatcherSettings.GetSettingsAsync(); + var radarrSettings = await RadarrSettings.GetSettingsAsync(); + if (radarrSettings.Enabled) { - Success = true, - Sent = true, - }; - } + return await SendToRadarr(model, radarrSettings); + } - if (cpSettings.Enabled) + var dogSettings = await DogNzbSettings.GetSettingsAsync(); + if (dogSettings.Enabled) + { + await SendToDogNzb(model, dogSettings); + return new SenderResult + { + Success = true, + Sent = true, + }; + } + + if (cpSettings.Enabled) + { + return await SendToCp(model, cpSettings, cpSettings.DefaultProfileId); + } + } + catch (Exception e) { - return await SendToCp(model, cpSettings, cpSettings.DefaultProfileId); + Log.LogError(e, "Error when seing movie to DVR app, added to the request queue"S); + await _requestQueuRepository.Add(new RequestQueue + { + Dts = DateTime.UtcNow, + Error = e.Message, + RequestId = model.Id, + Type = RequestType.Movie, + RetryCount = 0 + }); + _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); } - //if (watcherSettings.Enabled) - //{ - // return SendToWatcher(model, watcherSettings); - //} - - return new SenderResult { Success = true, @@ -93,9 +109,9 @@ namespace Ombi.Core.Senders private async Task SendToRadarr(MovieRequests model, RadarrSettings settings) { - + var qualityToUse = int.Parse(settings.DefaultQualityProfile); - + var rootFolderPath = settings.DefaultRootPath; var profiles = await _userProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == model.RequestedUserId); @@ -150,7 +166,7 @@ namespace Ombi.Core.Senders // Search for it if (!settings.AddOnly) { - await RadarrApi.MovieSearch(new[] {existingMovie.id}, settings.ApiKey, settings.FullUri); + await RadarrApi.MovieSearch(new[] { existingMovie.id }, settings.ApiKey, settings.FullUri); } return new SenderResult { Success = true, Sent = true }; diff --git a/src/Ombi.Core/Senders/MusicSender.cs b/src/Ombi.Core/Senders/MusicSender.cs index 60e4ca6ee..235ea3db8 100644 --- a/src/Ombi.Core/Senders/MusicSender.cs +++ b/src/Ombi.Core/Senders/MusicSender.cs @@ -1,37 +1,67 @@ using System; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Ombi.Api.Lidarr; using Ombi.Api.Lidarr.Models; using Ombi.Api.Radarr; using Ombi.Core.Settings; using Ombi.Helpers; using Ombi.Settings.Settings.Models.External; +using Ombi.Store.Entities; using Ombi.Store.Entities.Requests; +using Ombi.Store.Repository; using Serilog; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace Ombi.Core.Senders { public class MusicSender : IMusicSender { - public MusicSender(ISettingsService lidarr, ILidarrApi lidarrApi) + public MusicSender(ISettingsService lidarr, ILidarrApi lidarrApi, ILogger log, + IRepository requestQueue, INotificationHelper notify) { _lidarrSettings = lidarr; _lidarrApi = lidarrApi; + _log = log; + _requestQueueRepository = requestQueue; + _notificationHelper = notify; } private readonly ISettingsService _lidarrSettings; private readonly ILidarrApi _lidarrApi; + private readonly ILogger _log; + private readonly IRepository _requestQueueRepository; + private readonly INotificationHelper _notificationHelper; public async Task Send(AlbumRequest model) { - var settings = await _lidarrSettings.GetSettingsAsync(); - if (settings.Enabled) + try { - return await SendToLidarr(model, settings); + var settings = await _lidarrSettings.GetSettingsAsync(); + if (settings.Enabled) + { + return await SendToLidarr(model, settings); + } + + return new SenderResult { Success = false, Sent = false, Message = "Lidarr is not enabled" }; } + catch (Exception e) + { + _log.LogError(e, "Exception thrown when sending a music to DVR app, added to the request queue"); + await _requestQueueRepository.Add(new RequestQueue + { + Dts = DateTime.UtcNow, + Error = e.Message, + RequestId = model.Id, + Type = RequestType.Album, + RetryCount = 0 + }); + _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); + } + - return new SenderResult { Success = false, Sent = false, Message = "Lidarr is not enabled" }; + return new SenderResult { Success = false, Sent = false, Message = "Something went wrong!" }; } private async Task SendToLidarr(AlbumRequest model, LidarrSettings settings) diff --git a/src/Ombi.Core/Senders/TvSender.cs b/src/Ombi.Core/Senders/TvSender.cs index e48e54c1a..c590e52ca 100644 --- a/src/Ombi.Core/Senders/TvSender.cs +++ b/src/Ombi.Core/Senders/TvSender.cs @@ -23,7 +23,7 @@ namespace Ombi.Core.Senders { public TvSender(ISonarrApi sonarrApi, ILogger log, ISettingsService sonarrSettings, ISettingsService dog, IDogNzbApi dogApi, ISettingsService srSettings, - ISickRageApi srApi, IRepository userProfiles) + ISickRageApi srApi, IRepository userProfiles, IRepository requestQueue, INotificationHelper notify) { SonarrApi = sonarrApi; Logger = log; @@ -33,6 +33,8 @@ namespace Ombi.Core.Senders SickRageSettings = srSettings; SickRageApi = srApi; UserQualityProfiles = userProfiles; + _requestQueueRepository = requestQueue; + _notificationHelper = notify; } private ISonarrApi SonarrApi { get; } @@ -43,9 +45,15 @@ namespace Ombi.Core.Senders private ISettingsService DogNzbSettings { get; } private ISettingsService SickRageSettings { get; } private IRepository UserQualityProfiles { get; } + private readonly IRepository _requestQueueRepository; + private readonly INotificationHelper _notificationHelper; public async Task Send(ChildRequests model) { + try + { + + var sonarr = await SonarrSettings.GetSettingsAsync(); if (sonarr.Enabled) { @@ -97,6 +105,26 @@ namespace Ombi.Core.Senders { Success = true }; + } + catch (Exception e) + { + Logger.LogError(e, "Exception thrown when sending a movie to DVR app, added to the request queue"); + await _requestQueueRepository.Add(new RequestQueue + { + Dts = DateTime.UtcNow, + Error = e.Message, + RequestId = model.Id, + Type = RequestType.TvShow, + RetryCount = 0 + }); + _notificationHelper.Notify(model, NotificationType.ItemAddedToFaultQueue); + } + + return new SenderResult + { + Success = false, + Message = "Something wen't wrong!" + }; } private async Task SendToDogNzb(ChildRequests model, DogNzbSettings settings) diff --git a/src/Ombi.Store/Entities/RequestQueue.cs b/src/Ombi.Store/Entities/RequestQueue.cs index 980bfca31..e561f9ec7 100644 --- a/src/Ombi.Store/Entities/RequestQueue.cs +++ b/src/Ombi.Store/Entities/RequestQueue.cs @@ -9,6 +9,8 @@ namespace Ombi.Store.Entities public int RequestId { get; set; } public RequestType Type { get; set; } public DateTime Dts { get; set; } + public string Error { get; set; } public DateTime Completed { get; set; } + public int RetryCount { get; set; } } } \ No newline at end of file