/
directive_handler.go
51 lines (41 loc) · 1.4 KB
/
directive_handler.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
package agent
import (
"context"
"time"
"github.com/datawire/ambassador/pkg/api/agent"
"github.com/datawire/dlib/dlog"
)
type DirectiveHandler interface {
HandleDirective(context.Context, *Agent, *agent.Directive)
}
type BasicDirectiveHandler struct {
DefaultMinReportPeriod time.Duration
}
func (dh *BasicDirectiveHandler) HandleDirective(ctx context.Context, a *Agent, directive *agent.Directive) {
if directive == nil {
dlog.Warn(ctx, "Recieved empty directive, ignoring.")
return
}
ctx = dlog.WithField(ctx, "directive", directive.ID)
dlog.Debug(ctx, "Directive received")
if directive.StopReporting {
// The Director wants us to stop reporting
a.StopReporting(ctx)
}
if directive.MinReportPeriod != nil {
// The Director wants to adjust the minimum time we wait between reports
protoDur := directive.MinReportPeriod
// Note: This conversion ignores potential overflow. In practice this
// shouldn't be a problem, as the server will be constructing this
// durationpb.Duration from a valid time.Duration.
dur := time.Duration(protoDur.Seconds)*time.Second + time.Duration(protoDur.Nanos)*time.Nanosecond
dur = MaxDuration(dur, dh.DefaultMinReportPeriod) // respect configured minimum
a.SetMinReportPeriod(ctx, dur)
}
for _, command := range directive.Commands {
if command.Message != "" {
dlog.Info(ctx, command.Message)
}
}
a.SetLastDirectiveID(ctx, directive.ID)
}