Added housekeeping to cleanup orphaned episodes and history items

pull/4/head
Mark McDowall 11 years ago
parent 174689f533
commit 07b68b17ee

@ -4,6 +4,7 @@ using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Core.History; using NzbDrone.Core.History;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.HistoryTests namespace NzbDrone.Core.Test.HistoryTests
{ {
@ -41,5 +42,71 @@ namespace NzbDrone.Core.Test.HistoryTests
StoredModel.Data.Should().HaveCount(2); StoredModel.Data.Should().HaveCount(2);
} }
[Test]
public void should_delete_orphaned_items_by_series()
{
var history = Builder<History.History>.CreateNew().BuildNew();
Subject.Insert(history);
Subject.CleanupOrphanedBySeries();
Subject.All().Should().BeEmpty();
}
[Test]
public void should_delete_orphaned_items_by_episode()
{
var history = Builder<History.History>.CreateNew().BuildNew();
Subject.Insert(history);
Subject.CleanupOrphanedByEpisode();
Subject.All().Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_data_by_series()
{
var series = Builder<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.Id = 0)
.TheFirst(1)
.With(h => h.SeriesId = series.Id)
.Build();
Subject.InsertMany(history);
Subject.CleanupOrphanedBySeries();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(h => h.SeriesId == series.Id);
}
[Test]
public void should_not_delete_unorphaned_data_by_episode()
{
var episode = Builder<Episode>.CreateNew()
.BuildNew();
Db.Insert(episode);
var history = Builder<History.History>.CreateListOfSize(2)
.All()
.With(h => h.Id = 0)
.TheFirst(1)
.With(h => h.EpisodeId = episode.Id)
.Build();
Subject.InsertMany(history);
Subject.CleanupOrphanedByEpisode();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(h => h.EpisodeId == episode.Id);
}
} }
} }

@ -179,6 +179,7 @@
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteFileFixture.cs" /> <Compile Include="ProviderTests\RecycleBinProviderTests\DeleteFileFixture.cs" />
<Compile Include="ProviderTests\RecycleBinProviderTests\DeleteDirectoryFixture.cs" /> <Compile Include="ProviderTests\RecycleBinProviderTests\DeleteDirectoryFixture.cs" />
<Compile Include="NotificationTests\PlexProviderTest.cs" /> <Compile Include="NotificationTests\PlexProviderTest.cs" />
<Compile Include="TvTests\EpisodeRepositoryTests\CleanupOrphanedEpisodesFixture.cs" />
<Compile Include="TvTests\RefreshEpisodeServiceFixture.cs" /> <Compile Include="TvTests\RefreshEpisodeServiceFixture.cs" />
<Compile Include="TvTests\EpisodeProviderTests\HandleEpisodeFileDeletedFixture.cs" /> <Compile Include="TvTests\EpisodeProviderTests\HandleEpisodeFileDeletedFixture.cs" />
<Compile Include="TvTests\EpisodeRepositoryTests\FindEpisodeFixture.cs" /> <Compile Include="TvTests\EpisodeRepositoryTests\FindEpisodeFixture.cs" />

