New: Selectively refresh albums (#215)

* New: Selectively refresh albums

Closes #211
pull/221/head
Qstick 7 years ago committed by GitHub
parent 54ca73f474
commit 8395999696
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -43,6 +43,10 @@ namespace NzbDrone.Core.Test.MusicTests
Mocker.GetMock<IProvideAlbumInfo>() Mocker.GetMock<IProvideAlbumInfo>()
.Setup(s => s.GetAlbumInfo(It.IsAny<string>(), It.IsAny<string>())) .Setup(s => s.GetAlbumInfo(It.IsAny<string>(), It.IsAny<string>()))
.Callback(() => { throw new AlbumNotFoundException(album1.ForeignAlbumId); }); .Callback(() => { throw new AlbumNotFoundException(album1.ForeignAlbumId); });
Mocker.GetMock<ICheckIfAlbumShouldBeRefreshed>()
.Setup(s => s.ShouldRefresh(It.IsAny<Album>()))
.Returns(true);
} }
private void GivenNewAlbumInfo(Album album) private void GivenNewAlbumInfo(Album album)

@ -0,0 +1,103 @@
using System;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Music;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MusicTests
{
[TestFixture]
public class ShouldRefreshAlbumFixture : TestBase<ShouldRefreshAlbum>
{
private Album _album;
[SetUp]
public void Setup()
{
_album = Builder<Album>.CreateNew()
.With(e=>e.ReleaseDate = DateTime.Today.AddDays(-100))
.Build();
}
private void GivenAlbumLastRefreshedMonthsAgo()
{
_album.LastInfoSync = DateTime.UtcNow.AddDays(-90);
}
private void GivenAlbumLastRefreshedYesterday()
{
_album.LastInfoSync = DateTime.UtcNow.AddDays(-1);
}
private void GivenAlbumLastRefreshedRecently()
{
_album.LastInfoSync = DateTime.UtcNow.AddHours(-1);
}
private void GivenRecentlyReleased()
{
_album.ReleaseDate = DateTime.Today.AddDays(-7);
}
private void GivenFutureRelease()
{
_album.ReleaseDate = DateTime.Today.AddDays(7);
}
[Test]
public void should_return_false_if_album_last_refreshed_less_than_6_hours_ago()
{
GivenAlbumLastRefreshedRecently();
Subject.ShouldRefresh(_album).Should().BeFalse();
}
[Test]
public void should_return_true_if_album_last_refreshed_more_than_30_days_ago()
{
GivenAlbumLastRefreshedMonthsAgo();
Subject.ShouldRefresh(_album).Should().BeTrue();
}
[Test]
public void should_return_true_if_album_released_in_last_30_days()
{
GivenAlbumLastRefreshedYesterday();
GivenRecentlyReleased();
Subject.ShouldRefresh(_album).Should().BeTrue();
}
[Test]
public void should_return_true_if_album_releases_in_future()
{
GivenAlbumLastRefreshedYesterday();
GivenFutureRelease();
Subject.ShouldRefresh(_album).Should().BeTrue();
}
[Test]
public void should_return_false_when_recently_refreshed_album_released_over_30_days_ago()
{
GivenAlbumLastRefreshedYesterday();
Subject.ShouldRefresh(_album).Should().BeFalse();
}
[Test]
public void should_return_false_when_recently_refreshed_album_released_in_last_30_days()
{
GivenAlbumLastRefreshedRecently();
GivenRecentlyReleased();
Subject.ShouldRefresh(_album).Should().BeFalse();
}
}
}

@ -298,6 +298,7 @@
<Compile Include="MusicTests\ArtistServiceTests\AddArtistFixture.cs" /> <Compile Include="MusicTests\ArtistServiceTests\AddArtistFixture.cs" />
<Compile Include="MusicTests\ArtistServiceTests\UpdateMultipleArtistFixture.cs" /> <Compile Include="MusicTests\ArtistServiceTests\UpdateMultipleArtistFixture.cs" />
<Compile Include="MusicTests\RefreshAlbumServiceFixture.cs" /> <Compile Include="MusicTests\RefreshAlbumServiceFixture.cs" />
<Compile Include="MusicTests\ShouldRefreshAlbumFixture.cs" />
<Compile Include="NotificationTests\NotificationBaseFixture.cs" /> <Compile Include="NotificationTests\NotificationBaseFixture.cs" />
<Compile Include="NotificationTests\SynologyIndexerFixture.cs" /> <Compile Include="NotificationTests\SynologyIndexerFixture.cs" />
<Compile Include="OrganizerTests\FileNameBuilderTests\CleanTitleFixture.cs" /> <Compile Include="OrganizerTests\FileNameBuilderTests\CleanTitleFixture.cs" />

