/
checks.go
111 lines (98 loc) · 2.88 KB
/
checks.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package service
import (
"context"
"errors"
"github.com/vertex-center/vertex/config"
"github.com/vertex-center/vertex/core/port"
"github.com/vertex-center/vertex/core/types"
"github.com/vertex-center/vertex/pkg/log"
"github.com/vertex-center/vertex/pkg/net"
"github.com/vertex-center/vlog"
"go.uber.org/atomic"
)
// ChecksService is a service used to check if
// Vertex is ready to serve or not.
type ChecksService struct{}
func NewChecksService() port.ChecksService {
return &ChecksService{}
}
// CheckAll checks if Vertex is ready to serve, by doing
// some checks like internet connection, api readiness, etc.
// It returns a channel of CheckResponse, which contains
// the result of each check. The channel is closed when all
// checks are done.
func (s *ChecksService) CheckAll(ctx context.Context) <-chan types.CheckResponse {
checks := []func(ctx context.Context) types.CheckResponse{
s.checkInternet,
s.checkVertex,
s.checkKernel,
}
resChan := make(chan types.CheckResponse, len(checks))
remaining := atomic.NewInt32(int32(len(checks)))
for _, check := range checks {
check := check
go func() {
res := check(ctx)
resChan <- res
if res.Error != "" {
log.Error(errors.New("component check: failed"), vlog.String("id", res.ID), vlog.String("name", res.Name), vlog.String("reason", res.Error))
} else {
log.Info("component check: ok", vlog.String("id", res.ID), vlog.String("name", res.Name))
}
if remaining.Dec() == 0 {
log.Info("all components are checked")
close(resChan)
}
}()
}
return resChan
}
func (s *ChecksService) checkInternet(ctx context.Context) types.CheckResponse {
res := types.CheckResponse{
ID: "internet",
Name: "Internet connection",
}
err := net.WaitInternetConn(ctx)
if err != nil {
res.Error = err.Error()
}
return res
}
func (s *ChecksService) checkVertex(ctx context.Context) types.CheckResponse {
return s.checkURL(ctx, "api_vertex", "Vertex API", config.Current.VertexURL())
}
func (s *ChecksService) checkKernel(ctx context.Context) types.CheckResponse {
return s.checkURL(ctx, "api_kernel", "Vertex Kernel API", config.Current.KernelURL())
}
func (s *ChecksService) checkURL(ctx context.Context, id, name, url string) types.CheckResponse {
res := types.CheckResponse{
ID: id,
Name: name,
}
err := net.Wait(ctx, url)
if err != nil {
res.Error = err.Error()
}
return res
}
// Disabled for now, since ping requires access to the socket, which needs root.
// func (s *ChecksService) checkDocker(ctx context.Context) types.CheckResponse {
// res := types.CheckResponse{
// ID: "docker",
// Name: "Docker",
// }
//
// cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
// if err != nil {
// res.Error = err.Error()
// return res
// }
//
// _, err = cli.Ping(ctx)
// if err != nil {
// res.Error = err.Error()
// return res
// }
//
// return res
// }