1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

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

Closes #1422
This commit is contained in:
Mark McDowall 2019-04-28 11:50:18 -07:00
parent 949d764638
commit 05e17b70b5
4 changed files with 98 additions and 0 deletions

View File

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

View File

@ -245,6 +245,7 @@
<Compile Include="HealthCheck\Checks\MonoDebugFixture.cs" />
<Compile Include="HealthCheck\Checks\MonoVersionCheckFixture.cs" />
<Compile Include="HealthCheck\Checks\IndexerStatusCheckFixture.cs" />
<Compile Include="HealthCheck\Checks\SystemTimeCheckFixture.cs" />
<Compile Include="HealthCheck\Checks\RootFolderCheckFixture.cs" />
<Compile Include="HealthCheck\Checks\UpdateCheckFixture.cs" />
<Compile Include="HealthCheck\HealthCheckFixture.cs" />

View 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, ISonarrCloudRequestBuilder 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; }
}
}

View File

@ -152,6 +152,7 @@
<Compile Include="Download\Aggregation\RemoteEpisodeAggregationService.cs" />
<Compile Include="Download\Aggregation\Aggregators\AggregatePreferredWordScore.cs" />
<Compile Include="Download\Aggregation\Aggregators\IAggregateRemoteEpisode.cs" />
<Compile Include="HealthCheck\Checks\SystemTimeCheck.cs" />
<Compile Include="Housekeeping\Housekeepers\EnsureValidLanguageProfileId.cs" />
<Compile Include="Indexers\SeedConfigProvider.cs" />
<Compile Include="DataAugmentation\DailySeries\DailySeries.cs" />