Added ability for HealthChecks to run on specific events.

Taloth Saldono 8 years ago
parent 4e10d30cf6
commit e83e852e0d

@ -60,6 +60,11 @@ namespace NzbDrone.Common.Reflection
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)
{
return assembly.GetTypes().SingleOrDefault(c => c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));
@ -70,4 +75,4 @@ namespace NzbDrone.Common.Reflection
return type.GetCustomAttributes(typeof(TAttribute), true).Any();
}
}
}
}

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

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

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

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

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

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

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

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

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

@ -20,7 +20,5 @@ namespace NzbDrone.Core.HealthCheck.Checks
return new HealthCheck(GetType());
}
public override bool CheckOnConfigChange => false;
}
}

@ -47,8 +47,6 @@ namespace NzbDrone.Core.HealthCheck.Checks
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;
private bool HasMonoBug18599()
@ -80,4 +78,4 @@ namespace NzbDrone.Core.HealthCheck.Checks
return false;
}
}
}
}

@ -5,9 +5,11 @@ using System;
using System.Linq;
using System.Net;
using NzbDrone.Common.Cloud;
using NzbDrone.Core.Configuration.Events;
namespace NzbDrone.Core.HealthCheck.Checks
{
[CheckOn(typeof(ConfigSavedEvent))]
public class ProxyCheck : HealthCheckBase
{
private readonly Logger _logger;
@ -43,7 +45,7 @@ namespace NzbDrone.Core.HealthCheck.Checks
{
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)
{
_logger.Error("Proxy Health Check failed: {0}", response.StatusCode);

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

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

@ -6,8 +6,6 @@
public virtual bool CheckOnStartup => true;
public virtual bool CheckOnConfigChange => true;
public virtual bool CheckOnSchedule => true;
}
}

@ -3,6 +3,9 @@ using System.Collections.Generic;
using System.Linq;
using NLog;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Messaging;
using NzbDrone.Common.Reflection;
using NzbDrone.Core.Configuration.Events;
using NzbDrone.Core.Download;
using NzbDrone.Core.Indexers;
@ -21,13 +24,12 @@ namespace NzbDrone.Core.HealthCheck
public class HealthCheckService : IHealthCheckService,
IExecute<CheckHealthCommand>,
IHandleAsync<ApplicationStartedEvent>,
IHandleAsync<ConfigSavedEvent>,
IHandleAsync<ProviderUpdatedEvent<IIndexer>>,
IHandleAsync<ProviderDeletedEvent<IIndexer>>,
IHandleAsync<ProviderUpdatedEvent<IDownloadClient>>,
IHandleAsync<ProviderDeletedEvent<IDownloadClient>>
IHandleAsync<IEvent>
{
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 ICacheManager _cacheManager;
private readonly Logger _logger;
@ -39,12 +41,16 @@ namespace NzbDrone.Core.HealthCheck
ICacheManager cacheManager,
Logger logger)
{
_healthChecks = healthChecks;
_healthChecks = healthChecks.ToArray();
_eventAggregator = eventAggregator;
_cacheManager = cacheManager;
_logger = logger;
_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()
@ -52,10 +58,17 @@ namespace NzbDrone.Core.HealthCheck
return _healthCheckResults.Values.ToList();
}
private void PerformHealthCheck(Func<IProvideHealthCheck, bool> predicate)
private Dictionary<Type, IProvideHealthCheck[]> GetEventDrivenHealthChecks()
{
return _healthChecks
.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.Where(predicate)
.Select(c => c.Check())
var results = healthChecks.Select(c => c.Check())
.ToList();
foreach (var result in results)
@ -76,37 +89,37 @@ namespace NzbDrone.Core.HealthCheck
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)
{
PerformHealthCheck(c => c.CheckOnStartup);
}
public void HandleAsync(ConfigSavedEvent message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
PerformHealthCheck(_startupHealthChecks);
}
public void HandleAsync(ProviderUpdatedEvent<IIndexer> message)
public void HandleAsync(IEvent message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
}
if (message is HealthCheckCompleteEvent)
{
return;
}
public void HandleAsync(ProviderDeletedEvent<IIndexer> message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
}
IProvideHealthCheck[] checks;
if (!_eventDrivenHealthChecks.TryGetValue(message.GetType(), out checks))
{
return;
}
public void HandleAsync(ProviderUpdatedEvent<IDownloadClient> message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
}
// TODO: Add debounce
public void HandleAsync(ProviderDeletedEvent<IDownloadClient> message)
{
PerformHealthCheck(c => c.CheckOnConfigChange);
PerformHealthCheck(checks);
}
}
}

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

@ -62,6 +62,17 @@ namespace NzbDrone.Core.Messaging.Events
}
}
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>>())
{
var handlerLocal = handler;

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

Loading…
Cancel
Save