New: Show health warning if system time is off expected time

Co-Authored-By: Mark McDowall <markus101@users.noreply.github.com>
pull/4810/head
Mark McDowall 6 years ago committed by Qstick
parent f39824cb9c
commit 3766e0eba9

@ -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}")

@ -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<SystemTimeCheck>
{
[SetUp]
public void Setup()
{
Mocker.SetConstant<IRadarrCloudRequestBuilder>(new RadarrCloudRequestBuilder());
}
private void GivenServerTime(DateTime dateTime)
{
var json = new ServiceTimeResponse { DateTimeUtc = dateTime }.ToJson();
Mocker.GetMock<IHttpClient>()
.Setup(s => s.Execute(It.IsAny<HttpRequest>()))
.Returns<HttpRequest>(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);
}
}
}

@ -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<ServiceTimeResponse>(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; }
}
}

@ -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",

Loading…
Cancel
Save