/
failover.go
46 lines (40 loc) · 1.1 KB
/
failover.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
package main
import (
"context"
"github.com/DeBankDeFi/nodex/pkg/lib/log"
"github.com/DeBankDeFi/nodex/pkg/pb"
"github.com/DeBankDeFi/nodex/pkg/types"
"github.com/DeBankDeFi/nodex/pkg/cmdhelper"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var failoverFlag types.FailoverFlag
func failoverCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "failover",
Short: "make reader switch to another writer",
Run: failoverRun,
}
cmdhelper.ResolveFlagVariable(cmd, &failoverFlag)
return cmd
}
func failoverRun(cmd *cobra.Command, args []string) {
conn, err := grpc.Dial(failoverFlag.GrpcServer, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal("did not connect to grpc server", err)
}
defer conn.Close()
client := pb.NewHeartbeatServiceClient(conn)
_, err = client.SetRole(context.Background(), &pb.SetRoleRequest{
Id: &pb.NodeId{
Env: failoverFlag.Env,
ChainId: failoverFlag.ChainID,
Role: pb.NodeRole(failoverFlag.Role),
},
})
if err != nil {
log.Fatal("failover failed", err)
return
}
}