From 4877ce405e4d21f1bcf6adc66f25d2ef1022c274 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 5 Mar 2013 11:49:34 -0800 Subject: [PATCH] event based cleanup when a series is deleted. --- NzbDrone.Api/Series/SeriesModule.cs | 2 +- NzbDrone.Core/Datastore/BasicRepository.cs | 6 ++ .../Jobs/Implementations/DeleteSeriesJob.cs | 69 ------------------ NzbDrone.Core/MediaCover/MediaCoverService.cs | 21 +++++- NzbDrone.Core/MediaFiles/MediaFileService.cs | 10 ++- NzbDrone.Core/NzbDrone.Core.csproj | 1 - NzbDrone.Core/Providers/RecycleBinProvider.cs | 19 +++-- NzbDrone.Core/Tv/EpisodeService.cs | 10 ++- NzbDrone.Core/Tv/Events/SeriesDeletedEvent.cs | 4 +- NzbDrone.Core/Tv/SeasonService.cs | Bin 6468 -> 6712 bytes NzbDrone.Core/Tv/SeriesService.cs | 6 +- 11 files changed, 61 insertions(+), 87 deletions(-) delete mode 100644 NzbDrone.Core/Jobs/Implementations/DeleteSeriesJob.cs diff --git a/NzbDrone.Api/Series/SeriesModule.cs b/NzbDrone.Api/Series/SeriesModule.cs index 099ff27f5..4f02e29d0 100644 --- a/NzbDrone.Api/Series/SeriesModule.cs +++ b/NzbDrone.Api/Series/SeriesModule.cs @@ -100,7 +100,7 @@ namespace NzbDrone.Api.Series private Response DeleteSeries(int id) { var deleteFiles = Convert.ToBoolean(Request.Headers["deleteFiles"].FirstOrDefault()); - _jobProvider.Enqueue(typeof(DeleteSeriesJob), new { SeriesId = id, DeleteFiles = deleteFiles }); + _seriesService.DeleteSeries(id, deleteFiles); return new Response { StatusCode = HttpStatusCode.OK }; } } diff --git a/NzbDrone.Core/Datastore/BasicRepository.cs b/NzbDrone.Core/Datastore/BasicRepository.cs index 000f5627b..4042e98c3 100644 --- a/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/NzbDrone.Core/Datastore/BasicRepository.cs @@ -14,6 +14,7 @@ namespace NzbDrone.Core.Datastore void Delete(int id); IList InsertMany(IList model); IList UpdateMany(IList model); + void DeleteMany(IList model); void Purge(); bool HasItems(); } @@ -64,6 +65,11 @@ namespace NzbDrone.Core.Datastore return ObjectDatabase.UpdateMany(model); } + public void DeleteMany(IList model) + { + ObjectDatabase.DeleteMany(model); + } + public TModel Upsert(TModel model) { if (model.Id == 0) diff --git a/NzbDrone.Core/Jobs/Implementations/DeleteSeriesJob.cs b/NzbDrone.Core/Jobs/Implementations/DeleteSeriesJob.cs deleted file mode 100644 index 2bfe6a002..000000000 --- a/NzbDrone.Core/Jobs/Implementations/DeleteSeriesJob.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Linq; -using NLog; -using NzbDrone.Core.Model.Notification; -using NzbDrone.Core.Providers; -using NzbDrone.Core.Tv; - -namespace NzbDrone.Core.Jobs.Implementations -{ - public class DeleteSeriesJob : IJob - { - private readonly ISeriesService _seriesService; - private readonly RecycleBinProvider _recycleBinProvider; - private readonly ISeriesRepository _seriesRepository; - - private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); - - public DeleteSeriesJob(ISeriesService seriesService, RecycleBinProvider recycleBinProvider, ISeriesRepository seriesRepository) - { - _seriesService = seriesService; - _recycleBinProvider = recycleBinProvider; - _seriesRepository = seriesRepository; - } - - public string Name - { - get { return "Delete Series"; } - } - - public TimeSpan DefaultInterval - { - get { return TimeSpan.FromTicks(0); } - } - - public void Start(ProgressNotification notification, dynamic options) - { - if (options == null) - throw new ArgumentNullException("options"); - - if (options.SeriesId == 0) - throw new ArgumentNullException("options.SeriesId"); - - DeleteSeries(notification, (int)options.SeriesId, (bool)options.DeleteFiles); - } - - private void DeleteSeries(ProgressNotification notification, int seriesId, bool deleteFiles) - { - Logger.Trace("Deleting Series [{0}]", seriesId); - - var series = _seriesRepository.Get(seriesId); - var title = series.Title; - - notification.CurrentMessage = String.Format("Deleting '{0}' from database", title); - - _seriesRepository.Delete(seriesId); - - notification.CurrentMessage = String.Format("Successfully deleted '{0}' from database", title); - - if (deleteFiles) - { - notification.CurrentMessage = String.Format("Deleting files from disk for series '{0}'", title); - - _recycleBinProvider.DeleteDirectory(series.Path); - - notification.CurrentMessage = String.Format("Successfully deleted files from disk for series '{0}'", title); - } - } - } -} \ No newline at end of file diff --git a/NzbDrone.Core/MediaCover/MediaCoverService.cs b/NzbDrone.Core/MediaCover/MediaCoverService.cs index 89665f6e1..778767181 100644 --- a/NzbDrone.Core/MediaCover/MediaCoverService.cs +++ b/NzbDrone.Core/MediaCover/MediaCoverService.cs @@ -1,7 +1,6 @@ using System; using System.IO; using System.Linq; -using System.Threading; using NLog; using NzbDrone.Common; using NzbDrone.Common.Eventing; @@ -10,7 +9,9 @@ using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.MediaCover { - public class MediaCoverService : IHandleAsync + public class MediaCoverService : + IHandleAsync, + IHandleAsync { private readonly HttpProvider _httpProvider; private readonly DiskProvider _diskProvider; @@ -61,9 +62,23 @@ namespace NzbDrone.Core.MediaCover } } + public void HandleAsync(SeriesDeletedEvent message) + { + var path = GetSeriesCoverPath(message.Series.Id); + if (_diskProvider.FolderExists(path)) + { + _diskProvider.DeleteFolder(path, true); + } + } + private string GetCoverPath(int seriesId, MediaCoverTypes coverTypes) { - return Path.Combine(_coverRootFolder, seriesId.ToString("0000"), coverTypes.ToString().ToLower() + ".jpg"); + return Path.Combine(GetSeriesCoverPath(seriesId), coverTypes.ToString().ToLower() + ".jpg"); + } + + private string GetSeriesCoverPath(int seriesId) + { + return Path.Combine(_coverRootFolder, seriesId.ToString("0000")); } } } diff --git a/NzbDrone.Core/MediaFiles/MediaFileService.cs b/NzbDrone.Core/MediaFiles/MediaFileService.cs index da8ae9baa..74a1f4ecd 100644 --- a/NzbDrone.Core/MediaFiles/MediaFileService.cs +++ b/NzbDrone.Core/MediaFiles/MediaFileService.cs @@ -3,10 +3,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; using NLog; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Configuration; using NzbDrone.Core.Qualities; using NzbDrone.Core.Tv; using NzbDrone.Core.Helpers; +using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.MediaFiles { @@ -23,7 +25,7 @@ namespace NzbDrone.Core.MediaFiles string GetNewFilename(IList episodes, Series series, Quality quality, bool proper, EpisodeFile episodeFile); } - public class MediaFileService : IMediaFileService + public class MediaFileService : IMediaFileService, IHandleAsync { private readonly IConfigService _configService; private readonly IEpisodeService _episodeService; @@ -210,5 +212,11 @@ namespace NzbDrone.Core.MediaFiles return result.Trim(); } + + public void HandleAsync(SeriesDeletedEvent message) + { + var files = GetFilesBySeries(message.Series.Id); + _mediaFileRepository.DeleteMany(files); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 3de173990..3f9d09dbf 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -208,7 +208,6 @@ - diff --git a/NzbDrone.Core/Providers/RecycleBinProvider.cs b/NzbDrone.Core/Providers/RecycleBinProvider.cs index 854e493c2..46ada2e0a 100644 --- a/NzbDrone.Core/Providers/RecycleBinProvider.cs +++ b/NzbDrone.Core/Providers/RecycleBinProvider.cs @@ -1,15 +1,15 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Text; using NLog; using NzbDrone.Common; +using NzbDrone.Common.Eventing; using NzbDrone.Core.Configuration; +using NzbDrone.Core.Tv.Events; namespace NzbDrone.Core.Providers { - public class RecycleBinProvider + public class RecycleBinProvider : IHandleAsync { private readonly DiskProvider _diskProvider; private readonly IConfigService _configService; @@ -47,7 +47,7 @@ namespace NzbDrone.Core.Providers logger.Trace("Setting last accessed: {0}", path); _diskProvider.DirectorySetLastWriteTimeUtc(destination, DateTime.UtcNow); - foreach(var file in _diskProvider.GetFiles(destination, SearchOption.AllDirectories)) + foreach (var file in _diskProvider.GetFiles(destination, SearchOption.AllDirectories)) { _diskProvider.FileSetLastWriteTimeUtc(file, DateTime.UtcNow); } @@ -119,8 +119,8 @@ namespace NzbDrone.Core.Providers logger.Trace("Folder hasn't expired yet, skipping: {0}", folder); continue; } - - _diskProvider.DeleteFolder(folder, true); + + _diskProvider.DeleteFolder(folder, true); } foreach (var file in _diskProvider.GetFiles(_configService.RecycleBin, SearchOption.TopDirectoryOnly)) @@ -130,11 +130,16 @@ namespace NzbDrone.Core.Providers logger.Trace("File hasn't expired yet, skipping: {0}", file); continue; } - + _diskProvider.DeleteFile(file); } logger.Trace("Recycling Bin has been cleaned up."); } + + public void HandleAsync(SeriesDeletedEvent message) + { + DeleteDirectory(message.Series.Path); + } } } diff --git a/NzbDrone.Core/Tv/EpisodeService.cs b/NzbDrone.Core/Tv/EpisodeService.cs index afea08d79..540e19fdd 100644 --- a/NzbDrone.Core/Tv/EpisodeService.cs +++ b/NzbDrone.Core/Tv/EpisodeService.cs @@ -35,7 +35,9 @@ namespace NzbDrone.Core.Tv List EpisodesBetweenDates(DateTime start, DateTime end); } - public class EpisodeService : IEpisodeService, IHandle + public class EpisodeService : IEpisodeService, + IHandle, + IHandleAsync { private static readonly Logger logger = LogManager.GetCurrentClassLogger(); @@ -371,5 +373,11 @@ namespace NzbDrone.Core.Tv _episodeRepository.Update(episode); } } + + public void HandleAsync(SeriesDeletedEvent message) + { + var episodes = GetEpisodeBySeries(message.Series.Id); + _episodeRepository.DeleteMany(episodes); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/Tv/Events/SeriesDeletedEvent.cs b/NzbDrone.Core/Tv/Events/SeriesDeletedEvent.cs index cc9d242e6..f9acfe79a 100644 --- a/NzbDrone.Core/Tv/Events/SeriesDeletedEvent.cs +++ b/NzbDrone.Core/Tv/Events/SeriesDeletedEvent.cs @@ -6,10 +6,12 @@ namespace NzbDrone.Core.Tv.Events public class SeriesDeletedEvent : IEvent { public Series Series { get; private set; } + public bool DeleteFiles { get; private set; } - public SeriesDeletedEvent(Series series) + public SeriesDeletedEvent(Series series, bool deleteFiles) { Series = series; + DeleteFiles = deleteFiles; } } } \ No newline at end of file diff --git a/NzbDrone.Core/Tv/SeasonService.cs b/NzbDrone.Core/Tv/SeasonService.cs index d26c035a00335f11b4956e7c3ae6a23272894800..06e34136397c3c3c3ff6c2a65a985c33d705b32c 100644 GIT binary patch delta 151 zcmX?Nw8Lb>ea6XK7=;8Ji!1YzZGuyaGE<9PQgc!_Ut?UrF}a*g3?id}keNJ}O>1%h zJDXl&aeiL0f~|skYDsV^nB!ClQKpfbT3no%o~j4o>UpMUT2EffB(IQuEea6jPOw&0wXK|n70{}zY2Gsxn diff --git a/NzbDrone.Core/Tv/SeriesService.cs b/NzbDrone.Core/Tv/SeriesService.cs index 11415fa6c..22e1bb457 100644 --- a/NzbDrone.Core/Tv/SeriesService.cs +++ b/NzbDrone.Core/Tv/SeriesService.cs @@ -23,7 +23,7 @@ namespace NzbDrone.Core.Tv void UpdateFromSeriesEditor(IList editedSeries); Series FindByTvdbId(int tvdbId); void SetSeriesType(int seriesId, SeriesTypes seriesTypes); - void DeleteSeries(int seriesId); + void DeleteSeries(int seriesId, bool deleteFiles); } public class SeriesService : ISeriesService @@ -167,11 +167,11 @@ namespace NzbDrone.Core.Tv _seriesRepository.SetSeriesType(seriesId, seriesTypes); } - public void DeleteSeries(int seriesId) + public void DeleteSeries(int seriesId, bool deleteFiles) { var series = _seriesRepository.Get(seriesId); _seriesRepository.Delete(seriesId); - _eventAggregator.Publish(new SeriesDeletedEvent(series)); + _eventAggregator.Publish(new SeriesDeletedEvent(series, deleteFiles)); } } }