1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Added ability for HealthChecks to run on specific events.

This commit is contained in:
Taloth Saldono 2017-05-14 19:19:19 +02:00
parent 4e10d30cf6
commit e83e852e0d
20 changed files with 116 additions and 49 deletions

View File

@ -60,6 +60,11 @@ public static T GetAttribute<T>(this MemberInfo member, bool isRequired = true)
return (T)attribute; return (T)attribute;
} }
public static T[] GetAttributes<T>(this MemberInfo member) where T : Attribute
{
return member.GetCustomAttributes(typeof(T), false).OfType<T>().ToArray();
}
public static Type FindTypeByName(this Assembly assembly, string name) public static Type FindTypeByName(this Assembly assembly, string name)
{ {
return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)); return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
@ -70,4 +75,4 @@ public static bool HasAttribute<TAttribute>(this Type type)
return type.GetCustomAttributes(typeof(TAttribute), true).Any(); return type.GetCustomAttributes(typeof(TAttribute), true).Any();
} }
} }
} }

View File

@ -0,0 +1,16 @@
using System;
using NzbDrone.Common.Messaging;
namespace NzbDrone.Core.HealthCheck
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class CheckOnAttribute: Attribute
{
public Type EventType { get; set; }
public CheckOnAttribute(Type eventType)
{
EventType = eventType;
}
}
}

View File

@ -1,5 +1,6 @@
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
@ -11,7 +12,7 @@ public AppDataLocationCheck(IAppFolderInfo appFolderInfo)
{ {
_appFolderInfo = appFolderInfo; _appFolderInfo = appFolderInfo;
} }
public override HealthCheck Check() public override HealthCheck Check()
{ {
if (_appFolderInfo.StartUpFolder.IsParentPath(_appFolderInfo.AppDataFolder) || if (_appFolderInfo.StartUpFolder.IsParentPath(_appFolderInfo.AppDataFolder) ||
@ -22,7 +23,5 @@ public override HealthCheck Check()
return new HealthCheck(GetType()); return new HealthCheck(GetType());
} }
public override bool CheckOnConfigChange => false;
} }
} }

View File

