-
Notifications
You must be signed in to change notification settings - Fork 0
/
private.go
78 lines (65 loc) · 1.35 KB
/
private.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
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
74
75
76
77
78
package healthcheck
import (
"context"
"github.com/kazhuravlev/healthcheck/internal/logr"
"github.com/kazhuravlev/just"
"strings"
"time"
)
func (s *Healthcheck) runCheck(ctx context.Context, check checkContainer) Check {
ctx, cancel := context.WithTimeout(ctx, check.Check.timeout())
defer cancel()
rec := logr.Rec{
Time: time.Now(),
Error: nil,
}
{
resCh := make(chan logr.Rec, 1)
go func() {
defer close(resCh)
resCh <- check.Check.check(ctx)
}()
select {
case <-ctx.Done():
rec = logr.Rec{
Time: time.Now(),
Error: ctx.Err(),
}
case rec = <-resCh:
}
}
status := StatusUp
errText := ""
if rec.Error != nil {
status = StatusDown
errText = rec.Error.Error()
}
// TODO(zhuravlev): run on manual and bg checks.
s.opts.setCheckStatus(check.ID, status)
prev := just.SliceMap(check.Check.log(), func(rec logr.Rec) CheckState {
status := StatusUp
errText := ""
if rec.Error != nil {
errText = rec.Error.Error()
status = StatusDown
}
return CheckState{
ActualAt: rec.Time,
Status: status,
Error: errText,
}
})
return Check{
Name: check.ID,
State: CheckState{
ActualAt: rec.Time,
Status: status,
Error: errText,
},
Previous: prev,
}
}
func name2id(name string) (string, bool) {
id := strings.ReplaceAll(strings.ToLower(name), "-", "_")
return id, id == name
}