From 0ef7c3420bd660a786dd490d088877d148765bc2 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 1 Jul 2023 13:36:17 -0500 Subject: [PATCH] New: Added health check warning if SABnzbd sorting is enabled Fixes #3266 Fixes #3792 Co-Authored-By: Mark McDowall --- .../DownloadClientSortingCheckFixture.cs | 77 +++++++++++++++++++ .../Download/Clients/Sabnzbd/Sabnzbd.cs | 13 ++++ .../Download/DownloadClientInfo.cs | 1 + .../Checks/DownloadClientSortingCheck.cs | 62 +++++++++++++++ src/NzbDrone.Core/Localization/Core/en.json | 1 + 5 files changed, 154 insertions(+) create mode 100644 src/NzbDrone.Core.Test/HealthCheck/Checks/DownloadClientSortingCheckFixture.cs create mode 100644 src/NzbDrone.Core/HealthCheck/Checks/DownloadClientSortingCheck.cs diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/DownloadClientSortingCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/DownloadClientSortingCheckFixture.cs new file mode 100644 index 000000000..24a7ef890 --- /dev/null +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/DownloadClientSortingCheckFixture.cs @@ -0,0 +1,77 @@ +using System; +using Moq; +using NUnit.Framework; +using NzbDrone.Core.Download; +using NzbDrone.Core.Download.Clients; +using NzbDrone.Core.HealthCheck.Checks; +using NzbDrone.Core.Localization; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; + +namespace NzbDrone.Core.Test.HealthCheck.Checks +{ + [TestFixture] + public class DownloadClientFolderCheckFixture : CoreTest + { + private DownloadClientInfo _clientStatus; + private Mock _downloadClient; + + private static Exception[] DownloadClientExceptions = + { + new DownloadClientUnavailableException("error"), + new DownloadClientAuthenticationException("error"), + new DownloadClientException("error") + }; + + [SetUp] + public void Setup() + { + Mocker.GetMock() + .Setup(s => s.GetLocalizedString(It.IsAny())) + .Returns("Some Warning Message"); + + _clientStatus = new DownloadClientInfo + { + IsLocalhost = true, + SortingMode = null + }; + + _downloadClient = Mocker.GetMock(); + _downloadClient.Setup(s => s.Definition) + .Returns(new DownloadClientDefinition { Name = "Test" }); + + _downloadClient.Setup(s => s.GetStatus()) + .Returns(_clientStatus); + + Mocker.GetMock() + .Setup(s => s.GetDownloadClients(It.IsAny())) + .Returns(new IDownloadClient[] { _downloadClient.Object }); + } + + [Test] + public void should_return_ok_if_sorting_is_not_enabled() + { + Subject.Check().ShouldBeOk(); + } + + [Test] + public void should_return_warning_if_sorting_is_enabled() + { + _clientStatus.SortingMode = "TV"; + + Subject.Check().ShouldBeWarning(); + } + + [Test] + [TestCaseSource("DownloadClientExceptions")] + public void should_return_ok_if_client_throws_downloadclientexception(Exception ex) + { + _downloadClient.Setup(s => s.GetStatus()) + .Throws(ex); + + Subject.Check().ShouldBeOk(); + + ExceptionVerification.ExpectedErrors(0); + } + } +} diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs index e73310d01..8804190ea 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs @@ -260,6 +260,19 @@ namespace NzbDrone.Core.Download.Clients.Sabnzbd if (category != null) { + if (config.Misc.enable_tv_sorting && ContainsCategory(config.Misc.tv_categories, Settings.MusicCategory)) + { + status.SortingMode = "TV"; + } + else if (config.Misc.enable_movie_sorting && ContainsCategory(config.Misc.movie_categories, Settings.MusicCategory)) + { + status.SortingMode = "Movie"; + } + else if (config.Misc.enable_date_sorting && ContainsCategory(config.Misc.date_categories, Settings.MusicCategory)) + { + status.SortingMode = "Date"; + } + status.OutputRootFolders = new List { _remotePathMappingService.RemapRemoteToLocal(Settings.Host, category.FullPath) }; } diff --git a/src/NzbDrone.Core/Download/DownloadClientInfo.cs b/src/NzbDrone.Core/Download/DownloadClientInfo.cs index 734283ca4..686258520 100644 --- a/src/NzbDrone.Core/Download/DownloadClientInfo.cs +++ b/src/NzbDrone.Core/Download/DownloadClientInfo.cs @@ -11,6 +11,7 @@ namespace NzbDrone.Core.Download } public bool IsLocalhost { get; set; } + public string SortingMode { get; set; } public List OutputRootFolders { get; set; } } } diff --git a/src/NzbDrone.Core/HealthCheck/Checks/DownloadClientSortingCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/DownloadClientSortingCheck.cs new file mode 100644 index 000000000..c7dc81c9f --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/DownloadClientSortingCheck.cs @@ -0,0 +1,62 @@ +using System; +using NLog; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Datastore.Events; +using NzbDrone.Core.Download; +using NzbDrone.Core.Download.Clients; +using NzbDrone.Core.Localization; +using NzbDrone.Core.RemotePathMappings; +using NzbDrone.Core.RootFolders; +using NzbDrone.Core.ThingiProvider.Events; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + [CheckOn(typeof(ProviderUpdatedEvent))] + [CheckOn(typeof(ProviderDeletedEvent))] + [CheckOn(typeof(ModelEvent))] + [CheckOn(typeof(ModelEvent))] + + public class DownloadClientSortingCheck : HealthCheckBase + { + private readonly IProvideDownloadClient _downloadClientProvider; + private readonly Logger _logger; + + public DownloadClientSortingCheck(IProvideDownloadClient downloadClientProvider, + Logger logger, + ILocalizationService localizationService) + : base(localizationService) + { + _downloadClientProvider = downloadClientProvider; + _logger = logger; + } + + public override HealthCheck Check() + { + var clients = _downloadClientProvider.GetDownloadClients(true); + + foreach (var client in clients) + { + try + { + var clientName = client.Definition.Name; + var status = client.GetStatus(); + + if (status.SortingMode.IsNotNullOrWhiteSpace()) + { + return new HealthCheck(GetType(), HealthCheckResult.Warning, string.Format(_localizationService.GetLocalizedString("DownloadClientSortingCheckMessage"), clientName, status.SortingMode), "#download-folder-and-library-folder-not-different-folders"); + } + } + catch (DownloadClientException ex) + { + _logger.Debug(ex, "Unable to communicate with {0}", client.Definition.Name); + } + catch (Exception ex) + { + _logger.Error(ex, "Unknown error occurred in DownloadClientSortingCheck HealthCheck"); + } + } + + return new HealthCheck(GetType()); + } + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 14bcd5b46..5395b4d77 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -252,6 +252,7 @@ "DownloadClientCheckNoneAvailableMessage": "No download client is available", "DownloadClientCheckUnableToCommunicateMessage": "Unable to communicate with {0}.", "DownloadClientSettings": "Download Client Settings", + "DownloadClientSortingCheckMessage": "Download client {0} has {1} sorting enabled for Lidarr's category. You should disable sorting in your download client to avoid import issues.", "DownloadClientStatusCheckAllClientMessage": "All download clients are unavailable due to failures", "DownloadClientStatusCheckSingleClientMessage": "Download clients unavailable due to failures: {0}", "DownloadClients": "Download Clients",