diff --git a/NzbDrone.Core.Test/ProviderTests/LogProviderTest.cs b/NzbDrone.Core.Test/ProviderTests/LogProviderTest.cs index fc054dbb1..fa14d16d7 100644 --- a/NzbDrone.Core.Test/ProviderTests/LogProviderTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/LogProviderTest.cs @@ -222,5 +222,28 @@ public void pagedLogs() logs.Items.Should().HaveCount(50); logs.TotalItems.Should().Be(100); } + + [Test] + public void Trim_Logs_should_clear_logs_older_than_30_days() + { + //Setup + var historyItem = Builder.CreateListOfSize(20) + .TheFirst(10).With(c => c.Time = DateTime.Now) + .TheNext(10).With(c => c.Time = DateTime.Now.AddDays(-31)) + .Build(); + + var mocker = new AutoMoqer(); + var db = MockLib.GetEmptyDatabase(); + mocker.SetConstant(db); + + db.InsertMany(historyItem); + + //Act + db.Fetch().Should().HaveCount(20); + mocker.Resolve().Trim(); + + //Assert + db.Fetch().Should().HaveCount(10); + } } } diff --git a/NzbDrone.Core/CentralDispatch.cs b/NzbDrone.Core/CentralDispatch.cs index 1e0fd30cb..121a300e2 100644 --- a/NzbDrone.Core/CentralDispatch.cs +++ b/NzbDrone.Core/CentralDispatch.cs @@ -93,6 +93,7 @@ private void InitJobs() Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); Kernel.Bind().To().InSingletonScope(); + Kernel.Bind().To().InSingletonScope(); Kernel.Get().Initialize(); Kernel.Get().StartTimer(30); diff --git a/NzbDrone.Core/Instrumentation/LogProvider.cs b/NzbDrone.Core/Instrumentation/LogProvider.cs index 07fdcab98..5245a3ce8 100644 --- a/NzbDrone.Core/Instrumentation/LogProvider.cs +++ b/NzbDrone.Core/Instrumentation/LogProvider.cs @@ -46,5 +46,11 @@ public void DeleteAll() _database.Delete(""); Logger.Info("Cleared Log History"); } + + public void Trim() + { + _database.Delete("WHERE Time < @0", DateTime.Now.AddDays(-30).Date); + Logger.Info("Logs have been trimmed, events older than 30 days have been removed"); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Providers/Jobs/TrimLogsJob.cs b/NzbDrone.Core/Providers/Jobs/TrimLogsJob.cs new file mode 100644 index 000000000..8dedcbcc9 --- /dev/null +++ b/NzbDrone.Core/Providers/Jobs/TrimLogsJob.cs @@ -0,0 +1,35 @@ +using System.Diagnostics; +using System.IO; +using NLog; +using NzbDrone.Common; +using NzbDrone.Core.Instrumentation; +using NzbDrone.Core.Model.Notification; +using NzbDrone.Core.Providers.Core; + +namespace NzbDrone.Core.Providers.Jobs +{ + public class TrimLogsJob : IJob + { + private readonly LogProvider _logProvider; + + public TrimLogsJob(LogProvider logProvider) + { + _logProvider = logProvider; + } + + public string Name + { + get { return "Trim Logs Job"; } + } + + public int DefaultInterval + { + get { return 1440; } + } + + public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId) + { + _logProvider.Trim(); + } + } +} \ No newline at end of file