-
Notifications
You must be signed in to change notification settings - Fork 8
/
callback.go
81 lines (66 loc) · 2.04 KB
/
callback.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
package workflow
import (
"bytes"
"context"
"io"
)
type callback[Type any, Status StatusType] struct {
CallbackFunc CallbackFunc[Type, Status]
}
type CallbackFunc[Type any, Status StatusType] func(ctx context.Context, r *Run[Type, Status], reader io.Reader) (Status, error)
func (w *Workflow[Type, Status]) Callback(ctx context.Context, foreignID string, status Status, payload io.Reader) error {
updateFn := newUpdater[Type, Status](w.recordStore.Lookup, w.recordStore.Store, w.statusGraph, w.clock)
for _, s := range w.callback[status] {
err := processCallback(ctx, w, status, s.CallbackFunc, foreignID, payload, w.recordStore.Latest, w.recordStore.Store, updateFn)
if err != nil {
return err
}
}
return nil
}
type latestLookup func(ctx context.Context, workflowName, foreignID string) (*Record, error)
func processCallback[Type any, Status StatusType](
ctx context.Context,
w *Workflow[Type, Status],
currentStatus Status,
fn CallbackFunc[Type, Status],
foreignID string,
payload io.Reader,
latest latestLookup,
store storeFunc,
updater updater[Type, Status],
) error {
wr, err := latest(ctx, w.Name, foreignID)
if err != nil {
return err
}
if Status(wr.Status) != currentStatus {
// Latest record shows that the current status is in a different State than expected so skip.
return nil
}
run, err := buildRun[Type, Status](store, wr)
if err != nil {
return err
}
if payload == nil {
// Ensure that an empty value implementation of io.Reader is passed in instead of nil to avoid panic and
// rather allow an unmarshalling error.
payload = bytes.NewReader([]byte{})
}
next, err := fn(ctx, run, payload)
if err != nil {
return err
}
if skipUpdate(next) {
w.logger.maybeDebug(ctx, "skipping update", map[string]string{
"description": skipUpdateDescription(next),
"workflow_name": w.Name,
"foreign_id": run.ForeignID,
"run_id": run.RunID,
"run_state": run.RunState.String(),
"record_status": run.Status.String(),
})
return nil
}
return updater(ctx, currentStatus, next, run)
}