Fixed: Ended series will be refreshed from trakt every 30 days instead of daily

This commit is contained in:
Mark McDowall 2014-03-20 21:15:01 -07:00
parent 87e5d1d84f
commit c6e492af4e
6 changed files with 175 additions and 5 deletions

View File

@ -231,6 +231,7 @@
<Compile Include="TvTests\SeriesRepositoryTests\QualityProfileRepositoryFixture.cs" />
<Compile Include="TvTests\SeriesServiceTests\UpdateMultipleSeriesFixture.cs" />
<Compile Include="TvTests\SeriesServiceTests\UpdateSeriesFixture.cs" />
<Compile Include="TvTests\ShouldRefreshSeriesFixture.cs" />
<Compile Include="UpdateTests\UpdateServiceFixture.cs" />
<Compile Include="DecisionEngineTests\AcceptableSizeSpecificationFixture.cs" />
<Compile Include="Qualities\QualityDefinitionServiceFixture.cs" />

View File

@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.TvTests
{
[TestFixture]
public class ShouldRefreshSeriesFixture : TestBase<ShouldRefreshSeries>
{
private Series _series;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew()
.Build();
}
private void GivenSeriesIsEnded()
{
_series.Status = SeriesStatusType.Ended;
}
private void GivenSeriesLastRefreshedRecently()
{
_series.LastInfoSync = DateTime.UtcNow.AddDays(-1);
}
[Test]
public void should_return_true_if_series_is_continuing()
{
_series.Status = SeriesStatusType.Continuing;
Subject.ShouldRefresh(_series).Should().BeTrue();
}
[Test]
public void should_return_true_if_series_last_refreshed_more_than_30_days_ago()
{
GivenSeriesIsEnded();
_series.LastInfoSync = DateTime.UtcNow.AddDays(-100);
Subject.ShouldRefresh(_series).Should().BeTrue();
}
[Test]
public void should_should_return_true_if_episode_aired_in_last_30_days()
{
Mocker.GetMock<IEpisodeService>()
.Setup(s => s.GetEpisodeBySeries(_series.Id))
.Returns(Builder<Episode>.CreateListOfSize(2)
.TheFirst(1)
.With(e => e.AirDateUtc = DateTime.Today.AddDays(-7))
.TheLast(1)
.With(e => e.AirDateUtc = DateTime.Today.AddDays(-100))
.Build()
.ToList());
Subject.ShouldRefresh(_series).Should().BeTrue();
}
[Test]
public void should_should_return_false_when_recently_refreshed_ended_show_has_not_aired_for_30_days()
{
Mocker.GetMock<IEpisodeService>()
.Setup(s => s.GetEpisodeBySeries(_series.Id))
.Returns(Builder<Episode>.CreateListOfSize(2)
.All()
.With(e => e.AirDateUtc = DateTime.Today.AddDays(-100))
.Build()
.ToList());
Subject.ShouldRefresh(_series).Should().BeTrue();
}
}
}

View File

@ -13,5 +13,14 @@ namespace NzbDrone.Core.MediaFiles.Commands
return true;
}
}
public RescanSeriesCommand()
{
}
public RescanSeriesCommand(int seriesId)
{
SeriesId = seriesId;
}
}
}

View File

@ -670,6 +670,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tv\SeriesStatusType.cs" />
<Compile Include="Tv\RefreshSeriesService.cs" />
<Compile Include="Tv\ShouldRefreshSeries.cs" />
<Compile Include="Update\Commands\ApplicationUpdateCommand.cs" />
<Compile Include="Update\Commands\InstallUpdateCommand.cs" />
<Compile Include="Update\InstallUpdateService.cs" />

View File

@ -5,6 +5,7 @@ using System.Linq;
using NLog;
using NzbDrone.Core.DataAugmentation.DailySeries;
using NzbDrone.Core.Instrumentation.Extensions;
using NzbDrone.Core.MediaFiles.Commands;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.MetadataSource;
@ -21,15 +22,26 @@ namespace NzbDrone.Core.Tv
private readonly IRefreshEpisodeService _refreshEpisodeService;
private readonly IEventAggregator _eventAggregator;
private readonly IDailySeriesService _dailySeriesService;
private readonly ICommandExecutor _commandExecutor;
private readonly ICheckIfSeriesShouldBeRefreshed _checkIfSeriesShouldBeRefreshed;
private readonly Logger _logger;
public RefreshSeriesService(IProvideSeriesInfo seriesInfo, ISeriesService seriesService, IRefreshEpisodeService refreshEpisodeService, IEventAggregator eventAggregator, IDailySeriesService dailySeriesService, Logger logger)
public RefreshSeriesService(IProvideSeriesInfo seriesInfo,
ISeriesService seriesService,
IRefreshEpisodeService refreshEpisodeService,
IEventAggregator eventAggregator,
IDailySeriesService dailySeriesService,
ICommandExecutor commandExecutor,
ICheckIfSeriesShouldBeRefreshed checkIfSeriesShouldBeRefreshed,
Logger logger)
{
_seriesInfo = seriesInfo;
_seriesService = seriesService;
_refreshEpisodeService = refreshEpisodeService;
_eventAggregator = eventAggregator;
_dailySeriesService = dailySeriesService;
_commandExecutor = commandExecutor;
_checkIfSeriesShouldBeRefreshed = checkIfSeriesShouldBeRefreshed;
_logger = logger;
}
@ -116,13 +128,29 @@ namespace NzbDrone.Core.Tv
foreach (var series in allSeries)
{
try
if (_checkIfSeriesShouldBeRefreshed.ShouldRefresh(series))
{
RefreshSeriesInfo(series);
try
{
RefreshSeriesInfo(series);
}
catch (Exception e)
{
_logger.ErrorException("Couldn't refresh info for {0}".Inject(series), e);
}
}
catch (Exception e)
else
{
_logger.ErrorException("Couldn't refresh info for {0}".Inject(series), e);
try
{
_logger.Info("Skipping refresh of series: {0}", series.Title);
_commandExecutor.PublishCommand(new RescanSeriesCommand(series.Id));
}
catch (Exception e)
{
_logger.ErrorException("Couldn't rescan series {0}".Inject(series), e);
}
}
}
}

View File

@ -0,0 +1,49 @@
using System;
using System.Linq;
using NLog;
namespace NzbDrone.Core.Tv
{
public interface ICheckIfSeriesShouldBeRefreshed
{
bool ShouldRefresh(Series series);
}
public class ShouldRefreshSeries : ICheckIfSeriesShouldBeRefreshed
{
private readonly IEpisodeService _episodeService;
private readonly Logger _logger;
public ShouldRefreshSeries(IEpisodeService episodeService, Logger logger)
{
_episodeService = episodeService;
_logger = logger;
}
public bool ShouldRefresh(Series series)
{
if (series.Status == SeriesStatusType.Continuing)
{
_logger.Trace("Series {0} is continuing, should refresh.", series.Title);
return true;
}
if (series.LastInfoSync < DateTime.UtcNow.AddDays(-30))
{
_logger.Trace("Series {0} last updated more than 30 days ago, should refresh.", series.Title);
return true;
}
var lastEpisode = _episodeService.GetEpisodeBySeries(series.Id).OrderByDescending(e => e.AirDateUtc).FirstOrDefault();
if (lastEpisode != null && lastEpisode.AirDateUtc > DateTime.UtcNow.AddDays(-30))
{
_logger.Trace("Last episode in {0} aired less than 30 days ago, should refresh.", series.Title);
return true;
}
_logger.Trace("Series {0} should not be refreshed.", series.Title);
return false;
}
}
}