-
Notifications
You must be signed in to change notification settings - Fork 157
/
dispatcher.go
431 lines (330 loc) · 10.6 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"time"
"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"
dispatchercontracts "github.com/hatchet-dev/hatchet/internal/services/dispatcher/contracts"
"github.com/hatchet-dev/hatchet/internal/validator"
)
type DispatcherClient interface {
GetActionListener(ctx context.Context, req *GetActionListenerRequest) (WorkerActionListener, error)
SendStepActionEvent(ctx context.Context, in *ActionEvent) (*ActionEventResponse, error)
SendGroupKeyActionEvent(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
MaxRuns *int
}
// 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"
ActionTypeStartGetGroupKey ActionType = "START_GET_GROUP_KEY"
)
type Action struct {
// the worker id
WorkerId string
// the tenant id
TenantId string
// the workflow run id
WorkflowRunId string
// the get group key run id
GetGroupKeyRunId 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 []byte
// the action type
ActionType ActionType
}
type WorkerActionListener interface {
Actions(ctx context.Context) (<-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
ctx *contextLoader
}
func newDispatcher(conn *grpc.ClientConn, opts *sharedClientOpts) DispatcherClient {
return &dispatcherClientImpl{
client: dispatchercontracts.NewDispatcherClient(conn),
tenantId: opts.tenantId,
l: opts.l,
v: opts.v,
ctx: opts.ctxLoader,
}
}
type actionListenerImpl struct {
client dispatchercontracts.DispatcherClient
tenantId string
listenClient dispatchercontracts.Dispatcher_ListenClient
workerId string
l *zerolog.Logger
v validator.Validator
ctx *contextLoader
}
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
}
registerReq := &dispatchercontracts.WorkerRegisterRequest{
WorkerName: req.WorkerName,
Actions: req.Actions,
Services: req.Services,
}
if req.MaxRuns != nil {
mr := int32(*req.MaxRuns)
registerReq.MaxRuns = &mr
}
// register the worker
resp, err := d.client.Register(d.ctx.newContext(ctx), registerReq)
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(d.ctx.newContext(ctx), &dispatchercontracts.WorkerListenRequest{
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,
ctx: d.ctx,
}, nil
}
func (a *actionListenerImpl) Actions(ctx context.Context) (<-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)
panic(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)
panic(fmt.Errorf("failed to subscribe: %w", err))
}
continue
}
a.l.Error().Msgf("Failed to receive message: %v", err)
panic(fmt.Errorf("failed to receive message: %w", err))
}
var actionType ActionType
switch assignedAction.ActionType {
case dispatchercontracts.ActionType_START_STEP_RUN:
actionType = ActionTypeStartStepRun
case dispatchercontracts.ActionType_CANCEL_STEP_RUN:
actionType = ActionTypeCancelStepRun
case dispatchercontracts.ActionType_START_GET_GROUP_KEY:
actionType = ActionTypeStartGetGroupKey
default:
a.l.Error().Msgf("Unknown action type: %s", assignedAction.ActionType)
continue
}
a.l.Debug().Msgf("Received action type: %s", actionType)
unquoted, err := strconv.Unquote(assignedAction.ActionPayload)
if err != nil {
unquoted = assignedAction.ActionPayload
}
ch <- &Action{
TenantId: assignedAction.TenantId,
WorkflowRunId: assignedAction.WorkflowRunId,
GetGroupKeyRunId: assignedAction.GetGroupKeyRunId,
WorkerId: a.workerId,
JobId: assignedAction.JobId,
JobName: assignedAction.JobName,
JobRunId: assignedAction.JobRunId,
StepId: assignedAction.StepId,
StepRunId: assignedAction.StepRunId,
ActionId: assignedAction.ActionId,
ActionType: actionType,
ActionPayload: []byte(unquoted),
}
}
}()
return ch, nil
}
func (a *actionListenerImpl) retrySubscribe(ctx context.Context) error {
retries := 0
for retries < DefaultActionListenerRetryCount {
time.Sleep(DefaultActionListenerRetryInterval)
listenClient, err := a.client.Listen(a.ctx.newContext(ctx), &dispatchercontracts.WorkerListenRequest{
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(
a.ctx.newContext(context.Background()),
&dispatchercontracts.WorkerUnsubscribeRequest{
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) SendStepActionEvent(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.StepActionEventType
switch in.EventType {
case ActionEventTypeStarted:
actionEventType = dispatchercontracts.StepActionEventType_STEP_EVENT_TYPE_STARTED
case ActionEventTypeCompleted:
actionEventType = dispatchercontracts.StepActionEventType_STEP_EVENT_TYPE_COMPLETED
case ActionEventTypeFailed:
actionEventType = dispatchercontracts.StepActionEventType_STEP_EVENT_TYPE_FAILED
default:
actionEventType = dispatchercontracts.StepActionEventType_STEP_EVENT_TYPE_UNKNOWN
}
resp, err := d.client.SendStepActionEvent(d.ctx.newContext(ctx), &dispatchercontracts.StepActionEvent{
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
}
func (d *dispatcherClientImpl) SendGroupKeyActionEvent(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.GroupKeyActionEventType
switch in.EventType {
case ActionEventTypeStarted:
actionEventType = dispatchercontracts.GroupKeyActionEventType_GROUP_KEY_EVENT_TYPE_STARTED
case ActionEventTypeCompleted:
actionEventType = dispatchercontracts.GroupKeyActionEventType_GROUP_KEY_EVENT_TYPE_COMPLETED
case ActionEventTypeFailed:
actionEventType = dispatchercontracts.GroupKeyActionEventType_GROUP_KEY_EVENT_TYPE_FAILED
default:
actionEventType = dispatchercontracts.GroupKeyActionEventType_GROUP_KEY_EVENT_TYPE_UNKNOWN
}
resp, err := d.client.SendGroupKeyActionEvent(d.ctx.newContext(ctx), &dispatchercontracts.GroupKeyActionEvent{
WorkerId: in.WorkerId,
WorkflowRunId: in.WorkflowRunId,
GetGroupKeyRunId: in.GetGroupKeyRunId,
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
}