event based cleanup when a series is deleted.

This commit is contained in:
Keivan Beigi 2013-03-05 11:49:34 -08:00
parent cc1dcffdf3
commit 4877ce405e
11 changed files with 61 additions and 87 deletions

View File

@ -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 };
}
}

View File

@ -14,6 +14,7 @@ namespace NzbDrone.Core.Datastore
void Delete(int id);
IList<TModel> InsertMany(IList<TModel> model);
IList<TModel> UpdateMany(IList<TModel> model);
void DeleteMany(IList<TModel> model);
void Purge();
bool HasItems();
}
@ -64,6 +65,11 @@ namespace NzbDrone.Core.Datastore
return ObjectDatabase.UpdateMany(model);
}
public void DeleteMany(IList<TModel> model)
{
ObjectDatabase.DeleteMany(model);
}
public TModel Upsert(TModel model)
{
if (model.Id == 0)

View File

@ -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);
}
}
}
}

View File

@ -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<SeriesUpdatedEvent>
public class MediaCoverService :
IHandleAsync<SeriesUpdatedEvent>,
IHandleAsync<SeriesDeletedEvent>
{
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"));
}
}
}

View File

@ -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<Episode> episodes, Series series, Quality quality, bool proper, EpisodeFile episodeFile);
}
public class MediaFileService : IMediaFileService
public class MediaFileService : IMediaFileService, IHandleAsync<SeriesDeletedEvent>
{
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);
}
}
}

View File

@ -208,7 +208,6 @@
<Compile Include="Jobs\Implementations\BacklogSearchJob.cs" />
<Compile Include="Jobs\Implementations\CleanupRecycleBinJob.cs" />
<Compile Include="Jobs\Implementations\ConvertEpisodeJob.cs" />
<Compile Include="Jobs\Implementations\DeleteSeriesJob.cs" />
<Compile Include="Jobs\Implementations\DiskScanJob.cs" />
<Compile Include="Jobs\Implementations\EmptyRecycleBinJob.cs" />
<Compile Include="Jobs\Implementations\EpisodeSearchJob.cs" />

View File

@ -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<SeriesDeletedEvent>
{
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);
}
}
}

View File

@ -35,7 +35,9 @@ namespace NzbDrone.Core.Tv
List<Episode> EpisodesBetweenDates(DateTime start, DateTime end);
}
public class EpisodeService : IEpisodeService, IHandle<EpisodeGrabbedEvent>
public class EpisodeService : IEpisodeService,
IHandle<EpisodeGrabbedEvent>,
IHandleAsync<SeriesDeletedEvent>
{
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);
}
}
}

View File

@ -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;
}
}
}

Binary file not shown.

View File

@ -23,7 +23,7 @@ namespace NzbDrone.Core.Tv
void UpdateFromSeriesEditor(IList<Series> 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));
}
}
}