-
Notifications
You must be signed in to change notification settings - Fork 13
/
checkserver.go
95 lines (78 loc) · 2.57 KB
/
checkserver.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
package checkserver
import (
"context"
"encoding/json"
"fmt"
"github.com/OpenSlides/openslides-manage-service/pkg/connection"
"github.com/OpenSlides/openslides-manage-service/proto"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
)
const (
// CheckServerHelp contains the short help text for the command.
CheckServerHelp = "Checks if the server and its services are ready"
// CheckServerHelpExtra contains the long help text for the command without
// the headline.
CheckServerHelpExtra = `At the moment this only checks the health route of the backendManage service.`
)
// Cmd returns the subcommand.
func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "check-server",
Short: CheckServerHelp,
Long: CheckServerHelp + "\n\n" + CheckServerHelpExtra,
Args: cobra.NoArgs,
}
cp := connection.Unary(cmd)
cmd.RunE = func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), *cp.Timeout)
defer cancel()
cl, close, err := connection.Dial(ctx, *cp.Addr, *cp.PasswordFile, !*cp.NoSSL)
if err != nil {
return fmt.Errorf("connecting to gRPC server: %w", err)
}
defer close()
if err := Run(ctx, cl); err != nil {
return fmt.Errorf("checking server: %w", err)
}
return nil
}
return cmd
}
// Client
type gRPCClient interface {
CheckServer(ctx context.Context, in *proto.CheckServerRequest, opts ...grpc.CallOption) (*proto.CheckServerResponse, error)
}
// Run calls respective procedure to check the server via given gRPC client.
func Run(ctx context.Context, gc gRPCClient) error {
req := &proto.CheckServerRequest{}
for {
resp, err := gc.CheckServer(ctx, req)
if err != nil {
s, _ := status.FromError(err) // The ok value does not matter here.
return fmt.Errorf("calling manage service (checking server): %s", s.Message())
}
if resp.Ready {
break
}
}
// We reach this line only if the check server request was successful and
// the context was not canceled (e. g. deadline exceeded).
fmt.Println("Server is ready.")
return nil
}
// Server
type backendAction interface {
Health(context.Context) (json.RawMessage, error)
}
// CheckServer sends a health request to backend manage service.
func CheckServer(ctx context.Context, in *proto.CheckServerRequest, ba backendAction) *proto.CheckServerResponse {
_, err := ba.Health(ctx)
if err != nil {
// Special error handling here: We do not return the (wrapped) error but
// a response with falsy value.
return &proto.CheckServerResponse{Ready: false}
}
return &proto.CheckServerResponse{Ready: true}
}