Added cleanup job for search history

New: Search History cleanup job will only keep the last week of results
This commit is contained in:
Mark McDowall 2012-06-12 12:38:38 -07:00
parent c50ed82b7e
commit b7408b726a
5 changed files with 159 additions and 0 deletions

View File

@ -66,6 +66,46 @@ namespace NzbDrone.Core.Test.ProviderTests
i.SearchError = ReportRejectionType.None;
}
private void WithExpiredHistory()
{
var history = Builder<SearchHistory>.CreateListOfSize(10)
.All()
.With(h => h.SearchTime = DateTime.Now.AddDays(10))
.Build();
foreach(var searchHistory in history)
{
var items = Builder<SearchHistoryItem>.CreateListOfSize(10)
.All()
.With(i => i.Id == searchHistory.Id)
.Build();
Db.InsertMany(items);
}
Db.InsertMany(history);
}
private void WithValidHistory()
{
var history = Builder<SearchHistory>.CreateListOfSize(10)
.All()
.With(h => h.SearchTime = DateTime.Now)
.Build();
foreach (var searchHistory in history)
{
var items = Builder<SearchHistoryItem>.CreateListOfSize(10)
.All()
.With(i => i.Id == searchHistory.Id)
.Build();
Db.InsertMany(items);
}
Db.InsertMany(history);
}
[Test]
public void Add_should_add_history_and_history_items()
{
@ -263,5 +303,54 @@ namespace NzbDrone.Core.Test.ProviderTests
Mocker.GetMock<DownloadProvider>().Verify(v => v.DownloadReport(It.IsAny<EpisodeParseResult>()), Times.Once());
}
[Test]
public void Cleanup_should_not_blowup_if_there_is_nothing_to_delete()
{
WithRealDb();
Mocker.Resolve<SearchHistoryProvider>().Cleanup();
Db.Fetch<SearchHistory>().Should().HaveCount(0);
}
[Test]
public void Cleanup_should_delete_searchHistory_older_than_1_week()
{
WithRealDb();
WithExpiredHistory();
Mocker.Resolve<SearchHistoryProvider>().Cleanup();
Db.Fetch<SearchHistory>().Should().HaveCount(0);
}
[Test]
public void Cleanup_should_delete_searchHistoryItems_older_than_1_week()
{
WithRealDb();
WithExpiredHistory();
Mocker.Resolve<SearchHistoryProvider>().Cleanup();
Db.Fetch<SearchHistoryItem>().Should().HaveCount(0);
}
[Test]
public void Cleanup_should_not_delete_searchHistory_younger_than_1_week()
{
WithRealDb();
WithValidHistory();
Mocker.Resolve<SearchHistoryProvider>().Cleanup();
Db.Fetch<SearchHistory>().Should().HaveCount(10);
}
[Test]
public void Cleanup_should_not_delete_searchHistoryItems_younger_than_1_week()
{
WithRealDb();
WithValidHistory();
Mocker.Resolve<SearchHistoryProvider>().Cleanup();
Db.Fetch<SearchHistoryItem>().Should().HaveCount(100);
}
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Ninject;
using NLog;
using NzbDrone.Core.Helpers;
using NzbDrone.Core.Model.Notification;
using NzbDrone.Core.Providers;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Repository;
namespace NzbDrone.Core.Jobs
{
public class SearchHistoryCleanupJob : IJob
{
private readonly SearchHistoryProvider _searchHistoryProvider;
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
[Inject]
public SearchHistoryCleanupJob(SearchHistoryProvider searchHistoryProvider)
{
_searchHistoryProvider = searchHistoryProvider;
}
public SearchHistoryCleanupJob()
{
}
public string Name
{
get { return "Search History Cleanup"; }
}
public TimeSpan DefaultInterval
{
get { return TimeSpan.FromHours(24); }
}
public virtual void Start(ProgressNotification notification, int targetId, int secondaryTargetId)
{
Logger.Info("Running search history cleanup.");
_searchHistoryProvider.Cleanup();
}
}
}

View File

@ -248,6 +248,7 @@
<Compile Include="Helpers\SortHelper.cs" />
<Compile Include="Helpers\SabnzbdPriorityTypeConverter.cs" />
<Compile Include="Jobs\CheckpointJob.cs" />
<Compile Include="Jobs\SearchHistoryCleanupJob.cs" />
<Compile Include="Model\DownloadClientType.cs" />
<Compile Include="Instrumentation\LogDbContext.cs" />
<Compile Include="Instrumentation\LogProvider.cs" />

View File

@ -117,5 +117,19 @@ namespace NzbDrone.Core.Providers
logger.Info("Forcing Download of: {0}", item.ReportTitle);
_downloadProvider.DownloadReport(parseResult);
}
public virtual void Cleanup()
{
var ids = _database.Fetch<int>("SELECT Id FROM SearchHistory WHERE SearchTime > @0", DateTime.Now.AddDays(7));
if (ids.Any())
{
logger.Trace("Deleting old search items");
_database.Execute("DELETE FROM SearchHistoryItems WHERE SearchHistoryId IN (@0)", ids);
logger.Trace("Deleting old search results");
_database.Execute("DELETE FROM SearchHistory WHERE Id IN (@0)", ids);
}
}
}
}

10
NzbDrone.ncrunchsolution Normal file
View File

@ -0,0 +1,10 @@
<SolutionConfiguration>
<FileVersion>0</FileVersion>
<AutoEnableOnStartup>Default</AutoEnableOnStartup>
<AllowParallelTestExecution>false</AllowParallelTestExecution>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>UseStaticAnalysis</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>UseStaticAnalysis</FrameworkUtilisationTypeForMSpec>
<FrameworkUtilisationTypeForMSTest>UseStaticAnalysis</FrameworkUtilisationTypeForMSTest>
<EngineModes>Run all tests automatically:BFRydWU=;Run all tests manually:BUZhbHNl;Run impacted tests automatically, others manually (experimental!):CklzSW1wYWN0ZWQ=;Run pinned tests automatically, others manually:CElzUGlubmVk</EngineModes>
</SolutionConfiguration>