@ -0,0 +1,44 @@
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.TvTests.EpisodeRepositoryTests
{
[TestFixture]
public class CleanupOrphanedEpisodesFixture : DbTest<EpisodeRepository, Episode>
{
[Test]
public void should_delete_orphaned_episodes()
{
var episode = Builder<Episode>.CreateNew()
.BuildNew();
Subject.Insert(episode);
Subject.CleanupOrphanedEpisodes();
Subject.All().Should().BeEmpty();
}
[Test]
public void should_not_delete_unorphaned_episodes()
{
var series = Builder<Series>.CreateNew()
.BuildNew();
Db.Insert(series);
var episodes = Builder<Episode>.CreateListOfSize(2)
.All()
.With(e => e.Id = 0)
.TheFirst(1)
.With(e => e.SeriesId = series.Id)
.Build();
Subject.InsertMany(episodes);
Subject.CleanupOrphanedEpisodes();
Subject.All().Should().HaveCount(1);
Subject.All().Should().Contain(e => e.SeriesId == series.Id);
}
}
}

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Marr.Data.QGen; using Marr.Data.QGen;
using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
@ -13,13 +12,18 @@ namespace NzbDrone.Core.History
{ {
void Trim(); void Trim();
List<QualityModel> GetEpisodeHistory(int episodeId); List<QualityModel> GetEpisodeHistory(int episodeId);
void CleanupOrphanedBySeries();
void CleanupOrphanedByEpisode();
} }
public class HistoryRepository : BasicRepository<History>, IHistoryRepository public class HistoryRepository : BasicRepository<History>, IHistoryRepository
{ {
private readonly IDatabase _database;
public HistoryRepository(IDatabase database, IEventAggregator eventAggregator) public HistoryRepository(IDatabase database, IEventAggregator eventAggregator)
: base(database, eventAggregator) : base(database, eventAggregator)
{ {
_database = database;
} }
public void Trim() public void Trim()
@ -35,6 +39,30 @@ namespace NzbDrone.Core.History
return history.Select(h => h.Quality).ToList(); return history.Select(h => h.Quality).ToList();
} }
public void CleanupOrphanedBySeries()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Series
ON History.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
public void CleanupOrphanedByEpisode()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Episodes
ON History.EpisodeId = Episodes.Id
WHERE Episodes.Id IS NULL)");
}
public override PagingSpec<History> GetPaged(PagingSpec<History> pagingSpec) public override PagingSpec<History> GetPaged(PagingSpec<History> pagingSpec)
{ {
var pagingQuery = Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id) var pagingQuery = Query.Join<History, Series>(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id)

@ -18,6 +18,7 @@ namespace NzbDrone.Core.History
void Trim(); void Trim();
QualityModel GetBestQualityInHistory(int episodeId); QualityModel GetBestQualityInHistory(int episodeId);
PagingSpec<History> Paged(PagingSpec<History> pagingSpec); PagingSpec<History> Paged(PagingSpec<History> pagingSpec);
void CleanupOrphaned();
} }
public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent> public class HistoryService : IHistoryService, IHandle<EpisodeGrabbedEvent>, IHandle<EpisodeImportedEvent>
@ -41,6 +42,12 @@ namespace NzbDrone.Core.History
return _historyRepository.GetPaged(pagingSpec); return _historyRepository.GetPaged(pagingSpec);
} }
public void CleanupOrphaned()
{
_historyRepository.CleanupOrphanedBySeries();
_historyRepository.CleanupOrphanedByEpisode();
}
public void Purge() public void Purge()
{ {
_historyRepository.Purge(); _historyRepository.Purge();

@ -0,0 +1,23 @@
using NLog;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedEpisodes : IHousekeepingTask
{
private readonly IEpisodeService _episodeService;
private readonly Logger _logger;
public CleanupOrphanedEpisodes(IEpisodeService episodeService, Logger logger)
{
_episodeService = episodeService;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned episodes cleanup");
_episodeService.CleanupOrphanedEpisodes();
}
}
}

@ -0,0 +1,23 @@
using NLog;
using NzbDrone.Core.History;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedHistoryItems : IHousekeepingTask
{
private readonly IHistoryService _historyService;
private readonly Logger _logger;
public CleanupOrphanedHistoryItems(IHistoryService historyService, Logger logger)
{
_historyService = historyService;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned history cleanup");
_historyService.CleanupOrphaned();
}
}
}

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.Housekeeping
{
public class HousekeepingCommand : Command
{
}
}

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NLog;
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.Housekeeping
{
public interface IHousekeepingService
{
}
public class HousekeepingService : IHousekeepingService, IExecute<HousekeepingCommand>
{
private readonly IEnumerable<IHousekeepingTask> _housekeepers;
private readonly Logger _logger;
public HousekeepingService(IEnumerable<IHousekeepingTask> housekeepers, Logger logger)
{
_housekeepers = housekeepers;
_logger = logger;
}
public void Execute(HousekeepingCommand message)
{
_logger.Info("Running housecleaning tasks");
foreach (var housekeeper in _housekeepers)
{
try
{
housekeeper.Clean();
}
catch (Exception ex)
{
_logger.ErrorException("Error running housekeeping task: " + housekeeper.GetType().FullName, ex);
}
}
}
}
}

@ -0,0 +1,10 @@
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser.Model;
namespace NzbDrone.Core.Housekeeping
{
public interface IHousekeepingTask
{
void Clean();
}
}

