mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-08 14:10:50 -07:00
cache injection, error handling and logging on startup, etc
This commit is contained in:
parent
2ddb949178
commit
7593d3a7e9
6 changed files with 179 additions and 144 deletions
|
@ -121,30 +121,45 @@ namespace PlexRequests.Core
|
||||||
|
|
||||||
private async void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider)
|
private async void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.Info("Executing GetSettings call to Sonarr for quality profiles");
|
||||||
var sonarrSettingsService = new SettingsServiceV2<SonarrSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
|
var sonarrSettingsService = new SettingsServiceV2<SonarrSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
|
||||||
var sonarrSettings = sonarrSettingsService.GetSettings();
|
var sonarrSettings = sonarrSettingsService.GetSettings();
|
||||||
if (sonarrSettings.Enabled) {
|
if (sonarrSettings.Enabled)
|
||||||
cacheProvider.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
|
|
||||||
{
|
{
|
||||||
|
Log.Info("Begin executing GetProfiles call to Sonarr for quality profiles");
|
||||||
SonarrApi sonarrApi = new SonarrApi();
|
SonarrApi sonarrApi = new SonarrApi();
|
||||||
return sonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri);
|
var profiles = sonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri);
|
||||||
|
cacheProvider.Set(CacheKeys.SonarrQualityProfiles, profiles);
|
||||||
});
|
Log.Info("Finished executing GetProfiles call to Sonarr for quality profiles");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error("Failed to cache Sonarr quality profiles!", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void CacheCouchPotatoQualityProfiles(MemoryCacheProvider cacheProvider)
|
private async void CacheCouchPotatoQualityProfiles(MemoryCacheProvider cacheProvider)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.Info("Executing GetSettings call to CouchPotato for quality profiles");
|
||||||
var cpSettingsService = new SettingsServiceV2<CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
|
var cpSettingsService = new SettingsServiceV2<CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
|
||||||
var cpSettings = cpSettingsService.GetSettings();
|
var cpSettings = cpSettingsService.GetSettings();
|
||||||
if (cpSettings.Enabled)
|
if (cpSettings.Enabled)
|
||||||
{
|
{
|
||||||
cacheProvider.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
|
Log.Info("Begin executing GetProfiles call to CouchPotato for quality profiles");
|
||||||
{
|
|
||||||
CouchPotatoApi cpApi = new CouchPotatoApi();
|
CouchPotatoApi cpApi = new CouchPotatoApi();
|
||||||
return cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey);
|
var profiles = cpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey);
|
||||||
|
cacheProvider.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
|
||||||
});
|
Log.Info("Finished executing GetProfiles call to CouchPotato for quality profiles");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Log.Error("Failed to cache CouchPotato quality profiles!", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,7 @@ namespace PlexRequests.Helpers
|
||||||
/// <param name="key">The key.</param>
|
/// <param name="key">The key.</param>
|
||||||
/// <param name="data">The object we want to store.</param>
|
/// <param name="data">The object we want to store.</param>
|
||||||
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
||||||
void Set(string key, object data, int cacheTime);
|
void Set(string key, object data, int cacheTime = 20);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes the specified object from the cache.
|
/// Removes the specified object from the cache.
|
||||||
|
|
|
@ -76,6 +76,7 @@ namespace PlexRequests.UI.Modules
|
||||||
private ICouchPotatoApi CpApi { get; }
|
private ICouchPotatoApi CpApi { get; }
|
||||||
private IRepository<LogEntity> LogsRepo { get; }
|
private IRepository<LogEntity> LogsRepo { get; }
|
||||||
private INotificationService NotificationService { get; }
|
private INotificationService NotificationService { get; }
|
||||||
|
private ICacheProvider Cache { get; }
|
||||||
|
|
||||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||||
public AdminModule(ISettingsService<PlexRequestSettings> prService,
|
public AdminModule(ISettingsService<PlexRequestSettings> prService,
|
||||||
|
@ -94,7 +95,8 @@ namespace PlexRequests.UI.Modules
|
||||||
IPushoverApi pushoverApi,
|
IPushoverApi pushoverApi,
|
||||||
IRepository<LogEntity> logsRepo,
|
IRepository<LogEntity> logsRepo,
|
||||||
INotificationService notify,
|
INotificationService notify,
|
||||||
ISettingsService<HeadphonesSettings> headphones) : base("admin")
|
ISettingsService<HeadphonesSettings> headphones,
|
||||||
|
ICacheProvider cache) : base("admin")
|
||||||
{
|
{
|
||||||
PrService = prService;
|
PrService = prService;
|
||||||
CpService = cpService;
|
CpService = cpService;
|
||||||
|
@ -113,6 +115,7 @@ namespace PlexRequests.UI.Modules
|
||||||
PushoverApi = pushoverApi;
|
PushoverApi = pushoverApi;
|
||||||
NotificationService = notify;
|
NotificationService = notify;
|
||||||
HeadphonesService = headphones;
|
HeadphonesService = headphones;
|
||||||
|
Cache = cache;
|
||||||
|
|
||||||
#if !DEBUG
|
#if !DEBUG
|
||||||
this.RequiresAuthentication();
|
this.RequiresAuthentication();
|
||||||
|
@ -377,8 +380,7 @@ namespace PlexRequests.UI.Modules
|
||||||
// set the cache
|
// set the cache
|
||||||
if (profiles != null)
|
if (profiles != null)
|
||||||
{
|
{
|
||||||
var cache = new MemoryCacheProvider();
|
Cache.Set(CacheKeys.SonarrQualityProfiles, profiles);
|
||||||
cache.Set(CacheKeys.SonarrQualityProfiles, profiles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.AsJson(profiles);
|
return Response.AsJson(profiles);
|
||||||
|
@ -592,8 +594,7 @@ namespace PlexRequests.UI.Modules
|
||||||
// set the cache
|
// set the cache
|
||||||
if (profiles != null)
|
if (profiles != null)
|
||||||
{
|
{
|
||||||
var cache = new MemoryCacheProvider();
|
Cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
|
||||||
cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Response.AsJson(profiles);
|
return Response.AsJson(profiles);
|
||||||
|
|
|
@ -59,7 +59,8 @@ namespace PlexRequests.UI.Modules
|
||||||
ISettingsService<CouchPotatoSettings> cpSettings,
|
ISettingsService<CouchPotatoSettings> cpSettings,
|
||||||
ICouchPotatoApi cpApi,
|
ICouchPotatoApi cpApi,
|
||||||
ISonarrApi sonarrApi,
|
ISonarrApi sonarrApi,
|
||||||
ISickRageApi sickRageApi) : base("requests")
|
ISickRageApi sickRageApi,
|
||||||
|
ICacheProvider cache) : base("requests")
|
||||||
{
|
{
|
||||||
Service = service;
|
Service = service;
|
||||||
PrSettings = prSettings;
|
PrSettings = prSettings;
|
||||||
|
@ -71,6 +72,7 @@ namespace PlexRequests.UI.Modules
|
||||||
SonarrApi = sonarrApi;
|
SonarrApi = sonarrApi;
|
||||||
SickRageApi = sickRageApi;
|
SickRageApi = sickRageApi;
|
||||||
CpApi = cpApi;
|
CpApi = cpApi;
|
||||||
|
Cache = cache;
|
||||||
|
|
||||||
Get["/"] = _ => LoadRequests();
|
Get["/"] = _ => LoadRequests();
|
||||||
Get["/movies"] = _ => GetMovies();
|
Get["/movies"] = _ => GetMovies();
|
||||||
|
@ -96,6 +98,7 @@ namespace PlexRequests.UI.Modules
|
||||||
private ISonarrApi SonarrApi { get; }
|
private ISonarrApi SonarrApi { get; }
|
||||||
private ISickRageApi SickRageApi { get; }
|
private ISickRageApi SickRageApi { get; }
|
||||||
private ICouchPotatoApi CpApi { get; }
|
private ICouchPotatoApi CpApi { get; }
|
||||||
|
private ICacheProvider Cache { get; }
|
||||||
|
|
||||||
private Negotiator LoadRequests()
|
private Negotiator LoadRequests()
|
||||||
{
|
{
|
||||||
|
@ -126,7 +129,6 @@ namespace PlexRequests.UI.Modules
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
var mc = new MemoryCacheProvider();
|
|
||||||
List<QualityModel> qualities = new List<QualityModel>();
|
List<QualityModel> qualities = new List<QualityModel>();
|
||||||
|
|
||||||
if (isAdmin)
|
if (isAdmin)
|
||||||
|
@ -136,7 +138,7 @@ namespace PlexRequests.UI.Modules
|
||||||
{
|
{
|
||||||
taskList.Add(Task.Factory.StartNew(() =>
|
taskList.Add(Task.Factory.StartNew(() =>
|
||||||
{
|
{
|
||||||
return mc.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
|
return Cache.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
|
||||||
{
|
{
|
||||||
return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); // TODO: cache this!
|
return CpApi.GetProfiles(cpSettings.FullUri, cpSettings.ApiKey); // TODO: cache this!
|
||||||
});
|
});
|
||||||
|
@ -202,7 +204,6 @@ namespace PlexRequests.UI.Modules
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var mc = new MemoryCacheProvider();
|
|
||||||
List<QualityModel> qualities = new List<QualityModel>();
|
List<QualityModel> qualities = new List<QualityModel>();
|
||||||
if (isAdmin)
|
if (isAdmin)
|
||||||
{
|
{
|
||||||
|
@ -211,7 +212,7 @@ namespace PlexRequests.UI.Modules
|
||||||
{
|
{
|
||||||
taskList.Add(Task.Factory.StartNew(() =>
|
taskList.Add(Task.Factory.StartNew(() =>
|
||||||
{
|
{
|
||||||
return mc.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
|
return Cache.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
|
||||||
{
|
{
|
||||||
return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); // TODO: cache this!
|
return SonarrApi.GetProfiles(sonarrSettings.ApiKey, sonarrSettings.FullUri); // TODO: cache this!
|
||||||
|
|
||||||
|
|
|
@ -268,7 +268,7 @@ namespace PlexRequests.UI.Modules
|
||||||
};
|
};
|
||||||
|
|
||||||
Log.Trace(settings.DumpJson());
|
Log.Trace(settings.DumpJson());
|
||||||
if (IsAdmin || !settings.RequireMovieApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase)))
|
if (ShouldAutoApprove(RequestType.Movie, settings))
|
||||||
{
|
{
|
||||||
var cpSettings = CpService.GetSettings();
|
var cpSettings = CpService.GetSettings();
|
||||||
|
|
||||||
|
@ -422,7 +422,7 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
model.SeasonList = seasonsList.ToArray();
|
model.SeasonList = seasonsList.ToArray();
|
||||||
|
|
||||||
if (IsAdmin || !settings.RequireTvShowApproval || settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase)))
|
if (ShouldAutoApprove(RequestType.TvShow, settings))
|
||||||
{
|
{
|
||||||
var sonarrSettings = SonarrService.GetSettings();
|
var sonarrSettings = SonarrService.GetSettings();
|
||||||
var sender = new TvSender(SonarrApi, SickrageApi);
|
var sender = new TvSender(SonarrApi, SickrageApi);
|
||||||
|
@ -537,8 +537,7 @@ namespace PlexRequests.UI.Modules
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
if (IsAdmin || !settings.RequireMusicApproval ||
|
if (ShouldAutoApprove(RequestType.Album, settings))
|
||||||
settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase)))
|
|
||||||
{
|
{
|
||||||
Log.Debug("We don't require approval OR the user is in the whitelist");
|
Log.Debug("We don't require approval OR the user is in the whitelist");
|
||||||
var hpSettings = HeadphonesService.GetSettings();
|
var hpSettings = HeadphonesService.GetSettings();
|
||||||
|
@ -589,5 +588,24 @@ namespace PlexRequests.UI.Modules
|
||||||
|
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings)
|
||||||
|
{
|
||||||
|
// if the user is an admin or they are whitelisted, they go ahead and allow auto-approval
|
||||||
|
if (IsAdmin || prSettings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) return true;
|
||||||
|
|
||||||
|
// check by request type if the category requires approval or not
|
||||||
|
switch (requestType)
|
||||||
|
{
|
||||||
|
case RequestType.Movie:
|
||||||
|
return !prSettings.RequireMovieApproval;
|
||||||
|
case RequestType.TvShow:
|
||||||
|
return !prSettings.RequireTvShowApproval;
|
||||||
|
case RequestType.Album:
|
||||||
|
return !prSettings.RequireMusicApproval;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue