-
Notifications
You must be signed in to change notification settings - Fork 0
/
HealthCheck.cs
54 lines (45 loc) · 1.25 KB
/
HealthCheck.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
using System;
namespace NetPro.Checker
{
public class HealthCheck
{
public struct Result
{
public readonly string Name;
public readonly HealthResponse Check;
public Result(string name, HealthResponse check)
{
Name = name;
Check = check;
}
}
private readonly Func<HealthResponse> _checkFunc;
public string Name { get; private set; }
public HealthCheck(string name, Action check)
: this(name, () => { check(); return string.Empty; })
{ }
public HealthCheck(string name, Func<string> check)
: this(name, () => HealthResponse.Healthy(check()))
{ }
public HealthCheck(string name, Func<HealthResponse> check)
{
Name = name;
_checkFunc = check;
}
protected virtual HealthResponse Check()
{
return _checkFunc();
}
public Result Execute()
{
try
{
return new Result(Name, Check());
}
catch (Exception x)
{
return new Result(Name, HealthResponse.Unhealthy(x));
}
}
}
}