@ -5,6 +5,7 @@ using NLog;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.DataAugmentation.Scene;
using NzbDrone.Core.Housekeeping;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.Instrumentation.Commands; using NzbDrone.Core.Instrumentation.Commands;
using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Lifecycle;
@ -51,7 +52,8 @@ namespace NzbDrone.Core.Jobs
new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName}, new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName},
new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName}, new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).FullName},
new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName}, new ScheduledTask{ Interval = 3*60, TypeName = typeof(UpdateSceneMappingCommand).FullName},
new ScheduledTask{ Interval = 1, TypeName = typeof(TrackedCommandCleanupCommand).FullName} new ScheduledTask{ Interval = 1, TypeName = typeof(TrackedCommandCleanupCommand).FullName},
new ScheduledTask{ Interval = 24*60, TypeName = typeof(HousekeepingCommand).FullName}
}; };
var currentTasks = _scheduledTaskRepository.All(); var currentTasks = _scheduledTaskRepository.All();

@ -214,6 +214,11 @@
<Compile Include="Exceptions\DownstreamException.cs" /> <Compile Include="Exceptions\DownstreamException.cs" />
<Compile Include="Exceptions\NzbDroneClientException.cs" /> <Compile Include="Exceptions\NzbDroneClientException.cs" />
<Compile Include="Exceptions\StatusCodeToExceptions.cs" /> <Compile Include="Exceptions\StatusCodeToExceptions.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodes.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\HousekeepingCommand.cs" />
<Compile Include="Housekeeping\HousekeepingService.cs" />
<Compile Include="Housekeeping\IHousekeepingTask.cs" />
<Compile Include="IndexerSearch\SeriesSearchService.cs" /> <Compile Include="IndexerSearch\SeriesSearchService.cs" />
<Compile Include="IndexerSearch\SeriesSearchCommand.cs" /> <Compile Include="IndexerSearch\SeriesSearchCommand.cs" />
<Compile Include="IndexerSearch\EpisodeSearchService.cs" /> <Compile Include="IndexerSearch\EpisodeSearchService.cs" />

@ -22,6 +22,7 @@ namespace NzbDrone.Core.Tv
void SetMonitoredFlat(Episode episode, bool monitored); void SetMonitoredFlat(Episode episode, bool monitored);
void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored); void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
void SetFileId(int episodeId, int fileId); void SetFileId(int episodeId, int fileId);
void CleanupOrphanedEpisodes();
} }
public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository public class EpisodeRepository : BasicRepository<Episode>, IEpisodeRepository
@ -123,6 +124,18 @@ namespace NzbDrone.Core.Tv
SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId); SetFields(new Episode { Id = episodeId, EpisodeFileId = fileId }, episode => episode.EpisodeFileId);
} }
public void CleanupOrphanedEpisodes()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM Episodes
WHERE Id IN (
SELECT Episodes.Id FROM Episodes
LEFT OUTER JOIN Series
ON Episodes.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
private SortBuilder<Episode> GetEpisodesWithoutFilesQuery(PagingSpec<Episode> pagingSpec, DateTime currentTime, int startingSeasonNumber) private SortBuilder<Episode> GetEpisodesWithoutFilesQuery(PagingSpec<Episode> pagingSpec, DateTime currentTime, int startingSeasonNumber)
{ {
return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id) return Query.Join<Episode, Series>(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id)

@ -30,6 +30,7 @@ namespace NzbDrone.Core.Tv
void UpdateMany(List<Episode> episodes); void UpdateMany(List<Episode> episodes);
void DeleteMany(List<Episode> episodes); void DeleteMany(List<Episode> episodes);
void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored); void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored);
void CleanupOrphanedEpisodes();
} }
public class EpisodeService : IEpisodeService, public class EpisodeService : IEpisodeService,
@ -113,6 +114,11 @@ namespace NzbDrone.Core.Tv
_episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored); _episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored);
} }
public void CleanupOrphanedEpisodes()
{
_episodeRepository.CleanupOrphanedEpisodes();
}
public bool IsFirstOrLastEpisodeOfSeason(int episodeId) public bool IsFirstOrLastEpisodeOfSeason(int episodeId)
{ {
var episode = GetEpisode(episodeId); var episode = GetEpisode(episodeId);

Loading…
Cancel
Save