diff --git a/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs b/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs index 4ccc2364b..3a15958bc 100644 --- a/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs +++ b/NzbDrone.Core.Test/HistoryTests/HistoryRepositoryFixture.cs @@ -4,6 +4,7 @@ using NUnit.Framework; using NzbDrone.Core.History; using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Tv; namespace NzbDrone.Core.Test.HistoryTests { @@ -41,5 +42,71 @@ public void should_read_write_dictionary() StoredModel.Data.Should().HaveCount(2); } + + [Test] + public void should_delete_orphaned_items_by_series() + { + var history = Builder.CreateNew().BuildNew(); + Subject.Insert(history); + + Subject.CleanupOrphanedBySeries(); + Subject.All().Should().BeEmpty(); + } + + [Test] + public void should_delete_orphaned_items_by_episode() + { + var history = Builder.CreateNew().BuildNew(); + Subject.Insert(history); + + Subject.CleanupOrphanedByEpisode(); + Subject.All().Should().BeEmpty(); + } + + [Test] + public void should_not_delete_unorphaned_data_by_series() + { + var series = Builder.CreateNew() + .BuildNew(); + + Db.Insert(series); + + var history = Builder.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.CreateNew() + .BuildNew(); + + Db.Insert(episode); + + var history = Builder.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); + } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 119e1d333..8344a179d 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -179,6 +179,7 @@ + diff --git a/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/CleanupOrphanedEpisodesFixture.cs b/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/CleanupOrphanedEpisodesFixture.cs new file mode 100644 index 000000000..d21ef817d --- /dev/null +++ b/NzbDrone.Core.Test/TvTests/EpisodeRepositoryTests/CleanupOrphanedEpisodesFixture.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 + { + [Test] + public void should_delete_orphaned_episodes() + { + var episode = Builder.CreateNew() + .BuildNew(); + + Subject.Insert(episode); + Subject.CleanupOrphanedEpisodes(); + Subject.All().Should().BeEmpty(); + } + + [Test] + public void should_not_delete_unorphaned_episodes() + { + var series = Builder.CreateNew() + .BuildNew(); + + Db.Insert(series); + + var episodes = Builder.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); + } + } +} diff --git a/NzbDrone.Core/History/HistoryRepository.cs b/NzbDrone.Core/History/HistoryRepository.cs index 096397248..50ad32994 100644 --- a/NzbDrone.Core/History/HistoryRepository.cs +++ b/NzbDrone.Core/History/HistoryRepository.cs @@ -3,7 +3,6 @@ using System.Linq; using Marr.Data.QGen; using NzbDrone.Core.Datastore; -using NzbDrone.Core.Messaging; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Tv; @@ -13,13 +12,18 @@ public interface IHistoryRepository : IBasicRepository { void Trim(); List GetEpisodeHistory(int episodeId); + void CleanupOrphanedBySeries(); + void CleanupOrphanedByEpisode(); } public class HistoryRepository : BasicRepository, IHistoryRepository { + private readonly IDatabase _database; + public HistoryRepository(IDatabase database, IEventAggregator eventAggregator) : base(database, eventAggregator) { + _database = database; } public void Trim() @@ -35,6 +39,30 @@ public List GetEpisodeHistory(int episodeId) 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 GetPaged(PagingSpec pagingSpec) { var pagingQuery = Query.Join(JoinType.Inner, h => h.Series, (h, s) => h.SeriesId == s.Id) diff --git a/NzbDrone.Core/History/HistoryService.cs b/NzbDrone.Core/History/HistoryService.cs index 6053541d0..4fc398c72 100644 --- a/NzbDrone.Core/History/HistoryService.cs +++ b/NzbDrone.Core/History/HistoryService.cs @@ -18,6 +18,7 @@ public interface IHistoryService void Trim(); QualityModel GetBestQualityInHistory(int episodeId); PagingSpec Paged(PagingSpec pagingSpec); + void CleanupOrphaned(); } public class HistoryService : IHistoryService, IHandle, IHandle @@ -41,6 +42,12 @@ public PagingSpec Paged(PagingSpec pagingSpec) return _historyRepository.GetPaged(pagingSpec); } + public void CleanupOrphaned() + { + _historyRepository.CleanupOrphanedBySeries(); + _historyRepository.CleanupOrphanedByEpisode(); + } + public void Purge() { _historyRepository.Purge(); diff --git a/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs b/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs new file mode 100644 index 000000000..69cb713ca --- /dev/null +++ b/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedEpisodes.cs @@ -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(); + } + } +} diff --git a/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs b/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs new file mode 100644 index 000000000..0df2e5c1f --- /dev/null +++ b/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedHistoryItems.cs @@ -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(); + } + } +} diff --git a/NzbDrone.Core/Housekeeping/HousekeepingCommand.cs b/NzbDrone.Core/Housekeeping/HousekeepingCommand.cs new file mode 100644 index 000000000..ffdc21fc8 --- /dev/null +++ b/NzbDrone.Core/Housekeeping/HousekeepingCommand.cs @@ -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 + { + } +} diff --git a/NzbDrone.Core/Housekeeping/HousekeepingService.cs b/NzbDrone.Core/Housekeeping/HousekeepingService.cs new file mode 100644 index 000000000..00933844b --- /dev/null +++ b/NzbDrone.Core/Housekeeping/HousekeepingService.cs @@ -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 + { + private readonly IEnumerable _housekeepers; + private readonly Logger _logger; + + public HousekeepingService(IEnumerable 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); + } + } + } + } +} diff --git a/NzbDrone.Core/Housekeeping/IHousekeepingTask.cs b/NzbDrone.Core/Housekeeping/IHousekeepingTask.cs new file mode 100644 index 000000000..f6c536c7d --- /dev/null +++ b/NzbDrone.Core/Housekeeping/IHousekeepingTask.cs @@ -0,0 +1,10 @@ +using NzbDrone.Core.IndexerSearch.Definitions; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.Housekeeping +{ + public interface IHousekeepingTask + { + void Clean(); + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Jobs/TaskManager.cs b/NzbDrone.Core/Jobs/TaskManager.cs index a04931ac3..7851cbbf8 100644 --- a/NzbDrone.Core/Jobs/TaskManager.cs +++ b/NzbDrone.Core/Jobs/TaskManager.cs @@ -5,6 +5,7 @@ using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration.Events; using NzbDrone.Core.DataAugmentation.Scene; +using NzbDrone.Core.Housekeeping; using NzbDrone.Core.Indexers; using NzbDrone.Core.Instrumentation.Commands; using NzbDrone.Core.Lifecycle; @@ -51,7 +52,8 @@ public void Handle(ApplicationStartedEvent message) new ScheduledTask{ Interval = 60, TypeName = typeof(ApplicationUpdateCommand).FullName}, new ScheduledTask{ Interval = 1*60, TypeName = typeof(TrimLogCommand).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(); diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 26e301627..e53438f4a 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -214,6 +214,11 @@ + + + + + diff --git a/NzbDrone.Core/Tv/EpisodeRepository.cs b/NzbDrone.Core/Tv/EpisodeRepository.cs index 3a9ade8d9..db9984a0b 100644 --- a/NzbDrone.Core/Tv/EpisodeRepository.cs +++ b/NzbDrone.Core/Tv/EpisodeRepository.cs @@ -22,6 +22,7 @@ public interface IEpisodeRepository : IBasicRepository void SetMonitoredFlat(Episode episode, bool monitored); void SetMonitoredBySeason(int seriesId, int seasonNumber, bool monitored); void SetFileId(int episodeId, int fileId); + void CleanupOrphanedEpisodes(); } public class EpisodeRepository : BasicRepository, IEpisodeRepository @@ -123,6 +124,18 @@ public void SetFileId(int episodeId, int fileId) 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 GetEpisodesWithoutFilesQuery(PagingSpec pagingSpec, DateTime currentTime, int startingSeasonNumber) { return Query.Join(JoinType.Inner, e => e.Series, (e, s) => e.SeriesId == s.Id) diff --git a/NzbDrone.Core/Tv/EpisodeService.cs b/NzbDrone.Core/Tv/EpisodeService.cs index 2e83cc659..b58825e6a 100644 --- a/NzbDrone.Core/Tv/EpisodeService.cs +++ b/NzbDrone.Core/Tv/EpisodeService.cs @@ -30,6 +30,7 @@ public interface IEpisodeService void UpdateMany(List episodes); void DeleteMany(List episodes); void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool monitored); + void CleanupOrphanedEpisodes(); } public class EpisodeService : IEpisodeService, @@ -113,6 +114,11 @@ public void SetEpisodeMonitoredBySeason(int seriesId, int seasonNumber, bool mon _episodeRepository.SetMonitoredBySeason(seriesId, seasonNumber, monitored); } + public void CleanupOrphanedEpisodes() + { + _episodeRepository.CleanupOrphanedEpisodes(); + } + public bool IsFirstOrLastEpisodeOfSeason(int episodeId) { var episode = GetEpisode(episodeId);