@ -2,9 +2,12 @@
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IDownloadClient>))]
[CheckOn(typeof(ProviderDeletedEvent<IDownloadClient>))]
public class DownloadClientCheck : HealthCheckBase public class DownloadClientCheck : HealthCheckBase
{ {
private readonly IProvideDownloadClient _downloadClientProvider; private readonly IProvideDownloadClient _downloadClientProvider;

View File

@ -2,9 +2,13 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IDownloadClient>))]
[CheckOn(typeof(ProviderDeletedEvent<IDownloadClient>))]
[CheckOn(typeof(ProviderStatusChangedEvent<IDownloadClient>))]
public class DownloadClientStatusCheck : HealthCheckBase public class DownloadClientStatusCheck : HealthCheckBase
{ {
private readonly IDownloadClientFactory _providerFactory; private readonly IDownloadClientFactory _providerFactory;

View File

@ -1,9 +1,11 @@
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ConfigSavedEvent))]
public class DroneFactoryCheck : HealthCheckBase public class DroneFactoryCheck : HealthCheckBase
{ {
private readonly IConfigService _configService; private readonly IConfigService _configService;
@ -28,7 +30,7 @@ public override HealthCheck Check()
{ {
return new HealthCheck(GetType(), HealthCheckResult.Error, "Drone factory folder does not exist"); return new HealthCheck(GetType(), HealthCheckResult.Error, "Drone factory folder does not exist");
} }
if (!_diskProvider.FolderWritable(droneFactoryFolder)) if (!_diskProvider.FolderWritable(droneFactoryFolder))
{ {
return new HealthCheck(GetType(), HealthCheckResult.Error, "Unable to write to drone factory folder"); return new HealthCheck(GetType(), HealthCheckResult.Error, "Unable to write to drone factory folder");

View File

@ -3,13 +3,18 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.Download; using NzbDrone.Core.Download;
using NzbDrone.Core.Download.Clients; using NzbDrone.Core.Download.Clients;
using NzbDrone.Core.Download.Clients.Nzbget; using NzbDrone.Core.Download.Clients.Nzbget;
using NzbDrone.Core.Download.Clients.Sabnzbd; using NzbDrone.Core.Download.Clients.Sabnzbd;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IDownloadClient>))]
[CheckOn(typeof(ProviderDeletedEvent<IDownloadClient>))]
[CheckOn(typeof(ConfigSavedEvent))]
public class ImportMechanismCheck : HealthCheckBase public class ImportMechanismCheck : HealthCheckBase
{ {
private readonly IConfigService _configService; private readonly IConfigService _configService;

View File

@ -1,9 +1,13 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IIndexer>))]
[CheckOn(typeof(ProviderDeletedEvent<IIndexer>))]
[CheckOn(typeof(ProviderStatusChangedEvent<IIndexer>))]
public class IndexerRssCheck : HealthCheckBase public class IndexerRssCheck : HealthCheckBase
{ {
private readonly IIndexerFactory _indexerFactory; private readonly IIndexerFactory _indexerFactory;

View File

@ -1,9 +1,13 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IIndexer>))]
[CheckOn(typeof(ProviderDeletedEvent<IIndexer>))]
[CheckOn(typeof(ProviderStatusChangedEvent<IIndexer>))]
public class IndexerSearchCheck : HealthCheckBase public class IndexerSearchCheck : HealthCheckBase
{ {
private readonly IIndexerFactory _indexerFactory; private readonly IIndexerFactory _indexerFactory;

View File

@ -2,9 +2,13 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Indexers; using NzbDrone.Core.Indexers;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ProviderUpdatedEvent<IIndexer>))]
[CheckOn(typeof(ProviderDeletedEvent<IIndexer>))]
[CheckOn(typeof(ProviderStatusChangedEvent<IIndexer>))]
public class IndexerStatusCheck : HealthCheckBase public class IndexerStatusCheck : HealthCheckBase
{ {
private readonly IIndexerFactory _providerFactory; private readonly IIndexerFactory _providerFactory;

View File

@ -20,7 +20,5 @@ public override HealthCheck Check()
return new HealthCheck(GetType()); return new HealthCheck(GetType());
} }
public override bool CheckOnConfigChange => false;
} }
} }

View File

@ -47,8 +47,6 @@ public override HealthCheck Check()
return new HealthCheck(GetType(), HealthCheckResult.Warning, "You are running an old and unsupported version of Mono. Please upgrade Mono for improved stability."); return new HealthCheck(GetType(), HealthCheckResult.Warning, "You are running an old and unsupported version of Mono. Please upgrade Mono for improved stability.");
} }
public override bool CheckOnConfigChange => false;
public override bool CheckOnSchedule => false; public override bool CheckOnSchedule => false;
private bool HasMonoBug18599() private bool HasMonoBug18599()
@ -80,4 +78,4 @@ private bool HasMonoBug18599()
return false; return false;
} }
} }
} }

View File

@ -5,9 +5,11 @@
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using NzbDrone.Common.Cloud; using NzbDrone.Common.Cloud;
using NzbDrone.Core.Configuration.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ConfigSavedEvent))]
public class ProxyCheck : HealthCheckBase public class ProxyCheck : HealthCheckBase
{ {
private readonly Logger _logger; private readonly Logger _logger;
@ -43,7 +45,7 @@ public override HealthCheck Check()
{ {
var response = _client.Execute(request); var response = _client.Execute(request);
// We only care about 400 responses, other error codes can be ignored // We only care about 400 responses, other error codes can be ignored
if (response.StatusCode == HttpStatusCode.BadRequest) if (response.StatusCode == HttpStatusCode.BadRequest)
{ {
_logger.Error("Proxy Health Check failed: {0}", response.StatusCode); _logger.Error("Proxy Health Check failed: {0}", response.StatusCode);

View File

@ -1,9 +1,12 @@
using System.Linq; using System.Linq;
using NzbDrone.Common.Disk; using NzbDrone.Common.Disk;
using NzbDrone.Core.Tv; using NzbDrone.Core.Tv;
using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(SeriesDeletedEvent))]
[CheckOn(typeof(SeriesMovedEvent))]
public class RootFolderCheck : HealthCheckBase public class RootFolderCheck : HealthCheckBase
{ {
private readonly ISeriesService _seriesService; private readonly ISeriesService _seriesService;
@ -36,7 +39,5 @@ public override HealthCheck Check()
return new HealthCheck(GetType()); return new HealthCheck(GetType());
} }
public override bool CheckOnConfigChange => false;
} }
} }

