-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.go
76 lines (62 loc) · 2.17 KB
/
scheduler.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
package healthcheck
import (
"context"
"fmt"
"github.com/SotirisAlfonsos/chaos-master/pkg/chaoslogger"
v1 "github.com/SotirisAlfonsos/chaos-bot/proto/grpc/v1"
"github.com/SotirisAlfonsos/chaos-master/pkg/network"
"github.com/go-kit/kit/log/level"
"github.com/robfig/cron/v3"
)
type HealthChecker struct {
DetailsMap map[string]*Details
loggers chaoslogger.Loggers
}
type Details struct {
Status v1.HealthCheckResponse_ServingStatus
connection network.Connection
}
func Register(connections *network.Connections, loggers chaoslogger.Loggers) *HealthChecker {
healthChecker := &HealthChecker{DetailsMap: make(map[string]*Details), loggers: loggers}
for target, connection := range connections.Pool {
healthChecker.DetailsMap[target] = &Details{
Status: v1.HealthCheckResponse_UNKNOWN,
connection: connection,
}
}
return healthChecker
}
func (hch *HealthChecker) Start(report bool) {
c := cron.New()
id, err := c.AddFunc("@every 1m", func() {
for target, details := range hch.DetailsMap {
client, err := details.connection.GetHealthClient()
if err != nil {
_ = level.Error(hch.loggers.ErrLogger).Log(
"msg", fmt.Sprintf("Can not get healthcheck connection for target {%s}", target),
"err", err)
continue
}
resp, err := client.Check(context.Background(), &v1.HealthCheckRequest{})
if err != nil {
_ = level.Error(hch.loggers.ErrLogger).Log(
"msg", fmt.Sprintf("Failed to get valid response when health-checking target %s", target),
"err", err)
hch.DetailsMap[target].Status = v1.HealthCheckResponse_NOT_SERVING
} else {
hch.DetailsMap[target].Status = resp.Status
}
}
_ = level.Debug(hch.loggers.OutLogger).Log("msg", "checking status of bots")
if report {
for target, details := range hch.DetailsMap {
_ = level.Info(hch.loggers.OutLogger).Log("msg", fmt.Sprintf("Status of bot %s is %s", target, details.Status))
}
}
})
if err != nil {
_ = level.Error(hch.loggers.ErrLogger).Log("msg", "could not create scheduling task for automated health-checks")
}
_ = level.Info(hch.loggers.OutLogger).Log("msg", fmt.Sprintf("starting automated health-check scheduler with id %d", id))
c.Start()
}