|
|
@ -1,9 +1,13 @@
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using NLog;
|
|
|
|
using NLog;
|
|
|
|
|
|
|
|
using NzbDrone.Common.Cache;
|
|
|
|
using NzbDrone.Core.Configuration.Events;
|
|
|
|
using NzbDrone.Core.Configuration.Events;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
using NzbDrone.Core.Indexers;
|
|
|
|
|
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.ThingiProvider.Events;
|
|
|
|
using NzbDrone.Core.ThingiProvider.Events;
|
|
|
@ -12,56 +16,86 @@ namespace NzbDrone.Core.HealthCheck
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public interface IHealthCheckService
|
|
|
|
public interface IHealthCheckService
|
|
|
|
{
|
|
|
|
{
|
|
|
|
List<HealthCheck> PerformHealthCheck();
|
|
|
|
List<HealthCheck> Results();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class HealthCheckService : IHealthCheckService,
|
|
|
|
public class HealthCheckService : IHealthCheckService,
|
|
|
|
IExecute<CheckHealthCommand>,
|
|
|
|
IExecute<CheckHealthCommand>,
|
|
|
|
|
|
|
|
IHandleAsync<ApplicationStartedEvent>,
|
|
|
|
IHandleAsync<ConfigSavedEvent>,
|
|
|
|
IHandleAsync<ConfigSavedEvent>,
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IIndexer>>,
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IIndexer>>,
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IDownloadClient>>
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IDownloadClient>>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IProvideHealthCheck> _healthChecks;
|
|
|
|
private readonly IEnumerable<IProvideHealthCheck> _healthChecks;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
|
|
|
|
|
|
|
private readonly ICacheManager _cacheManager;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks, IEventAggregator eventAggregator, Logger logger)
|
|
|
|
private readonly ICached<HealthCheck> _healthCheckResults;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks,
|
|
|
|
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_healthChecks = healthChecks;
|
|
|
|
_healthChecks = healthChecks;
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
|
|
|
_cacheManager = cacheManager;
|
|
|
|
_logger = logger;
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_healthCheckResults = _cacheManager.GetCache<HealthCheck>(GetType());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<HealthCheck> Results()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return _healthCheckResults.Values.ToList();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<HealthCheck> PerformHealthCheck()
|
|
|
|
private void PerformHealthCheck(Func<IProvideHealthCheck, bool> predicate)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_logger.Trace("Checking health");
|
|
|
|
var results = _healthChecks.Where(predicate)
|
|
|
|
var result = _healthChecks.Select(c => c.Check()).Where(c => c != null).ToList();
|
|
|
|
.Select(c => c.Check())
|
|
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
foreach (var result in results)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (result.Type == HealthCheckResult.Ok)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_healthCheckResults.Remove(result.Source.Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute(CheckHealthCommand message)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//Until we have stored health checks we should just trigger the complete event
|
|
|
|
_healthCheckResults.Set(result.Source.Name, result);
|
|
|
|
//and let the clients check in
|
|
|
|
}
|
|
|
|
//Multiple connected clients means we're going to compute the health check multiple times
|
|
|
|
}
|
|
|
|
//Multiple checks feels a bit ugly, but means the most up to date information goes to the client
|
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new TriggerHealthCheckEvent());
|
|
|
|
_eventAggregator.PublishEvent(new HealthCheckCompleteEvent());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ConfigSavedEvent message)
|
|
|
|
public void HandleAsync(ConfigSavedEvent message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new TriggerHealthCheckEvent());
|
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IIndexer> message)
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IIndexer> message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new TriggerHealthCheckEvent());
|
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IDownloadClient> message)
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IDownloadClient> message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_eventAggregator.PublishEvent(new TriggerHealthCheckEvent());
|
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PerformHealthCheck(c => c.CheckOnStartup);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Execute(CheckHealthCommand message)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PerformHealthCheck(c => c.CheckOnSchedule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|