From 34e74c74c1e72b0688ce794a3c7717f72088b7ff Mon Sep 17 00:00:00 2001 From: tidusjar Date: Fri, 27 May 2016 13:50:27 +0100 Subject: [PATCH] Fixed #277 --- PlexRequests.Core/Setup.cs | 22 +++- PlexRequests.Services/Jobs/JobNames.cs | 3 +- PlexRequests.Services/Jobs/StoreBackup.cs | 117 +++++++++--------- PlexRequests.Services/Jobs/StoreCleanup.cs | 82 ++++++++++++ .../PlexRequests.Services.csproj | 1 + PlexRequests.UI/Jobs/Scheduler.cs | 14 ++- 6 files changed, 176 insertions(+), 63 deletions(-) create mode 100644 PlexRequests.Services/Jobs/StoreCleanup.cs diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 740bc3df1..4c6128259 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -53,7 +53,7 @@ namespace PlexRequests.Core { CreateDefaultSettingsPage(urlBase); } - + var version = CheckSchema(); if (version > 0) { @@ -77,7 +77,7 @@ namespace PlexRequests.Core { var productVersion = AssemblyHelper.GetProductVersion(); var trimStatus = new Regex("[^0-9]", RegexOptions.Compiled).Replace(productVersion, string.Empty).PadRight(4, '0'); - var version = int.Parse(trimStatus); + var version = int.Parse(trimStatus); var connection = Db.DbConnection(); var schema = connection.GetSchemaVersion(); @@ -181,6 +181,7 @@ namespace PlexRequests.Core /// /// Migrates to version 1.8. /// This includes updating the admin account to have all roles. + /// Set the log level to info /// private void MigrateToVersion1800() { @@ -203,6 +204,23 @@ namespace PlexRequests.Core throw; } + try + { + var settingsService = new SettingsServiceV2(new SettingsJsonRepository(Db, new MemoryCacheProvider())); + var logSettings = settingsService.GetSettings(); + logSettings.Level = LogLevel.Info.Ordinal; + settingsService.SaveSettings(logSettings); + + LoggingHelper.ReconfigureLogLevel(LogLevel.FromOrdinal(logSettings.Level)); + + } + catch (Exception e) + { + Log.Error(e); + throw; + } + + } } } diff --git a/PlexRequests.Services/Jobs/JobNames.cs b/PlexRequests.Services/Jobs/JobNames.cs index d996ac01e..ff6791d92 100644 --- a/PlexRequests.Services/Jobs/JobNames.cs +++ b/PlexRequests.Services/Jobs/JobNames.cs @@ -24,7 +24,7 @@ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ************************************************************************/ #endregion -namespace PlexRequests.Services +namespace PlexRequests.Services.Jobs { public static class JobNames { @@ -33,5 +33,6 @@ namespace PlexRequests.Services public const string SonarrCacher = "Sonarr Cacher"; public const string SrCacher = "SickRage Cacher"; public const string PlexChecker = "Plex Availability Cacher"; + public const string StoreCleanup = "Database Cleanup"; } } \ No newline at end of file diff --git a/PlexRequests.Services/Jobs/StoreBackup.cs b/PlexRequests.Services/Jobs/StoreBackup.cs index 1009e0daf..e36f73eb4 100644 --- a/PlexRequests.Services/Jobs/StoreBackup.cs +++ b/PlexRequests.Services/Jobs/StoreBackup.cs @@ -26,15 +26,11 @@ #endregion using System; using System.IO; -using System.Linq; -using System.Globalization; using NLog; using PlexRequests.Services.Interfaces; using PlexRequests.Store; -using PlexRequests.Store.Models; -using PlexRequests.Store.Repository; using Quartz; @@ -57,7 +53,7 @@ namespace PlexRequests.Services.Jobs public void Execute(IJobExecutionContext context) { TakeBackup(); - Cleanup (); + Cleanup(); } private void TakeBackup() @@ -81,11 +77,11 @@ namespace PlexRequests.Services.Jobs try { - if(DoWeNeedToBackup(backupDir.FullName)) - { - File.Copy(dbPath, Path.Combine(backupDir.FullName, $"PlexRequests.sqlite_{DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}.bak")); - } - } + if (DoWeNeedToBackup(backupDir.FullName)) + { + File.Copy(dbPath, Path.Combine(backupDir.FullName, $"PlexRequests.sqlite_{DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}.bak")); + } + } catch (Exception e) { Log.Warn(e); @@ -98,53 +94,58 @@ namespace PlexRequests.Services.Jobs } - private void Cleanup() - { - Log.Trace("Starting DB Cleanup"); - var dbPath = Sql.CurrentPath; - var dir = Path.GetDirectoryName(dbPath); - if (dir == null) - { - Log.Warn("We couldn't find the DB path. We cannot backup."); - return; - } - var backupDir = Directory.CreateDirectory(Path.Combine(dir, "Backup")); - - var files = backupDir.GetFiles(); - - foreach (var file in files) { - var dt = ParseName(file.Name); - if(dt < DateTime.Now.AddDays(-7)){ - try { - - File.Delete(file.FullName); - } catch (Exception ex) { - Log.Error(ex); - } - } - } - - } - - private bool DoWeNeedToBackup(string backupPath) - { - var files = Directory.GetFiles(backupPath); - //TODO Get the latest file and if it's within an hour of DateTime.Now then don't bother backing up. - return true; - } - - private DateTime ParseName(string fileName) - { - var names = fileName.Split(new []{'_','.',' '}, StringSplitOptions.RemoveEmptyEntries); - if(names.Count() > 1) - { - DateTime parsed; - //DateTime.TryParseExcat(names[1], "yyyy-MM-dd hh.mm.ss",CultureInfo.CurrentUICulture, DateTimeStyles.None, out parsed); - DateTime.TryParse(names[2], out parsed); - return parsed; - - } - return DateTime.MinValue; - } + private void Cleanup() + { + Log.Trace("Starting DB Cleanup"); + var dbPath = Sql.CurrentPath; + var dir = Path.GetDirectoryName(dbPath); + if (dir == null) + { + Log.Warn("We couldn't find the DB path. We cannot backup."); + return; + } + var backupDir = Directory.CreateDirectory(Path.Combine(dir, "Backup")); + + var files = backupDir.GetFiles(); + + foreach (var file in files) + { + var dt = ParseName(file.Name); + if (dt < DateTime.Now.AddDays(-7)) + { + try + { + + File.Delete(file.FullName); + } + catch (Exception ex) + { + Log.Error(ex); + } + } + } + + } + + private bool DoWeNeedToBackup(string backupPath) + { + var files = Directory.GetFiles(backupPath); + //TODO Get the latest file and if it's within an hour of DateTime.Now then don't bother backing up. + return true; + } + + private DateTime ParseName(string fileName) + { + var names = fileName.Split(new[] { '_', '.', ' ' }, StringSplitOptions.RemoveEmptyEntries); + if (names.Length > 1) + { + DateTime parsed; + //DateTime.TryParseExcat(names[1], "yyyy-MM-dd hh.mm.ss",CultureInfo.CurrentUICulture, DateTimeStyles.None, out parsed); + DateTime.TryParse(names[2], out parsed); + return parsed; + + } + return DateTime.MinValue; + } } } \ No newline at end of file diff --git a/PlexRequests.Services/Jobs/StoreCleanup.cs b/PlexRequests.Services/Jobs/StoreCleanup.cs new file mode 100644 index 000000000..a394211f5 --- /dev/null +++ b/PlexRequests.Services/Jobs/StoreCleanup.cs @@ -0,0 +1,82 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: StoreCleanup.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +using System; +using System.Linq; + +using NLog; + +using PlexRequests.Services.Interfaces; +using PlexRequests.Store.Models; +using PlexRequests.Store.Repository; + +using Quartz; + +namespace PlexRequests.Services.Jobs +{ + public class StoreCleanup : IJob + { + private static readonly Logger Log = LogManager.GetCurrentClassLogger(); + + public StoreCleanup(IRepository repo, IJobRecord rec) + { + Repo = repo; + JobRecord = rec; + } + + private IJobRecord JobRecord { get; } + + private IRepository Repo { get; } + + private void Cleanup() + { + try + { + var items = Repo.GetAll(); + var orderedItems = items.Where(x => x.Date < DateTime.Now.AddDays(-7)); + + foreach (var o in orderedItems) + { + Repo.Delete(o); + } + } + catch (Exception e) + { + Log.Error(e); + } + finally + { + JobRecord.Record(JobNames.StoreCleanup); + } + + } + + public void Execute(IJobExecutionContext context) + { + Cleanup(); + } + } +} \ No newline at end of file diff --git a/PlexRequests.Services/PlexRequests.Services.csproj b/PlexRequests.Services/PlexRequests.Services.csproj index 257fc5b7e..caafe8d77 100644 --- a/PlexRequests.Services/PlexRequests.Services.csproj +++ b/PlexRequests.Services/PlexRequests.Services.csproj @@ -73,6 +73,7 @@ + diff --git a/PlexRequests.UI/Jobs/Scheduler.cs b/PlexRequests.UI/Jobs/Scheduler.cs index 0ccae710b..63ffe8b64 100644 --- a/PlexRequests.UI/Jobs/Scheduler.cs +++ b/PlexRequests.UI/Jobs/Scheduler.cs @@ -56,13 +56,15 @@ namespace PlexRequests.UI.Jobs var sickrage = JobBuilder.Create().WithIdentity("SickRageCacher", "Cache").Build(); var sonarr = JobBuilder.Create().WithIdentity("SonarrCacher", "Cache").Build(); var cp = JobBuilder.Create().WithIdentity("CouchPotatoCacher", "Cache").Build(); - var store = JobBuilder.Create().WithIdentity("StoreBackup", "Backup").Build(); + var store = JobBuilder.Create().WithIdentity("StoreBackup", "Database").Build(); + var storeClean = JobBuilder.Create().WithIdentity("StoreCleanup", "Database").Build(); jobs.Add(plex); jobs.Add(sickrage); jobs.Add(sonarr); jobs.Add(cp); jobs.Add(store); + jobs.Add(storeClean); return jobs; } @@ -129,17 +131,25 @@ namespace PlexRequests.UI.Jobs var storeBackup = TriggerBuilder.Create() - .WithIdentity("StoreBackup", "Backup") + .WithIdentity("StoreBackup", "Database") .StartNow() .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever()) .Build(); + var storeCleanup = + TriggerBuilder.Create() + .WithIdentity("StoreCleanup", "Database") + .StartNow() + .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever()) + .Build(); + triggers.Add(plexAvailabilityChecker); triggers.Add(srCacher); triggers.Add(sonarrCacher); triggers.Add(cpCacher); triggers.Add(storeBackup); + triggers.Add(storeCleanup); return triggers; }