-
Notifications
You must be signed in to change notification settings - Fork 126
/
dispatcher.go
371 lines (285 loc) · 8.66 KB
/
dispatcher.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"time"
dispatchercontracts "github.com/hatchet-dev/hatchet/internal/services/dispatcher/contracts"
"github.com/hatchet-dev/hatchet/internal/validator"
"github.com/rs/zerolog"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)
type DispatcherClient interface {
GetActionListener(ctx context.Context, req *GetActionListenerRequest) (WorkerActionListener, error)
SendActionEvent(ctx context.Context, in *ActionEvent) (*ActionEventResponse, error)
}
const (
DefaultActionListenerRetryInterval = 5 * time.Second
DefaultActionListenerRetryCount = 5
)
// TODO: add validator to client side
type GetActionListenerRequest struct {
WorkerName string
Services []string
Actions []string
}
// ActionPayload unmarshals the action payload into the target. It also validates the resulting target.
type ActionPayload func(target interface{}) error
type ActionType string
const (
ActionTypeStartStepRun ActionType = "START_STEP_RUN"
ActionTypeCancelStepRun ActionType = "CANCEL_STEP_RUN"
)
type Action struct {
// the worker id
WorkerId string
// the tenant id
TenantId string
// the job id
JobId string
// the job name
JobName string
// the job run id
JobRunId string
// the step id
StepId string
// the step run id
StepRunId string
// the action id
ActionId string
// the action payload
ActionPayload ActionPayload
// the action type
ActionType ActionType
}
type WorkerActionListener interface {
Actions(ctx context.Context, errCh chan<- error) (<-chan *Action, error)
Unregister() error
}
type ActionEventType string
const (
ActionEventTypeUnknown ActionEventType = "STEP_EVENT_TYPE_UNKNOWN"
ActionEventTypeStarted ActionEventType = "STEP_EVENT_TYPE_STARTED"
ActionEventTypeCompleted ActionEventType = "STEP_EVENT_TYPE_COMPLETED"
ActionEventTypeFailed ActionEventType = "STEP_EVENT_TYPE_FAILED"
)
type ActionEvent struct {
*Action
// the event timestamp
EventTimestamp *time.Time
// the step event type
EventType ActionEventType
// The event payload. This must be JSON-compatible as it gets marshalled to a JSON string.
EventPayload interface{}
}
type ActionEventResponse struct {
// the tenant id
TenantId string
// the id of the worker
WorkerId string
}
type dispatcherClientImpl struct {
client dispatchercontracts.DispatcherClient
tenantId string
l *zerolog.Logger
v validator.Validator
}
func newDispatcher(conn *grpc.ClientConn, opts *sharedClientOpts) DispatcherClient {
return &dispatcherClientImpl{
client: dispatchercontracts.NewDispatcherClient(conn),
tenantId: opts.tenantId,
l: opts.l,
v: opts.v,
}
}
type actionListenerImpl struct {
client dispatchercontracts.DispatcherClient
tenantId string
listenClient dispatchercontracts.Dispatcher_ListenClient
workerId string
l *zerolog.Logger
v validator.Validator
}
func (d *dispatcherClientImpl) newActionListener(ctx context.Context, req *GetActionListenerRequest) (*actionListenerImpl, error) {
// validate the request
if err := d.v.Validate(req); err != nil {
return nil, err
}
// register the worker
resp, err := d.client.Register(ctx, &dispatchercontracts.WorkerRegisterRequest{
TenantId: d.tenantId,
WorkerName: req.WorkerName,
Actions: req.Actions,
Services: req.Services,
})
if err != nil {
return nil, fmt.Errorf("could not register the worker: %w", err)
}
d.l.Debug().Msgf("Registered worker with id: %s", resp.WorkerId)
// subscribe to the worker
listener, err := d.client.Listen(ctx, &dispatchercontracts.WorkerListenRequest{
TenantId: d.tenantId,
WorkerId: resp.WorkerId,
})
if err != nil {
return nil, fmt.Errorf("could not subscribe to the worker: %w", err)
}
return &actionListenerImpl{
client: d.client,
listenClient: listener,
workerId: resp.WorkerId,
l: d.l,
v: d.v,
tenantId: d.tenantId,
}, nil
}
func (a *actionListenerImpl) Actions(ctx context.Context, errCh chan<- error) (<-chan *Action, error) {
ch := make(chan *Action)
a.l.Debug().Msgf("Starting to listen for actions")
go func() {
for {
assignedAction, err := a.listenClient.Recv()
if err != nil {
// if context is cancelled, unsubscribe and close the channel
if ctx.Err() != nil {
a.l.Debug().Msgf("Context cancelled, closing channel")
defer close(ch)
err := a.listenClient.CloseSend()
if err != nil {
a.l.Error().Msgf("Failed to close send: %v", err)
errCh <- fmt.Errorf("failed to close send: %w", err)
}
return
}
statusErr, isStatusErr := status.FromError(err)
// latter case handles errors like `rpc error: code = Unavailable desc = error reading from server: EOF`
// which apparently is not an EOF error
if errors.Is(err, io.EOF) || (isStatusErr && statusErr.Code() == codes.Unavailable) {
err = a.retrySubscribe(ctx)
if err != nil {
a.l.Error().Msgf("Failed to subscribe: %v", err)
errCh <- fmt.Errorf("failed to subscribe: %w", err)
return
}
continue
}
a.l.Error().Msgf("Failed to receive message: %v", err)
errCh <- fmt.Errorf("failed to receive message: %w", err)
return
}
var actionType ActionType
switch assignedAction.ActionType {
case dispatchercontracts.ActionType_START_STEP_RUN:
actionType = ActionTypeStartStepRun
case dispatchercontracts.ActionType_CANCEL_STEP_RUN:
actionType = ActionTypeCancelStepRun
default:
a.l.Error().Msgf("Unknown action type: %s", assignedAction.ActionType)
continue
}
a.l.Debug().Msgf("Received action type: %s", actionType)
ch <- &Action{
TenantId: assignedAction.TenantId,
WorkerId: a.workerId,
JobId: assignedAction.JobId,
JobName: assignedAction.JobName,
JobRunId: assignedAction.JobRunId,
StepId: assignedAction.StepId,
StepRunId: assignedAction.StepRunId,
ActionId: assignedAction.ActionId,
ActionType: actionType,
ActionPayload: func(target interface{}) error {
if err := a.v.Validate(target); err != nil {
return err
}
unquoted, err := strconv.Unquote(assignedAction.ActionPayload)
if err != nil {
return err
}
return json.Unmarshal([]byte(unquoted), target)
},
}
}
}()
return ch, nil
}
func (a *actionListenerImpl) retrySubscribe(ctx context.Context) error {
retries := 0
for retries < DefaultActionListenerRetryCount {
time.Sleep(DefaultActionListenerRetryInterval)
listenClient, err := a.client.Listen(ctx, &dispatchercontracts.WorkerListenRequest{
TenantId: a.tenantId,
WorkerId: a.workerId,
})
if err != nil {
retries++
a.l.Error().Err(err).Msgf("could not subscribe to the worker")
continue
}
a.listenClient = listenClient
return nil
}
return fmt.Errorf("could not subscribe to the worker after %d retries", retries)
}
func (a *actionListenerImpl) Unregister() error {
_, err := a.client.Unsubscribe(
context.Background(),
&dispatchercontracts.WorkerUnsubscribeRequest{
TenantId: a.tenantId,
WorkerId: a.workerId,
},
)
if err != nil {
return err
}
return nil
}
func (d *dispatcherClientImpl) GetActionListener(ctx context.Context, req *GetActionListenerRequest) (WorkerActionListener, error) {
return d.newActionListener(ctx, req)
}
func (d *dispatcherClientImpl) SendActionEvent(ctx context.Context, in *ActionEvent) (*ActionEventResponse, error) {
// validate the request
if err := d.v.Validate(in); err != nil {
return nil, err
}
payloadBytes, err := json.Marshal(in.EventPayload)
if err != nil {
return nil, err
}
var actionEventType dispatchercontracts.ActionEventType
switch in.EventType {
case ActionEventTypeStarted:
actionEventType = dispatchercontracts.ActionEventType_STEP_EVENT_TYPE_STARTED
case ActionEventTypeCompleted:
actionEventType = dispatchercontracts.ActionEventType_STEP_EVENT_TYPE_COMPLETED
case ActionEventTypeFailed:
actionEventType = dispatchercontracts.ActionEventType_STEP_EVENT_TYPE_FAILED
default:
actionEventType = dispatchercontracts.ActionEventType_STEP_EVENT_TYPE_UNKNOWN
}
resp, err := d.client.SendActionEvent(ctx, &dispatchercontracts.ActionEvent{
TenantId: d.tenantId,
WorkerId: in.WorkerId,
JobId: in.JobId,
JobRunId: in.JobRunId,
StepId: in.StepId,
StepRunId: in.StepRunId,
ActionId: in.ActionId,
EventTimestamp: timestamppb.New(*in.EventTimestamp),
EventType: actionEventType,
EventPayload: string(payloadBytes),
})
if err != nil {
return nil, err
}
return &ActionEventResponse{
TenantId: resp.TenantId,
WorkerId: resp.WorkerId,
}, nil
}