View File

@ -4,10 +4,12 @@
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.Update; using NzbDrone.Core.Update;
namespace NzbDrone.Core.HealthCheck.Checks namespace NzbDrone.Core.HealthCheck.Checks
{ {
[CheckOn(typeof(ConfigFileSavedEvent))]
public class UpdateCheck : HealthCheckBase public class UpdateCheck : HealthCheckBase
{ {
private readonly IDiskProvider _diskProvider; private readonly IDiskProvider _diskProvider;
@ -66,7 +68,5 @@ public override HealthCheck Check()
return new HealthCheck(GetType()); return new HealthCheck(GetType());
} }
public override bool CheckOnConfigChange => false;
} }
} }

View File

@ -6,8 +6,6 @@ public abstract class HealthCheckBase : IProvideHealthCheck
public virtual bool CheckOnStartup => true; public virtual bool CheckOnStartup => true;
public virtual bool CheckOnConfigChange => true;
public virtual bool CheckOnSchedule => true; public virtual bool CheckOnSchedule => true;
} }
} }

View File

@ -3,6 +3,9 @@
using System.Linq; using System.Linq;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Reflection;
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;
@ -21,13 +24,12 @@ public interface IHealthCheckService
public class HealthCheckService : IHealthCheckService, public class HealthCheckService : IHealthCheckService,
IExecute<CheckHealthCommand>, IExecute<CheckHealthCommand>,
IHandleAsync<ApplicationStartedEvent>, IHandleAsync<ApplicationStartedEvent>,
IHandleAsync<ConfigSavedEvent>, IHandleAsync<IEvent>
IHandleAsync<ProviderUpdatedEvent<IIndexer>>,
IHandleAsync<ProviderDeletedEvent<IIndexer>>,
IHandleAsync<ProviderUpdatedEvent<IDownloadClient>>,
IHandleAsync<ProviderDeletedEvent<IDownloadClient>>
{ {
private readonly IEnumerable<IProvideHealthCheck> _healthChecks; private readonly IProvideHealthCheck[] _healthChecks;
private readonly IProvideHealthCheck[] _startupHealthChecks;
private readonly IProvideHealthCheck[] _scheduledHealthChecks;
private readonly Dictionary<Type, IProvideHealthCheck[]> _eventDrivenHealthChecks;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly ICacheManager _cacheManager; private readonly ICacheManager _cacheManager;
private readonly Logger _logger; private readonly Logger _logger;
@ -39,12 +41,16 @@ public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks,
ICacheManager cacheManager, ICacheManager cacheManager,
Logger logger) Logger logger)
{ {
_healthChecks = healthChecks; _healthChecks = healthChecks.ToArray();
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_cacheManager = cacheManager; _cacheManager = cacheManager;
_logger = logger; _logger = logger;
_healthCheckResults = _cacheManager.GetCache<HealthCheck>(GetType()); _healthCheckResults = _cacheManager.GetCache<HealthCheck>(GetType());
_startupHealthChecks = _healthChecks.Where(v => v.CheckOnStartup).ToArray();
_scheduledHealthChecks = _healthChecks.Where(v => v.CheckOnSchedule).ToArray();
_eventDrivenHealthChecks = GetEventDrivenHealthChecks();
} }
public List<HealthCheck> Results() public List<HealthCheck> Results()
@ -52,10 +58,17 @@ public List<HealthCheck> Results()
return _healthCheckResults.Values.ToList(); return _healthCheckResults.Values.ToList();
} }
private void PerformHealthCheck(Func<IProvideHealthCheck, bool> predicate) private Dictionary<Type, IProvideHealthCheck[]> GetEventDrivenHealthChecks()
{ {
var results = _healthChecks.Where(predicate) return _healthChecks
.Select(c => c.Check()) .SelectMany(h => h.GetType().GetAttributes<CheckOnAttribute>().Select(a => Tuple.Create(a.EventType, h)))
.GroupBy(t => t.Item1, t => t.Item2)
.ToDictionary(g => g.Key, g => g.ToArray());
}
private void PerformHealthCheck(IProvideHealthCheck[] healthChecks)
{
var results = healthChecks.Select(c => c.Check())
.ToList(); .ToList();
foreach (var result in results) foreach (var result in results)
@ -76,37 +89,37 @@ private void PerformHealthCheck(Func<IProvideHealthCheck, bool> predicate)
public void Execute(CheckHealthCommand message) public void Execute(CheckHealthCommand message)
{ {
PerformHealthCheck(c => message.Trigger == CommandTrigger.Manual || c.CheckOnSchedule); if (message.Trigger == CommandTrigger.Manual)
{
PerformHealthCheck(_healthChecks);
}
else
{
PerformHealthCheck(_scheduledHealthChecks);
}
} }
public void HandleAsync(ApplicationStartedEvent message) public void HandleAsync(ApplicationStartedEvent message)
{ {
PerformHealthCheck(c => c.CheckOnStartup); PerformHealthCheck(_startupHealthChecks);
} }
public void HandleAsync(ConfigSavedEvent message) public void HandleAsync(IEvent message)
{ {
PerformHealthCheck(c => c.CheckOnConfigChange); if (message is HealthCheckCompleteEvent)
} {
return;
}
public void HandleAsync(ProviderUpdatedEvent<IIndexer> message) IProvideHealthCheck[] checks;
{ if (!_eventDrivenHealthChecks.TryGetValue(message.GetType(), out checks))
PerformHealthCheck(c => c.CheckOnConfigChange); {
} return;
}
public void HandleAsync(ProviderDeletedEvent<IIndexer> message) // TODO: Add debounce
{
PerformHealthCheck(c => c.CheckOnConfigChange);
}
public void HandleAsync(ProviderUpdatedEvent<IDownloadClient> message) PerformHealthCheck(checks);
{
PerformHealthCheck(c => c.CheckOnConfigChange);
}
public void HandleAsync(ProviderDeletedEvent<IDownloadClient> message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
} }
} }
} }

