diff --git a/src/NzbDrone.Core/HealthCheck/Checks/RemovedArtistCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/RemovedArtistCheck.cs new file mode 100644 index 000000000..2654b1d8c --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/RemovedArtistCheck.cs @@ -0,0 +1,53 @@ +using System.Linq; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Localization; +using NzbDrone.Core.Music; +using NzbDrone.Core.Music.Events; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + [CheckOn(typeof(ArtistUpdatedEvent))] + [CheckOn(typeof(ArtistsDeletedEvent), CheckOnCondition.FailedOnly)] + public class RemovedArtistCheck : HealthCheckBase, ICheckOnCondition, ICheckOnCondition + { + private readonly IArtistService _artistService; + private readonly Logger _logger; + + public RemovedArtistCheck(ILocalizationService localizationService, IArtistService artistService, Logger logger) + : base(localizationService) + { + _artistService = artistService; + _logger = logger; + } + + public override HealthCheck Check() + { + var deletedArtists = _artistService.GetAllArtists().Where(v => v.Metadata.Value.Status == ArtistStatusType.Deleted).ToList(); + + if (deletedArtists.Empty()) + { + return new HealthCheck(GetType()); + } + + var artistText = deletedArtists.Select(s => $"{s.Name} (mbid {s.ForeignArtistId})").Join(", "); + + if (deletedArtists.Count == 1) + { + return new HealthCheck(GetType(), HealthCheckResult.Error, $"Artist {artistText} was removed from MusicBrainz"); + } + + return new HealthCheck(GetType(), HealthCheckResult.Error, $"Artists {artistText} were removed from MusicBrainz"); + } + + public bool ShouldCheckOnEvent(ArtistsDeletedEvent deletedEvent) + { + return deletedEvent.Artists.Any(artist => artist.Metadata.Value.Status == ArtistStatusType.Deleted); + } + + public bool ShouldCheckOnEvent(ArtistUpdatedEvent updatedEvent) + { + return updatedEvent.Artist.Metadata.Value.Status == ArtistStatusType.Deleted; + } + } +} diff --git a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs index a9a89dc48..452286f47 100644 --- a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs +++ b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs @@ -136,7 +136,7 @@ private void ProcessHealthChecks() public void Execute(CheckHealthCommand message) { - var healthChecks = message.Trigger == CommandTrigger.Manual ? _healthChecks : _scheduledHealthChecks; + var healthChecks = message.Trigger == CommandTrigger.Manual ? _healthChecks : _scheduledHealthChecks; lock (_pendingHealthChecks) { diff --git a/src/NzbDrone.Core/Music/Model/ArtistStatusType.cs b/src/NzbDrone.Core/Music/Model/ArtistStatusType.cs index 478959016..1535c2dcc 100644 --- a/src/NzbDrone.Core/Music/Model/ArtistStatusType.cs +++ b/src/NzbDrone.Core/Music/Model/ArtistStatusType.cs @@ -2,6 +2,7 @@ namespace NzbDrone.Core.Music { public enum ArtistStatusType { + Deleted = -1, Continuing = 0, Ended = 1 } diff --git a/src/NzbDrone.Core/Music/Services/RefreshArtistService.cs b/src/NzbDrone.Core/Music/Services/RefreshArtistService.cs index 48223ecf8..2126e54bb 100644 --- a/src/NzbDrone.Core/Music/Services/RefreshArtistService.cs +++ b/src/NzbDrone.Core/Music/Services/RefreshArtistService.cs @@ -87,7 +87,15 @@ protected override RemoteData GetRemoteData(Artist local, List remote) } catch (ArtistNotFoundException) { - _logger.Error($"Could not find artist with id {local.Metadata.Value.ForeignArtistId}"); + if (local.Metadata.Value.Status != ArtistStatusType.Deleted) + { + local.Metadata.Value.Status = ArtistStatusType.Deleted; + _artistService.UpdateArtist(local); + _logger.Debug("Artist marked as deleted on MusicBrainz for {0}", local.Name); + _eventAggregator.PublishEvent(new ArtistUpdatedEvent(local)); + } + + _logger.Error($"Artist '{local.Name}' (mbid {local.Metadata.Value.ForeignArtistId}) was not found, it may have been removed from MusicBrainz."); } return result; diff --git a/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs b/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs index 26548d757..3a4eb2130 100644 --- a/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs +++ b/src/NzbDrone.Core/Music/Utilities/ShouldRefreshArtist.cs @@ -42,9 +42,9 @@ public bool ShouldRefresh(Artist artist) return false; } - if (artist.Metadata.Value.Status == ArtistStatusType.Continuing && artist.LastInfoSync < DateTime.UtcNow.AddDays(-2)) + if (artist.Metadata.Value.Status != ArtistStatusType.Ended && artist.LastInfoSync < DateTime.UtcNow.AddDays(-2)) { - _logger.Trace("Artist {0} is continuing and has not been refreshed in 2 days, should refresh.", artist.Name); + _logger.Trace("Artist {0} is not ended and has not been refreshed in 2 days, should refresh.", artist.Name); return true; } @@ -52,7 +52,7 @@ public bool ShouldRefresh(Artist artist) if (lastAlbum != null && lastAlbum.ReleaseDate > DateTime.UtcNow.AddDays(-30)) { - _logger.Trace("Last album in {0} aired less than 30 days ago, should refresh.", artist.Name); + _logger.Trace("Last album in {0} released less than 30 days ago, should refresh.", artist.Name); return true; }