-
Notifications
You must be signed in to change notification settings - Fork 110
/
restart_checker.go
67 lines (55 loc) · 1.65 KB
/
restart_checker.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 server implements the entry point for running a robot web server.
package server
import (
"context"
"time"
"github.com/edaniels/golog"
apppb "go.viam.com/api/app/v1"
"go.viam.com/utils"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/config"
)
const (
defaultNeedsRestartCheckInterval = time.Second * 5
minNeedsRestartCheckInterval = time.Second * 1
)
type needsRestartChecker interface {
needsRestart(ctx context.Context) (bool, time.Duration, error)
close()
}
type needsRestartCheckerGRPC struct {
cfg *config.Cloud
logger golog.Logger
client rpc.ClientConn
}
func (c *needsRestartCheckerGRPC) close() {
if c.client != nil {
utils.UncheckedErrorFunc(c.client.Close)
}
}
func (c *needsRestartCheckerGRPC) needsRestart(ctx context.Context) (bool, time.Duration, error) {
service := apppb.NewRobotServiceClient(c.client)
res, err := service.NeedsRestart(ctx, &apppb.NeedsRestartRequest{Id: c.cfg.ID})
if err != nil {
return false, defaultNeedsRestartCheckInterval, err
}
restartInterval := res.RestartCheckInterval.AsDuration()
if restartInterval < minNeedsRestartCheckInterval {
c.logger.Warnf("received restart interval less than %s not using was %d",
minNeedsRestartCheckInterval,
res.RestartCheckInterval.AsDuration())
restartInterval = defaultNeedsRestartCheckInterval
}
return res.MustRestart, restartInterval, nil
}
func newRestartChecker(ctx context.Context, cfg *config.Cloud, logger golog.Logger) (needsRestartChecker, error) {
client, err := config.CreateNewGRPCClient(ctx, cfg, logger)
if err != nil {
return nil, err
}
return &needsRestartCheckerGRPC{
cfg: cfg,
logger: logger,
client: client,
}, nil
}