@ -30,6 +30,7 @@ namespace NzbDrone.Core.Music
private readonly IProvideAlbumInfo _albumInfo; private readonly IProvideAlbumInfo _albumInfo;
private readonly IRefreshTrackService _refreshTrackService; private readonly IRefreshTrackService _refreshTrackService;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly ICheckIfAlbumShouldBeRefreshed _checkIfAlbumShouldBeRefreshed;
private readonly Logger _logger; private readonly Logger _logger;
public RefreshAlbumService(IAlbumService albumService, public RefreshAlbumService(IAlbumService albumService,
@ -37,6 +38,7 @@ namespace NzbDrone.Core.Music
IProvideAlbumInfo albumInfo, IProvideAlbumInfo albumInfo,
IRefreshTrackService refreshTrackService, IRefreshTrackService refreshTrackService,
IEventAggregator eventAggregator, IEventAggregator eventAggregator,
ICheckIfAlbumShouldBeRefreshed checkIfAlbumShouldBeRefreshed,
Logger logger) Logger logger)
{ {
_albumService = albumService; _albumService = albumService;
@ -44,6 +46,7 @@ namespace NzbDrone.Core.Music
_albumInfo = albumInfo; _albumInfo = albumInfo;
_refreshTrackService = refreshTrackService; _refreshTrackService = refreshTrackService;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_checkIfAlbumShouldBeRefreshed = checkIfAlbumShouldBeRefreshed;
_logger = logger; _logger = logger;
} }
@ -51,7 +54,10 @@ namespace NzbDrone.Core.Music
{ {
foreach (var album in albums) foreach (var album in albums)
{ {
RefreshAlbumInfo(album); if (_checkIfAlbumShouldBeRefreshed.ShouldRefresh(album))
{
RefreshAlbumInfo(album);
}
} }
} }

@ -0,0 +1,44 @@
using NLog;
using System;
namespace NzbDrone.Core.Music
{
public interface ICheckIfAlbumShouldBeRefreshed
{
bool ShouldRefresh(Album album);
}
public class ShouldRefreshAlbum : ICheckIfAlbumShouldBeRefreshed
{
private readonly Logger _logger;
public ShouldRefreshAlbum(Logger logger)
{
_logger = logger;
}
public bool ShouldRefresh(Album album)
{
if (album.LastInfoSync < DateTime.UtcNow.AddDays(-30))
{
_logger.Trace("Album {0} last updated more than 30 days ago, should refresh.", album.Title);
return true;
}
if (album.LastInfoSync >= DateTime.UtcNow.AddHours(-6))
{
_logger.Trace("Album {0} last updated less than 6 hours ago, should not be refreshed.", album.Title);
return false;
}
if (album.ReleaseDate > DateTime.UtcNow.AddDays(-30))
{
_logger.Trace("album {0} released less than 30 days ago, should refresh.", album.Title);
return true;
}
_logger.Trace("Album {0} released long ago and recently refreshed, should not be refreshed.", album.Title);
return false;
}
}
}

@ -822,6 +822,7 @@
<Compile Include="Music\MoveArtistService.cs" /> <Compile Include="Music\MoveArtistService.cs" />
<Compile Include="Music\ArtistScannedHandler.cs" /> <Compile Include="Music\ArtistScannedHandler.cs" />
<Compile Include="Music\ArtistEditedService.cs" /> <Compile Include="Music\ArtistEditedService.cs" />
<Compile Include="Music\ShouldRefreshAlbum.cs" />
<Compile Include="Music\TrackMonitoredService.cs" /> <Compile Include="Music\TrackMonitoredService.cs" />
<Compile Include="Music\Member.cs" /> <Compile Include="Music\Member.cs" />
<Compile Include="Music\AddArtistOptions.cs" /> <Compile Include="Music\AddArtistOptions.cs" />

Loading…
Cancel
Save