-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathready.go
67 lines (52 loc) · 1.28 KB
/
ready.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
package cmd
import (
"context"
"net/http"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/log"
"github.com/spf13/cobra"
)
type readyConfig struct {
MonitoringURL string
}
func defaultReadyConfig() readyConfig {
return readyConfig{
MonitoringURL: "http://localhost:26660",
}
}
func newReadyCmd() *cobra.Command {
cfg := defaultReadyConfig()
cmd := &cobra.Command{
Use: "ready",
Short: "Query remote node for readiness",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
err := queryReady(cmd.Context(), cfg)
if err != nil {
return errors.Wrap(err, "ready")
}
return nil
},
}
bindReadyFlags(cmd, &cfg)
return cmd
}
// queryReady calls halo's /ready endpoint and returns nil if the status is ready
// or an error otherwise.
func queryReady(ctx context.Context, cfg readyConfig) error {
url := cfg.MonitoringURL + "/ready"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return errors.Wrap(err, "http request creation")
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "http request")
}
defer resp.Body.Close()
if resp.StatusCode/100 != 2 {
return errors.New("node not ready")
}
log.Info(ctx, "Node ready")
return nil
}