New: Automatic search for missing episodes if RSS Sync hasn't been run recently

pull/3113/head
Mark McDowall 11 years ago
parent 0f75a9008a
commit 192e79d2ff

@ -11,7 +11,12 @@ using NzbDrone.Core.Tv;
namespace NzbDrone.Core.IndexerSearch
{
public class MissingEpisodeSearchService : IExecute<EpisodeSearchCommand>, IExecute<MissingEpisodeSearchCommand>
public interface IEpisodeSearchService
{
void MissingEpisodesAiredAfter(DateTime dateTime);
}
public class MissingEpisodeSearchService : IEpisodeSearchService, IExecute<EpisodeSearchCommand>, IExecute<MissingEpisodeSearchCommand>
{
private readonly ISearchForNzb _nzbSearchService;
private readonly IDownloadApprovedReports _downloadApprovedReports;
@ -32,6 +37,26 @@ namespace NzbDrone.Core.IndexerSearch
_logger = logger;
}
public void MissingEpisodesAiredAfter(DateTime dateTime)
{
var missing = _episodeService.EpisodesBetweenDates(dateTime, DateTime.UtcNow)
.Where(e => !e.HasFile &&
!_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id))
.ToList();
var downloadedCount = 0;
_logger.Info("Searching for {0} missing episodes since last RSS Sync", missing.Count);
foreach (var episode in missing)
{
var decisions = _nzbSearchService.EpisodeSearch(episode);
var downloaded = _downloadApprovedReports.DownloadApproved(decisions);
downloadedCount += downloaded.Count;
}
_logger.ProgressInfo("Completed search for {0} episodes. {1} reports downloaded.", missing.Count, downloadedCount);
}
public void Execute(EpisodeSearchCommand message)
{
foreach (var episodeId in message.EpisodeIds)
@ -57,9 +82,9 @@ namespace NzbDrone.Core.IndexerSearch
FilterExpression = v => v.Monitored == true && v.Series.Monitored == true
}).Records.ToList();
var missing = episodes.Where(e => !_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id));
var missing = episodes.Where(e => !_queueService.GetQueue().Select(q => q.Episode.Id).Contains(e.Id)).ToList();
_logger.ProgressInfo("Performing missing search for {0} episodes", episodes.Count);
_logger.ProgressInfo("Performing missing search for {0} episodes", missing.Count);
var downloadedCount = 0;
//Limit requests to indexers at 100 per minute
@ -71,12 +96,10 @@ namespace NzbDrone.Core.IndexerSearch
var decisions = _nzbSearchService.EpisodeSearch(episode);
var downloaded = _downloadApprovedReports.DownloadApproved(decisions);
downloadedCount += downloaded.Count;
_logger.ProgressInfo("Episode search completed. {0} reports downloaded.", downloaded.Count);
}
}
_logger.ProgressInfo("Completed missing search for {0} episodes. {1} reports downloaded.", episodes.Count, downloadedCount);
_logger.ProgressInfo("Completed missing search for {0} episodes. {1} reports downloaded.", missing.Count, downloadedCount);
}
}
}

@ -1,7 +1,9 @@
using System.Linq;
using System;
using System.Linq;
using NLog;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
using NzbDrone.Core.IndexerSearch;
using NzbDrone.Core.Instrumentation.Extensions;
using NzbDrone.Core.Messaging.Commands;
@ -17,16 +19,19 @@ namespace NzbDrone.Core.Indexers
private readonly IFetchAndParseRss _rssFetcherAndParser;
private readonly IMakeDownloadDecision _downloadDecisionMaker;
private readonly IDownloadApprovedReports _downloadApprovedReports;
private readonly IEpisodeSearchService _episodeSearchService;
private readonly Logger _logger;
public RssSyncService(IFetchAndParseRss rssFetcherAndParser,
IMakeDownloadDecision downloadDecisionMaker,
IDownloadApprovedReports downloadApprovedReports,
IEpisodeSearchService episodeSearchService,
Logger logger)
{
_rssFetcherAndParser = rssFetcherAndParser;
_downloadDecisionMaker = downloadDecisionMaker;
_downloadApprovedReports = downloadApprovedReports;
_episodeSearchService = episodeSearchService;
_logger = logger;
}
@ -45,6 +50,12 @@ namespace NzbDrone.Core.Indexers
public void Execute(RssSyncCommand message)
{
Sync();
if (message.LastExecutionTime.HasValue && DateTime.UtcNow.Subtract(message.LastExecutionTime.Value).TotalHours > 3)
{
_logger.Info("RSS Sync hasn't run since: {0}. Searching for any missing episodes since then.", message.LastExecutionTime.Value);
_episodeSearchService.MissingEpisodesAiredAfter(message.LastExecutionTime.Value.AddDays(-1));
}
}
}
}

@ -43,7 +43,7 @@ namespace NzbDrone.Core.Jobs
try
{
_commandExecutor.PublishCommand(task.TypeName);
_commandExecutor.PublishCommand(task.TypeName, task.LastExecution);
}
catch (Exception e)
{

@ -35,6 +35,7 @@ namespace NzbDrone.Core.Messaging.Commands
public string Message { get; private set; }
public string Name { get; private set; }
public DateTime? LastExecutionTime { get; set; }
protected Command()
{

@ -49,8 +49,15 @@ namespace NzbDrone.Core.Messaging.Commands
}
public void PublishCommand(string commandTypeName)
{
PublishCommand(commandTypeName, null);
}
public void PublishCommand(string commandTypeName, DateTime? lastExecutionTime)
{
dynamic command = GetCommand(commandTypeName);
command.LastExecutionTime = lastExecutionTime;
PublishCommand(command);
}

@ -1,9 +1,12 @@
using System;
namespace NzbDrone.Core.Messaging.Commands
{
public interface ICommandExecutor
{
void PublishCommand<TCommand>(TCommand command) where TCommand : Command;
void PublishCommand(string commandTypeName);
void PublishCommand(string commandTypeName, DateTime? lastEecutionTime);
Command PublishCommandAsync<TCommand>(TCommand command) where TCommand : Command;
Command PublishCommandAsync(string commandTypeName);
}

Loading…
Cancel
Save