Save to the request queue !wip

This commit is contained in:
TidusJar 2018-12-03 20:54:01 +00:00
parent b1e6fd313b
commit 207fbe0a8f
4 changed files with 110 additions and 34 deletions

View file

@ -1,4 +1,5 @@
using System.Linq; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -19,7 +20,7 @@ namespace Ombi.Core.Senders
{ {
public MovieSender(ISettingsService<RadarrSettings> radarrSettings, IRadarrApi api, ILogger<MovieSender> log, public MovieSender(ISettingsService<RadarrSettings> radarrSettings, IRadarrApi api, ILogger<MovieSender> log,
ISettingsService<DogNzbSettings> dogSettings, IDogNzbApi dogApi, ISettingsService<CouchPotatoSettings> cpSettings, ISettingsService<DogNzbSettings> dogSettings, IDogNzbApi dogApi, ISettingsService<CouchPotatoSettings> cpSettings,
ICouchPotatoApi cpApi, IRepository<UserQualityProfiles> userProfiles) ICouchPotatoApi cpApi, IRepository<UserQualityProfiles> userProfiles, IRepository<RequestQueue> requestQueue, INotificationHelper notify)
{ {
RadarrSettings = radarrSettings; RadarrSettings = radarrSettings;
RadarrApi = api; RadarrApi = api;
@ -29,6 +30,8 @@ namespace Ombi.Core.Senders
CouchPotatoSettings = cpSettings; CouchPotatoSettings = cpSettings;
CouchPotatoApi = cpApi; CouchPotatoApi = cpApi;
_userProfiles = userProfiles; _userProfiles = userProfiles;
_requestQueuRepository = requestQueue;
_notificationHelper = notify;
} }
private ISettingsService<RadarrSettings> RadarrSettings { get; } private ISettingsService<RadarrSettings> RadarrSettings { get; }
@ -39,39 +42,52 @@ namespace Ombi.Core.Senders
private ISettingsService<CouchPotatoSettings> CouchPotatoSettings { get; } private ISettingsService<CouchPotatoSettings> CouchPotatoSettings { get; }
private ICouchPotatoApi CouchPotatoApi { get; } private ICouchPotatoApi CouchPotatoApi { get; }
private readonly IRepository<UserQualityProfiles> _userProfiles; private readonly IRepository<UserQualityProfiles> _userProfiles;
private readonly IRepository<RequestQueue> _requestQueuRepository;
private readonly INotificationHelper _notificationHelper;
public async Task<SenderResult> Send(MovieRequests model) public async Task<SenderResult> Send(MovieRequests model)
{ {
var cpSettings = await CouchPotatoSettings.GetSettingsAsync(); try
//var watcherSettings = await WatcherSettings.GetSettingsAsync();
var radarrSettings = await RadarrSettings.GetSettingsAsync();
if (radarrSettings.Enabled)
{ {
return await SendToRadarr(model, radarrSettings);
}
var dogSettings = await DogNzbSettings.GetSettingsAsync(); var cpSettings = await CouchPotatoSettings.GetSettingsAsync();
if (dogSettings.Enabled) //var watcherSettings = await WatcherSettings.GetSettingsAsync();
{ var radarrSettings = await RadarrSettings.GetSettingsAsync();
await SendToDogNzb(model, dogSettings); if (radarrSettings.Enabled)
return new SenderResult
{ {
Success = true, return await SendToRadarr(model, radarrSettings);
Sent = true, }
};
}
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 return new SenderResult
{ {
Success = true, Success = true,
@ -93,9 +109,9 @@ namespace Ombi.Core.Senders
private async Task<SenderResult> SendToRadarr(MovieRequests model, RadarrSettings settings) private async Task<SenderResult> SendToRadarr(MovieRequests model, RadarrSettings settings)
{ {
var qualityToUse = int.Parse(settings.DefaultQualityProfile); var qualityToUse = int.Parse(settings.DefaultQualityProfile);
var rootFolderPath = settings.DefaultRootPath; var rootFolderPath = settings.DefaultRootPath;
var profiles = await _userProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == model.RequestedUserId); var profiles = await _userProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == model.RequestedUserId);
@ -150,7 +166,7 @@ namespace Ombi.Core.Senders
// Search for it // Search for it
if (!settings.AddOnly) 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 }; return new SenderResult { Success = true, Sent = true };

View file

@ -1,37 +1,67 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ombi.Api.Lidarr; using Ombi.Api.Lidarr;
using Ombi.Api.Lidarr.Models; using Ombi.Api.Lidarr.Models;
using Ombi.Api.Radarr; using Ombi.Api.Radarr;
using Ombi.Core.Settings; using Ombi.Core.Settings;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Settings.Settings.Models.External; using Ombi.Settings.Settings.Models.External;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests; using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
using Serilog; using Serilog;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace Ombi.Core.Senders namespace Ombi.Core.Senders
{ {
public class MusicSender : IMusicSender public class MusicSender : IMusicSender
{ {
public MusicSender(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi) public MusicSender(ISettingsService<LidarrSettings> lidarr, ILidarrApi lidarrApi, ILogger<MusicSender> log,
IRepository<RequestQueue> requestQueue, INotificationHelper notify)
{ {
_lidarrSettings = lidarr; _lidarrSettings = lidarr;
_lidarrApi = lidarrApi; _lidarrApi = lidarrApi;
_log = log;
_requestQueueRepository = requestQueue;
_notificationHelper = notify;
} }
private readonly ISettingsService<LidarrSettings> _lidarrSettings; private readonly ISettingsService<LidarrSettings> _lidarrSettings;
private readonly ILidarrApi _lidarrApi; private readonly ILidarrApi _lidarrApi;
private readonly ILogger _log;
private readonly IRepository<RequestQueue> _requestQueueRepository;
private readonly INotificationHelper _notificationHelper;
public async Task<SenderResult> Send(AlbumRequest model) public async Task<SenderResult> Send(AlbumRequest model)
{ {
var settings = await _lidarrSettings.GetSettingsAsync(); try
if (settings.Enabled)
{ {
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<SenderResult> SendToLidarr(AlbumRequest model, LidarrSettings settings) private async Task<SenderResult> SendToLidarr(AlbumRequest model, LidarrSettings settings)

View file

@ -23,7 +23,7 @@ namespace Ombi.Core.Senders
{ {
public TvSender(ISonarrApi sonarrApi, ILogger<TvSender> log, ISettingsService<SonarrSettings> sonarrSettings, public TvSender(ISonarrApi sonarrApi, ILogger<TvSender> log, ISettingsService<SonarrSettings> sonarrSettings,
ISettingsService<DogNzbSettings> dog, IDogNzbApi dogApi, ISettingsService<SickRageSettings> srSettings, ISettingsService<DogNzbSettings> dog, IDogNzbApi dogApi, ISettingsService<SickRageSettings> srSettings,
ISickRageApi srApi, IRepository<UserQualityProfiles> userProfiles) ISickRageApi srApi, IRepository<UserQualityProfiles> userProfiles, IRepository<RequestQueue> requestQueue, INotificationHelper notify)
{ {
SonarrApi = sonarrApi; SonarrApi = sonarrApi;
Logger = log; Logger = log;
@ -33,6 +33,8 @@ namespace Ombi.Core.Senders
SickRageSettings = srSettings; SickRageSettings = srSettings;
SickRageApi = srApi; SickRageApi = srApi;
UserQualityProfiles = userProfiles; UserQualityProfiles = userProfiles;
_requestQueueRepository = requestQueue;
_notificationHelper = notify;
} }
private ISonarrApi SonarrApi { get; } private ISonarrApi SonarrApi { get; }
@ -43,9 +45,15 @@ namespace Ombi.Core.Senders
private ISettingsService<DogNzbSettings> DogNzbSettings { get; } private ISettingsService<DogNzbSettings> DogNzbSettings { get; }
private ISettingsService<SickRageSettings> SickRageSettings { get; } private ISettingsService<SickRageSettings> SickRageSettings { get; }
private IRepository<UserQualityProfiles> UserQualityProfiles { get; } private IRepository<UserQualityProfiles> UserQualityProfiles { get; }
private readonly IRepository<RequestQueue> _requestQueueRepository;
private readonly INotificationHelper _notificationHelper;
public async Task<SenderResult> Send(ChildRequests model) public async Task<SenderResult> Send(ChildRequests model)
{ {
try
{
var sonarr = await SonarrSettings.GetSettingsAsync(); var sonarr = await SonarrSettings.GetSettingsAsync();
if (sonarr.Enabled) if (sonarr.Enabled)
{ {
@ -97,6 +105,26 @@ namespace Ombi.Core.Senders
{ {
Success = true 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<DogNzbAddResult> SendToDogNzb(ChildRequests model, DogNzbSettings settings) private async Task<DogNzbAddResult> SendToDogNzb(ChildRequests model, DogNzbSettings settings)

View file

@ -9,6 +9,8 @@ namespace Ombi.Store.Entities
public int RequestId { get; set; } public int RequestId { get; set; }
public RequestType Type { get; set; } public RequestType Type { get; set; }
public DateTime Dts { get; set; } public DateTime Dts { get; set; }
public string Error { get; set; }
public DateTime Completed { get; set; } public DateTime Completed { get; set; }
public int RetryCount { get; set; }
} }
} }