/
checker.go
43 lines (31 loc) · 860 Bytes
/
checker.go
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
package fxhealthchecker
import "context"
type HealthCheckerResult struct {
Success bool `json:"success"`
ProbesResults map[string]*HealthCheckerProbeResult `json:"probes"`
}
type HealthChecker struct {
probes []HealthCheckerProbe
}
func NewHealthChecker() *HealthChecker {
return &HealthChecker{
probes: []HealthCheckerProbe{},
}
}
func (c *HealthChecker) AddProbe(p HealthCheckerProbe) *HealthChecker {
c.probes = append(c.probes, p)
return c
}
func (c *HealthChecker) Run(ctx context.Context) *HealthCheckerResult {
success := true
probeResults := map[string]*HealthCheckerProbeResult{}
for _, p := range c.probes {
pr := p.Check(ctx)
success = success && pr.Success
probeResults[p.Name()] = pr
}
return &HealthCheckerResult{
Success: success,
ProbesResults: probeResults,
}
}