-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthCheckRegistry.cs
73 lines (61 loc) · 2.16 KB
/
HealthCheckRegistry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace NetPro.Checker
{
/// <summary>
/// Registry for health checks
/// </summary>
public class HealthCheckRegistry
{
public struct HealthStatus
{
/// <summary>
/// Flag indicating whether any checks are registered
/// </summary>
//[JsonIgnore]
public readonly bool HasRegisteredChecks;
/// <summary>
/// Whether or not all health checks have passed
/// </summary>
public readonly bool IsHealthy;
/// <summary>
/// Array containing result of each registered health check
/// </summary>
public readonly HealthCheck.Result[] Results;
public HealthStatus(IEnumerable<HealthCheck.Result> results)
{
Results = results.ToArray();
IsHealthy = Results.All(r => r.Check.IsHealthy);
HasRegisteredChecks = Results.Length > 0;
}
}
private static readonly ConcurrentDictionary<string, HealthCheck> Checks = new ConcurrentDictionary<string, HealthCheck>();
public static void RegisterHealthCheck(string name, Action check)
{
RegisterHealthCheck(new HealthCheck(name, check));
}
public static void RegisterHealthCheck(string name, Func<string> check)
{
RegisterHealthCheck(new HealthCheck(name, check));
}
public static void RegisterHealthCheck(string name, Func<HealthResponse> check)
{
RegisterHealthCheck(new HealthCheck(name, check));
}
public static void RegisterHealthCheck(HealthCheck healthCheck)
{
Checks.TryAdd(healthCheck.Name, healthCheck);
}
public static HealthStatus GetStatus()
{
var results = Checks.Values.Select(v => v.Execute()).OrderBy(r => r.Name);
return new HealthStatus(results);
}
public static void UnregisterAllHealthChecks()
{
Checks.Clear();
}
}
}