From d19deea56c10f262ea90e13e6f8e30baf18310ac Mon Sep 17 00:00:00 2001 From: tidusjar Date: Mon, 8 Aug 2016 13:38:26 +0100 Subject: [PATCH] Added the transaction back into the DB. Do not run the episode cacher if it's been run in the last hour --- .../SettingModels/ScheduledJobsSettings.cs | 2 +- .../Jobs/PlexEpisodeCacher.cs | 13 +++++++++++- .../Repository/BaseGenericRepository.cs | 21 ++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/PlexRequests.Core/SettingModels/ScheduledJobsSettings.cs b/PlexRequests.Core/SettingModels/ScheduledJobsSettings.cs index 4feefc3cc..2facac40c 100644 --- a/PlexRequests.Core/SettingModels/ScheduledJobsSettings.cs +++ b/PlexRequests.Core/SettingModels/ScheduledJobsSettings.cs @@ -37,7 +37,7 @@ namespace PlexRequests.Core.SettingModels StoreBackup = 24; StoreCleanup = 24; UserRequestLimitResetter = 12; - PlexEpisodeCacher = 2; + PlexEpisodeCacher = 12; } public int PlexAvailabilityChecker { get; set; } diff --git a/PlexRequests.Services/Jobs/PlexEpisodeCacher.cs b/PlexRequests.Services/Jobs/PlexEpisodeCacher.cs index 263d8944e..1ccafea09 100644 --- a/PlexRequests.Services/Jobs/PlexEpisodeCacher.cs +++ b/PlexRequests.Services/Jobs/PlexEpisodeCacher.cs @@ -49,13 +49,14 @@ namespace PlexRequests.Services.Jobs public class PlexEpisodeCacher : IJob { public PlexEpisodeCacher(ISettingsService plexSettings, IPlexApi plex, ICacheProvider cache, - IJobRecord rec, IRepository repo) + IJobRecord rec, IRepository repo, ISettingsService jobs) { Plex = plexSettings; PlexApi = plex; Cache = cache; Job = rec; Repo = repo; + Jobs = jobs; } private ISettingsService Plex { get; } @@ -64,6 +65,7 @@ namespace PlexRequests.Services.Jobs private ICacheProvider Cache { get; } private IJobRecord Job { get; } private IRepository Repo { get; } + private ISettingsService Jobs { get; } private const int ResultCount = 25; private const string PlexType = "episode"; private const string TableName = "PlexEpisodes"; @@ -142,6 +144,15 @@ namespace PlexRequests.Services.Jobs { try { + var jobs = Job.GetJobs(); + var job = jobs.FirstOrDefault(x => x.Name.Equals(JobNames.EpisodeCacher, StringComparison.CurrentCultureIgnoreCase)); + if (job != null) + { + if (job.LastRun > DateTime.Now.AddHours(-1)) // If it's been run in the last hour + { + return; + } + } CacheEpisodes(); } catch (Exception e) diff --git a/PlexRequests.Store/Repository/BaseGenericRepository.cs b/PlexRequests.Store/Repository/BaseGenericRepository.cs index 647ce8f8a..f29c2dc95 100644 --- a/PlexRequests.Store/Repository/BaseGenericRepository.cs +++ b/PlexRequests.Store/Repository/BaseGenericRepository.cs @@ -290,26 +290,17 @@ namespace PlexRequests.Store.Repository using (var db = Config.DbConnection()) { db.Open(); - //using (var tran = db.BeginTransaction()) - //{ - Log.Trace("Starting Transaction"); + using (var tran = db.BeginTransaction()) + { var result = enumerable.Sum(e => db.Insert(e)); - var done = result == enumerable.Length; - - Log.Trace($"Total Result: {done}"); - Log.Trace($"db result: {result}"); - Log.Trace($"totalitems result: {enumerable.Length}"); - if (done) + if (result != 0) { - Log.Trace("Commiting"); - //tran.Commit(); + tran.Commit(); return true; } - Log.Trace("Rolling back the tran"); - - //tran.Rollback(); + tran.Rollback(); return false; - //} + } } }