From 3766e0eba9e50b6b3a86722f8a08305c2be4dcfd Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 28 Apr 2019 11:50:18 -0700 Subject: [PATCH] New: Show health warning if system time is off expected time Co-Authored-By: Mark McDowall --- .../Cloud/RadarrCloudRequestBuilder.cs | 2 +- .../Checks/SystemTimeCheckFixture.cs | 49 +++++++++++++++++++ .../HealthCheck/Checks/SystemTimeCheck.cs | 49 +++++++++++++++++++ src/NzbDrone.Core/Localization/Core/en.json | 1 + 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core.Test/HealthCheck/Checks/SystemTimeCheckFixture.cs create mode 100644 src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs diff --git a/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs b/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs index 39d16483a..ce22ce009 100644 --- a/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs +++ b/src/NzbDrone.Common/Cloud/RadarrCloudRequestBuilder.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Common.Cloud { public RadarrCloudRequestBuilder() { - Services = new HttpRequestBuilder("https://radarr.lidarr.audio/v1/") + Services = new HttpRequestBuilder("https://radarr.servarr.com/v1/") .CreateFactory(); TMDB = new HttpRequestBuilder("https://api.themoviedb.org/{api}/{route}/{id}{secondaryRoute}") diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/SystemTimeCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/SystemTimeCheckFixture.cs new file mode 100644 index 000000000..e3885d652 --- /dev/null +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/SystemTimeCheckFixture.cs @@ -0,0 +1,49 @@ +using System; +using System.Text; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.Cloud; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.HealthCheck.Checks; +using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; + +namespace NzbDrone.Core.Test.HealthCheck.Checks +{ + [TestFixture] + public class SystemTimeCheckFixture : CoreTest + { + [SetUp] + public void Setup() + { + Mocker.SetConstant(new RadarrCloudRequestBuilder()); + } + + private void GivenServerTime(DateTime dateTime) + { + var json = new ServiceTimeResponse { DateTimeUtc = dateTime }.ToJson(); + + Mocker.GetMock() + .Setup(s => s.Execute(It.IsAny())) + .Returns(r => new HttpResponse(r, new HttpHeader(), Encoding.ASCII.GetBytes(json))); + } + + [Test] + public void should_not_return_error_when_system_time_is_close_to_server_time() + { + GivenServerTime(DateTime.UtcNow); + + Subject.Check().ShouldBeOk(); + } + + [Test] + public void should_return_error_when_system_time_is_more_than_one_day_from_server_time() + { + GivenServerTime(DateTime.UtcNow.AddDays(2)); + + Subject.Check().ShouldBeError(); + ExceptionVerification.ExpectedErrors(1); + } + } +} diff --git a/src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs new file mode 100644 index 000000000..36e913e69 --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs @@ -0,0 +1,49 @@ +using System; +using NLog; +using NzbDrone.Common.Cloud; +using NzbDrone.Common.Http; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Localization; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + public class SystemTimeCheck : HealthCheckBase + { + private readonly IHttpClient _client; + private readonly IHttpRequestBuilderFactory _cloudRequestBuilder; + private readonly Logger _logger; + + public SystemTimeCheck(IHttpClient client, IRadarrCloudRequestBuilder cloudRequestBuilder, ILocalizationService localizationService, Logger logger) + : base(localizationService) + { + _client = client; + _cloudRequestBuilder = cloudRequestBuilder.Services; + _logger = logger; + } + + public override HealthCheck Check() + { + var request = _cloudRequestBuilder.Create() + .Resource("/time") + .Build(); + + var response = _client.Execute(request); + var result = Json.Deserialize(response.Content); + var systemTime = DateTime.UtcNow; + + // +/- more than 1 day + if (Math.Abs(result.DateTimeUtc.Subtract(systemTime).TotalDays) >= 1) + { + _logger.Error("System time mismatch. SystemTime: {0} Expected Time: {1}. Update system time", systemTime, result.DateTimeUtc); + return new HealthCheck(GetType(), HealthCheckResult.Error, _localizationService.GetLocalizedString("SystemTimeCheckMessage")); + } + + return new HealthCheck(GetType()); + } + } + + public class ServiceTimeResponse + { + public DateTime DateTimeUtc { get; set; } + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 19485d0ce..3c008479f 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -239,6 +239,7 @@ "Studio": "Studio", "Style": "Style", "System": "System", + "SystemTimeCheckMessage": "System time is off by more than 1 day. Scheduled tasks may not run correctly until the time is corrected", "TableOptions": "Table Options", "TableOptionsColumnsMessage": "Choose which columns are visible and which order they appear in", "Tags": "Tags",