2014-04-10 03:15:13 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2014-02-26 08:40:47 +03:00
|
|
|
using System.Linq;
|
2014-04-10 03:15:13 +03:00
|
|
|
using System.Linq.Expressions;
|
2014-02-26 08:40:47 +03:00
|
|
|
using NLog;
|
2014-04-10 03:15:13 +03:00
|
|
|
using NzbDrone.Common.Cache;
|
2014-02-26 08:40:47 +03:00
|
|
|
using NzbDrone.Core.Configuration.Events;
|
|
|
|
using NzbDrone.Core.Download;
|
|
|
|
using NzbDrone.Core.Indexers;
|
2014-04-10 03:15:13 +03:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2014-02-26 08:40:47 +03:00
|
|
|
using NzbDrone.Core.Messaging.Commands;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.ThingiProvider.Events;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.HealthCheck
|
|
|
|
{
|
|
|
|
public interface IHealthCheckService
|
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
List<HealthCheck> Results();
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class HealthCheckService : IHealthCheckService,
|
|
|
|
IExecute<CheckHealthCommand>,
|
2014-04-10 03:15:13 +03:00
|
|
|
IHandleAsync<ApplicationStartedEvent>,
|
2014-02-26 08:40:47 +03:00
|
|
|
IHandleAsync<ConfigSavedEvent>,
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IIndexer>>,
|
|
|
|
IHandleAsync<ProviderUpdatedEvent<IDownloadClient>>
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IProvideHealthCheck> _healthChecks;
|
|
|
|
private readonly IEventAggregator _eventAggregator;
|
2014-04-10 03:15:13 +03:00
|
|
|
private readonly ICacheManager _cacheManager;
|
2014-02-26 08:40:47 +03:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2014-04-10 03:15:13 +03:00
|
|
|
private readonly ICached<HealthCheck> _healthCheckResults;
|
|
|
|
|
|
|
|
public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks,
|
|
|
|
IEventAggregator eventAggregator,
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
Logger logger)
|
2014-02-26 08:40:47 +03:00
|
|
|
{
|
|
|
|
_healthChecks = healthChecks;
|
|
|
|
_eventAggregator = eventAggregator;
|
2014-04-10 03:15:13 +03:00
|
|
|
_cacheManager = cacheManager;
|
2014-02-26 08:40:47 +03:00
|
|
|
_logger = logger;
|
2014-04-10 03:15:13 +03:00
|
|
|
|
|
|
|
_healthCheckResults = _cacheManager.GetCache<HealthCheck>(GetType());
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
2014-04-10 03:15:13 +03:00
|
|
|
public List<HealthCheck> Results()
|
2014-02-26 08:40:47 +03:00
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
return _healthCheckResults.Values.ToList();
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
2014-04-10 03:15:13 +03:00
|
|
|
private void PerformHealthCheck(Func<IProvideHealthCheck, bool> predicate)
|
2014-02-26 08:40:47 +03:00
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
var results = _healthChecks.Where(predicate)
|
|
|
|
.Select(c => c.Check())
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
foreach (var result in results)
|
|
|
|
{
|
|
|
|
if (result.Type == HealthCheckResult.Ok)
|
|
|
|
{
|
|
|
|
_healthCheckResults.Remove(result.Source.Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_healthCheckResults.Set(result.Source.Name, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_eventAggregator.PublishEvent(new HealthCheckCompleteEvent());
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(ConfigSavedEvent message)
|
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IIndexer> message)
|
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(ProviderUpdatedEvent<IDownloadClient> message)
|
|
|
|
{
|
2014-04-10 03:15:13 +03:00
|
|
|
PerformHealthCheck(c => c.CheckOnConfigChange);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void HandleAsync(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
PerformHealthCheck(c => c.CheckOnStartup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Execute(CheckHealthCommand message)
|
|
|
|
{
|
|
|
|
PerformHealthCheck(c => c.CheckOnSchedule);
|
2014-02-26 08:40:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|