mirror of
https://github.com/lidarr/Lidarr
synced 2025-01-03 05:25:10 +00:00
New: Show health warning if system time is off expected time
Closes #1422
This commit is contained in:
parent
e4d5e17cf0
commit
c3af687442
3 changed files with 97 additions and 1 deletions
|
@ -13,7 +13,7 @@ public class LidarrCloudRequestBuilder : ILidarrCloudRequestBuilder
|
|||
{
|
||||
public LidarrCloudRequestBuilder()
|
||||
{
|
||||
Services = new HttpRequestBuilder("https://services.lidarr.audio/v1/")
|
||||
Services = new HttpRequestBuilder("https://lidarr.servarr.com/v1/")
|
||||
.CreateFactory();
|
||||
|
||||
Search = new HttpRequestBuilder("https://api.lidarr.audio/api/v0.4/{route}")
|
||||
|
|
|
@ -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<ISonarrCloudRequestBuilder>(new SonarrCloudRequestBuilder());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
47
src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs
Normal file
47
src/NzbDrone.Core/HealthCheck/Checks/SystemTimeCheck.cs
Normal file
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Cloud;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.Serializer;
|
||||
|
||||
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, ILidarrCloudRequestBuilder cloudRequestBuilder, Logger logger)
|
||||
{
|
||||
_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, $"System time is off by more than 1 day. Scheduled tasks may not run correctly until the time is corrected");
|
||||
}
|
||||
|
||||
return new HealthCheck(GetType());
|
||||
}
|
||||
}
|
||||
|
||||
public class ServiceTimeResponse
|
||||
{
|
||||
public DateTime DateTimeUtc { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue