Blacklist cleanup

Fixed: Cleanup blacklist when series is deleted
Fixed: Cleanup blacklist on startup
pull/2/head
Mark McDowall 11 years ago
parent 6d1cb90723
commit 4515c1d155

@ -1,4 +1,7 @@
using NzbDrone.Core.Datastore;
using System;
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Messaging.Events;
namespace NzbDrone.Core.Blacklisting
@ -6,6 +9,7 @@ namespace NzbDrone.Core.Blacklisting
public interface IBlacklistRepository : IBasicRepository<Blacklist>
{
bool Blacklisted(string sourceTitle);
List<Blacklist> BlacklistedBySeries(int seriesId);
}
public class BlacklistRepository : BasicRepository<Blacklist>, IBlacklistRepository
@ -17,7 +21,12 @@ namespace NzbDrone.Core.Blacklisting
public bool Blacklisted(string sourceTitle)
{
return Query.Any(e => e.SourceTitle.Contains(sourceTitle));
return Query.Where(e => e.SourceTitle.Contains(sourceTitle)).Any();
}
public List<Blacklist> BlacklistedBySeries(int seriesId)
{
return Query.Where(b => b.SeriesId == seriesId);
}
}
}

@ -3,6 +3,7 @@ using NzbDrone.Core.Datastore;
using NzbDrone.Core.Download;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.Blacklisting
{
@ -13,7 +14,7 @@ namespace NzbDrone.Core.Blacklisting
void Delete(int id);
}
public class BlacklistService : IBlacklistService, IHandle<DownloadFailedEvent>, IExecute<ClearBlacklistCommand>
public class BlacklistService : IBlacklistService, IExecute<ClearBlacklistCommand>, IHandle<DownloadFailedEvent>, IHandle<SeriesDeletedEvent>
{
private readonly IBlacklistRepository _blacklistRepository;
private readonly IRedownloadFailedDownloads _redownloadFailedDownloadService;
@ -39,6 +40,11 @@ namespace NzbDrone.Core.Blacklisting
_blacklistRepository.Delete(id);
}
public void Execute(ClearBlacklistCommand message)
{
_blacklistRepository.Purge();
}
public void Handle(DownloadFailedEvent message)
{
var blacklist = new Blacklist
@ -55,9 +61,11 @@ namespace NzbDrone.Core.Blacklisting
_redownloadFailedDownloadService.Redownload(message.SeriesId, message.EpisodeIds);
}
public void Execute(ClearBlacklistCommand message)
public void Handle(SeriesDeletedEvent message)
{
_blacklistRepository.Purge();
var blacklisted = _blacklistRepository.BlacklistedBySeries(message.Series.Id);
_blacklistRepository.DeleteMany(blacklisted);
}
}
}

@ -0,0 +1,31 @@
using NLog;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Housekeeping.Housekeepers
{
public class CleanupOrphanedBlacklist : IHousekeepingTask
{
private readonly IDatabase _database;
private readonly Logger _logger;
public CleanupOrphanedBlacklist(IDatabase database, Logger logger)
{
_database = database;
_logger = logger;
}
public void Clean()
{
_logger.Trace("Running orphaned blacklist cleanup");
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
WHERE Id IN (
SELECT Blacklist.Id FROM Blacklist
LEFT OUTER JOIN Series
ON Blacklist.SeriesId = Series.Id
WHERE Series.Id IS NULL)");
}
}
}

@ -283,6 +283,7 @@
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedHistoryItems.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedMetadataFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupAdditionalNamingSpecs.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedBlacklist.cs" />
<Compile Include="Housekeeping\Housekeepers\UpdateCleanTitleForSeries.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedEpisodeFiles.cs" />
<Compile Include="Housekeeping\Housekeepers\FixFutureRunScheduledTasks.cs" />

Loading…
Cancel
Save