From e78b8d534652dc9dfdf6b0a9dfec59d0c74de648 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sun, 9 Oct 2022 10:15:50 -0500 Subject: [PATCH] New: Add long term Application status Healthcheck --- .../Checks/ApplicationLongTermStatusCheck.cs | 59 +++++++++++++++++++ src/NzbDrone.Core/Localization/Core/en.json | 2 + 2 files changed, 61 insertions(+) create mode 100644 src/NzbDrone.Core/HealthCheck/Checks/ApplicationLongTermStatusCheck.cs diff --git a/src/NzbDrone.Core/HealthCheck/Checks/ApplicationLongTermStatusCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/ApplicationLongTermStatusCheck.cs new file mode 100644 index 000000000..0ea4bbd3c --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/ApplicationLongTermStatusCheck.cs @@ -0,0 +1,59 @@ +using System; +using System.Linq; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Applications; +using NzbDrone.Core.Localization; +using NzbDrone.Core.ThingiProvider.Events; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + [CheckOn(typeof(ProviderUpdatedEvent))] + [CheckOn(typeof(ProviderDeletedEvent))] + [CheckOn(typeof(ProviderStatusChangedEvent))] + public class ApplicationLongTermStatusCheck : HealthCheckBase + { + private readonly IApplicationFactory _providerFactory; + private readonly IApplicationStatusService _providerStatusService; + + public ApplicationLongTermStatusCheck(IApplicationFactory providerFactory, + IApplicationStatusService providerStatusService, + ILocalizationService localizationService) + : base(localizationService) + { + _providerFactory = providerFactory; + _providerStatusService = providerStatusService; + } + + public override HealthCheck Check() + { + var enabledProviders = _providerFactory.GetAvailableProviders(); + var backOffProviders = enabledProviders.Join(_providerStatusService.GetBlockedProviders(), + i => i.Definition.Id, + s => s.ProviderId, + (i, s) => new { Provider = i, Status = s }) + .Where(p => p.Status.InitialFailure.HasValue && + p.Status.InitialFailure.Value.Before( + DateTime.UtcNow.AddHours(-6))) + .ToList(); + + if (backOffProviders.Empty()) + { + return new HealthCheck(GetType()); + } + + if (backOffProviders.Count == enabledProviders.Count) + { + return new HealthCheck(GetType(), + HealthCheckResult.Error, + _localizationService.GetLocalizedString("ApplicationLongTermStatusCheckAllClientMessage"), + "#applications-are-unavailable-due-to-failures"); + } + + return new HealthCheck(GetType(), + HealthCheckResult.Warning, + string.Format(_localizationService.GetLocalizedString("ApplicationLongTermStatusCheckSingleClientMessage"), + string.Join(", ", backOffProviders.Select(v => v.Provider.Definition.Name))), + "#applications-are-unavailable-due-to-failures"); + } + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 8411402df..79ea9e092 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -23,6 +23,8 @@ "AppDataDirectory": "AppData directory", "AppDataLocationHealthCheckMessage": "Updating will not be possible to prevent deleting AppData on Update", "Application": "Application", + "ApplicationLongTermStatusCheckAllClientMessage": "All applications are unavailable due to failures for more than 6 hours", + "ApplicationLongTermStatusCheckSingleClientMessage": "Applications unavailable due to failures for more than 6 hours: {0}", "Applications": "Applications", "ApplicationStatusCheckAllClientMessage": "All applications are unavailable due to failures", "ApplicationStatusCheckSingleClientMessage": "Applications unavailable due to failures: {0}",