cache injection, error handling and logging on startup, etc

pull/140/head
Drewster727 9 years ago
parent 2ddb949178
commit 7593d3a7e9

@ -121,30 +121,45 @@ namespace PlexRequests.Core
private async void CacheSonarrQualityProfiles(MemoryCacheProvider cacheProvider)
{
var sonarrSettingsService = new SettingsServiceV2<SonarrSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var sonarrSettings = sonarrSettingsService.GetSettings();
if (sonarrSettings.Enabled) {
cacheProvider.GetOrSet(CacheKeys.SonarrQualityProfiles, () =>
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 sonarrSettings = sonarrSettingsService.GetSettings();
if (sonarrSettings.Enabled)
{
Log.Info("Begin executing GetProfiles call to Sonarr for quality profiles");
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)
{
var cpSettingsService = new SettingsServiceV2<CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var cpSettings = cpSettingsService.GetSettings();
if (cpSettings.Enabled)
try
{
cacheProvider.GetOrSet(CacheKeys.CouchPotatoQualityProfiles, () =>
Log.Info("Executing GetSettings call to CouchPotato for quality profiles");
var cpSettingsService = new SettingsServiceV2<CouchPotatoSettings>(new SettingsJsonRepository(new DbConfiguration(new SqliteFactory()), new MemoryCacheProvider()));
var cpSettings = cpSettingsService.GetSettings();
if (cpSettings.Enabled)
{
Log.Info("Begin executing GetProfiles call to CouchPotato for quality profiles");
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="data">The object we want to store.</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>
/// Removes the specified object from the cache.

@ -76,6 +76,7 @@ namespace PlexRequests.UI.Modules
private ICouchPotatoApi CpApi { get; }
private IRepository<LogEntity> LogsRepo { get; }
private INotificationService NotificationService { get; }
private ICacheProvider Cache { get; }
private static Logger Log = LogManager.GetCurrentClassLogger();
public AdminModule(ISettingsService<PlexRequestSettings> prService,
@ -94,7 +95,8 @@ namespace PlexRequests.UI.Modules
IPushoverApi pushoverApi,
IRepository<LogEntity> logsRepo,
INotificationService notify,
ISettingsService<HeadphonesSettings> headphones) : base("admin")
ISettingsService<HeadphonesSettings> headphones,
ICacheProvider cache) : base("admin")
{
PrService = prService;
CpService = cpService;
@ -113,6 +115,7 @@ namespace PlexRequests.UI.Modules
PushoverApi = pushoverApi;
NotificationService = notify;
HeadphonesService = headphones;
Cache = cache;
#if !DEBUG
this.RequiresAuthentication();
@ -377,8 +380,7 @@ namespace PlexRequests.UI.Modules
// set the cache
if (profiles != null)
{
var cache = new MemoryCacheProvider();
cache.Set(CacheKeys.SonarrQualityProfiles, profiles);
Cache.Set(CacheKeys.SonarrQualityProfiles, profiles);
}
return Response.AsJson(profiles);
@ -592,8 +594,7 @@ namespace PlexRequests.UI.Modules
// set the cache
if (profiles != null)
{
var cache = new MemoryCacheProvider();
cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
Cache.Set(CacheKeys.CouchPotatoQualityProfiles, profiles);
}
return Response.AsJson(profiles);

@ -59,7 +59,8 @@ namespace PlexRequests.UI.Modules
ISettingsService<CouchPotatoSettings> cpSettings,
ICouchPotatoApi cpApi,
ISonarrApi sonarrApi,
ISickRageApi sickRageApi) : base("requests")
ISickRageApi sickRageApi,
ICacheProvider cache) : base("requests")
{
Service = service;
PrSettings = prSettings;
@ -71,6 +72,7 @@ namespace PlexRequests.UI.Modules
SonarrApi = sonarrApi;
SickRageApi = sickRageApi;
CpApi = cpApi;
Cache = cache;
Get["/"] = _ => LoadRequests();
Get["/movies"] = _ => GetMovies();
@ -96,6 +98,7 @@ namespace PlexRequests.UI.Modules
private ISonarrApi SonarrApi { get; }
private ISickRageApi SickRageApi { get; }
private ICouchPotatoApi CpApi { get; }
private ICacheProvider Cache { get; }
private Negotiator LoadRequests()
{
@ -126,7 +129,6 @@ namespace PlexRequests.UI.Modules
}));
var mc = new MemoryCacheProvider();
List<QualityModel> qualities = new List<QualityModel>();
if (isAdmin)
@ -136,7 +138,7 @@ namespace PlexRequests.UI.Modules
{
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!
});
@ -202,7 +204,6 @@ namespace PlexRequests.UI.Modules
}
}));
var mc = new MemoryCacheProvider();
List<QualityModel> qualities = new List<QualityModel>();
if (isAdmin)
{
@ -211,7 +212,7 @@ namespace PlexRequests.UI.Modules
{
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!

@ -268,7 +268,7 @@ namespace PlexRequests.UI.Modules
};
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();
@ -422,7 +422,7 @@ namespace PlexRequests.UI.Modules
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 sender = new TvSender(SonarrApi, SickrageApi);
@ -537,8 +537,7 @@ namespace PlexRequests.UI.Modules
};
if (IsAdmin || !settings.RequireMusicApproval ||
settings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase)))
if (ShouldAutoApprove(RequestType.Album, settings))
{
Log.Debug("We don't require approval OR the user is in the whitelist");
var hpSettings = HeadphonesService.GetSettings();
@ -589,5 +588,24 @@ namespace PlexRequests.UI.Modules
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…
Cancel
Save