View File

@ -4,7 +4,6 @@ public interface IProvideHealthCheck
{ {
HealthCheck Check(); HealthCheck Check();
bool CheckOnStartup { get; } bool CheckOnStartup { get; }
bool CheckOnConfigChange { get; }
bool CheckOnSchedule { get; } bool CheckOnSchedule { get; }
} }
} }

View File

@ -62,6 +62,17 @@ public void PublishEvent<TEvent>(TEvent @event) where TEvent : class, IEvent
} }
} }
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<IEvent>>())
{
var handlerLocal = handler;
_taskFactory.StartNew(() =>
{
handlerLocal.HandleAsync(@event);
}, TaskCreationOptions.PreferFairness)
.LogExceptions();
}
foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>()) foreach (var handler in _serviceFactory.BuildAll<IHandleAsync<TEvent>>())
{ {
var handlerLocal = handler; var handlerLocal = handler;

View File

@ -577,6 +577,7 @@
<Compile Include="HealthCheck\HealthCheckBase.cs" /> <Compile Include="HealthCheck\HealthCheckBase.cs" />
<Compile Include="HealthCheck\HealthCheckCompleteEvent.cs" /> <Compile Include="HealthCheck\HealthCheckCompleteEvent.cs" />
<Compile Include="HealthCheck\HealthCheckService.cs" /> <Compile Include="HealthCheck\HealthCheckService.cs" />
<Compile Include="HealthCheck\CheckOnAttribute.cs" />
<Compile Include="HealthCheck\IProvideHealthCheck.cs" /> <Compile Include="HealthCheck\IProvideHealthCheck.cs" />
<Compile Include="History\History.cs" /> <Compile Include="History\History.cs" />
<Compile Include="History\HistoryRepository.cs" /> <Compile Include="History\HistoryRepository.cs" />