-
Notifications
You must be signed in to change notification settings - Fork 132
/
watch.go
135 lines (118 loc) · 2.98 KB
/
watch.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package client
import (
"context"
"io"
"time"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/armadaproject/armada/internal/common/util"
"github.com/armadaproject/armada/pkg/api"
"github.com/armadaproject/armada/pkg/client/domain"
)
func GetJobSetState(client api.EventClient, queue, jobSetId string, context context.Context, errorOnNotExists bool, forceNew bool, forceLegacy bool) *domain.WatchContext {
latestState := domain.NewWatchContext()
WatchJobSet(client, queue, jobSetId, false, errorOnNotExists, forceNew, forceLegacy, context, func(state *domain.WatchContext, _ api.Event) bool {
latestState = state
return false
})
return latestState
}
func WatchJobSet(
client api.EventClient,
queue, jobSetId string,
waitForNew bool,
errorOnNotExists bool,
forceNew bool,
forceLegacy bool,
context context.Context,
onUpdate func(*domain.WatchContext, api.Event) bool,
) *domain.WatchContext {
return WatchJobSetWithJobIdsFilter(client, queue, jobSetId, waitForNew, errorOnNotExists, forceNew, forceLegacy, []string{}, context, onUpdate)
}
func WatchJobSetWithJobIdsFilter(
client api.EventClient,
queue, jobSetId string,
waitForNew bool,
errorOnNotExists bool,
forceNew bool,
forceLegacy bool,
jobIds []string,
context context.Context,
onUpdate func(*domain.WatchContext, api.Event) bool,
) *domain.WatchContext {
state := domain.NewWatchContext()
jobIdsSet := util.StringListToSet(jobIds)
filterOnJobId := len(jobIdsSet) > 0
lastMessageId := ""
for {
select {
case <-context.Done():
return state
default:
}
clientStream, e := client.GetJobSetEvents(context,
&api.JobSetRequest{
Queue: queue,
Id: jobSetId,
FromMessageId: lastMessageId,
Watch: waitForNew,
ErrorIfMissing: errorOnNotExists,
ForceNew: forceNew,
ForceLegacy: forceLegacy,
},
)
if e != nil {
log.Error(e)
time.Sleep(5 * time.Second)
continue
}
for {
msg, e := clientStream.Recv()
if e != nil {
if err, ok := status.FromError(e); ok {
switch err.Code() {
case codes.NotFound:
log.Error(err.Message())
return state
case codes.PermissionDenied:
log.Error(err.Message())
return state
}
}
if e == io.EOF {
return state
}
if !isTransportClosingError(e) {
log.Error(e)
}
time.Sleep(5 * time.Second)
break
}
lastMessageId = msg.Id
event, e := api.UnwrapEvent(msg.Message)
if e != nil {
// This can mean that the event type reported from server is unknown to the client
log.Error(e)
continue
}
if filterOnJobId && !jobIdsSet[event.GetJobId()] {
continue
}
state.ProcessEvent(event)
shouldExit := onUpdate(state, event)
if shouldExit {
return state
}
}
}
}
func isTransportClosingError(e error) bool {
if err, ok := status.FromError(e); ok {
switch err.Code() {
case codes.Unavailable:
return true
}
}
return false
}