-
Notifications
You must be signed in to change notification settings - Fork 34
/
context_box_connector.go
53 lines (44 loc) · 1.76 KB
/
context_box_connector.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
package outboundAdapters
import (
"google.golang.org/grpc"
"github.com/berops/claudie/internal/envs"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
cbox "github.com/berops/claudie/services/context-box/client"
)
type ContextBoxConnector struct {
Connection *grpc.ClientConn
}
// Connect establishes a gRPC connection with the context-box microservice
func (c *ContextBoxConnector) Connect() error {
connection, err := utils.GrpcDialWithRetryAndBackoff("context-box", envs.ContextBoxURL)
if err != nil {
return err
}
c.Connection = connection
return nil
}
// The context-box microservice has a scheduler queue(FIFO) containing ConfigInfos
// of those configs whose desired state needs to be built.
// GetConfigScheduler gets config from context-box DB corresponding to the configInfo
// present in the front of the scheduler queue.
func (c ContextBoxConnector) GetConfigScheduler(contextBoxGrpcClient pb.ContextBoxServiceClient) (*pb.GetConfigResponse, error) {
return cbox.GetConfigScheduler(contextBoxGrpcClient)
}
// SaveConfigScheduler saves a config into context-box DB
func (c ContextBoxConnector) SaveConfigScheduler(config *pb.Config, contextBoxGrpcClient pb.ContextBoxServiceClient) error {
return cbox.SaveConfigScheduler(contextBoxGrpcClient, &pb.SaveConfigRequest{Config: config})
}
// Disconnect closes the underlying gRPC connection to context-box microservice
func (c *ContextBoxConnector) Disconnect() {
utils.CloseClientConnection(c.Connection)
}
// PerformHealthCheck checks health of the underlying gRPC connection to context-box microservice.
func (c *ContextBoxConnector) PerformHealthCheck() error {
if err := utils.IsConnectionReady(c.Connection); err == nil {
return nil
} else {
c.Connection.Connect()
return err
}
}