diff --git a/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs index fcf9d58c8..74802a39e 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs @@ -19,6 +19,10 @@ namespace NzbDrone.Core.Test.HealthCheck Mocker.SetConstant>(new[] { _healthCheck }); Mocker.SetConstant(Mocker.Resolve()); + + Mocker.GetMock() + .Setup(v => v.GetServerChecks()) + .Returns(new List()); } [Test] diff --git a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs index d4aa2f314..28226504e 100644 --- a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs +++ b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs @@ -25,6 +25,7 @@ namespace NzbDrone.Core.HealthCheck private readonly IProvideHealthCheck[] _startupHealthChecks; private readonly IProvideHealthCheck[] _scheduledHealthChecks; private readonly Dictionary _eventDrivenHealthChecks; + private readonly IServerSideNotificationService _serverSideNotificationService; private readonly IEventAggregator _eventAggregator; private readonly ICacheManager _cacheManager; private readonly Logger _logger; @@ -32,11 +33,13 @@ namespace NzbDrone.Core.HealthCheck private readonly ICached _healthCheckResults; public HealthCheckService(IEnumerable healthChecks, + IServerSideNotificationService serverSideNotificationService, IEventAggregator eventAggregator, ICacheManager cacheManager, Logger logger) { _healthChecks = healthChecks.ToArray(); + _serverSideNotificationService = serverSideNotificationService; _eventAggregator = eventAggregator; _cacheManager = cacheManager; _logger = logger; @@ -83,6 +86,8 @@ namespace NzbDrone.Core.HealthCheck } } + results.AddRange(_serverSideNotificationService.GetServerChecks()); + foreach (var result in results) { if (result.Type == HealthCheckResult.Ok) diff --git a/src/NzbDrone.Core/HealthCheck/ServerSideNotificationService.cs b/src/NzbDrone.Core/HealthCheck/ServerSideNotificationService.cs new file mode 100644 index 000000000..139f95e82 --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/ServerSideNotificationService.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using NLog; +using NzbDrone.Common.Cloud; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Configuration; + +namespace NzbDrone.Core.HealthCheck +{ + public interface IServerSideNotificationService + { + public List GetServerChecks(); + } + + public class ServerSideNotificationService : IServerSideNotificationService + { + private readonly IHttpClient _client; + private readonly IConfigFileProvider _configFileProvider; + private readonly IHttpRequestBuilderFactory _cloudRequestBuilder; + private readonly Logger _logger; + + public ServerSideNotificationService(IHttpClient client, IConfigFileProvider configFileProvider, ILidarrCloudRequestBuilder cloudRequestBuilder, Logger logger) + { + _client = client; + _configFileProvider = configFileProvider; + _cloudRequestBuilder = cloudRequestBuilder.Services; + _logger = logger; + } + + public List GetServerChecks() + { + var request = _cloudRequestBuilder.Create() + .Resource("/notification") + .AddQueryParam("version", BuildInfo.Version) + .AddQueryParam("os", OsInfo.Os.ToString().ToLowerInvariant()) + .AddQueryParam("arch", RuntimeInformation.OSArchitecture) + .AddQueryParam("runtime", PlatformInfo.Platform.ToString().ToLowerInvariant()) + .AddQueryParam("branch", _configFileProvider.Branch) + .Build(); + try + { + _logger.Trace("Getting server side health notifications"); + var response = _client.Execute(request); + var result = Json.Deserialize>(response.Content); + return result.Select(x => new HealthCheck(GetType(), x.Type, x.Message, x.WikiUrl)).ToList(); + } + catch (Exception ex) + { + _logger.Error(ex, "Failed to retrieve server notifications"); + return new List(); + } + } + } + + public class ServerNotificationResponse + { + public HealthCheckResult Type { get; set; } + public string Message { get; set; } + public string WikiUrl { get; set; } + } +}