-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhook.go
144 lines (124 loc) · 3.01 KB
/
hook.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
package workflow
import (
"context"
"errors"
"strconv"
"time"
"k8s.io/utils/clock"
"github.com/luno/workflow/internal/metrics"
)
// RunStateChangeHookFunc defines the function signature for all hooks associated to the run.
type RunStateChangeHookFunc[Type any, Status StatusType] func(ctx context.Context, record *TypedRecord[Type, Status]) error
func runStateChangeHookConsumer[Type any, Status StatusType](
w *Workflow[Type, Status],
runState RunState,
hook RunStateChangeHookFunc[Type, Status],
) {
role := makeRole(
w.Name(),
runState.String(),
"run-state-change-hook",
"consumer",
)
processName := role
w.run(role, processName, func(ctx context.Context) error {
topic := RunStateChangeTopic(w.Name())
consumerStream, err := w.eventStreamer.NewConsumer(
ctx,
topic,
role,
WithConsumerPollFrequency(w.defaultOpts.pollingFrequency),
)
if err != nil {
return err
}
defer consumerStream.Close()
return runHooks(
ctx,
w.Name(),
processName,
consumerStream,
runState,
w.recordStore.Lookup,
hook,
w.defaultOpts.lagAlert,
w.clock,
)
}, w.defaultOpts.errBackOff)
}
func runHooks[Type any, Status StatusType](
ctx context.Context,
workflowName string,
processName string,
c Consumer,
runState RunState,
lookup lookupFunc,
hook RunStateChangeHookFunc[Type, Status],
lagAlert time.Duration,
clock clock.Clock,
) error {
for {
if ctx.Err() != nil {
return ctx.Err()
}
e, ack, err := c.Recv(ctx)
if err != nil {
return err
}
rs, err := strconv.ParseInt(e.Headers[HeaderRunState], 10, 64)
if err != nil {
metrics.ProcessSkippedEvents.WithLabelValues(workflowName, processName, "invalid or missing HeaderRunState").
Inc()
err = ack()
if err != nil {
return err
}
continue
}
if RunState(rs) != runState {
metrics.ProcessSkippedEvents.WithLabelValues(workflowName, processName, "mismatch on current run state and target run state").
Inc()
err = ack()
if err != nil {
return err
}
continue
}
record, err := lookup(ctx, e.ForeignID)
if err != nil {
if errors.Is(err, ErrRecordNotFound) {
metrics.ProcessSkippedEvents.WithLabelValues(workflowName, processName, "record not found").Inc()
err = ack()
if err != nil {
return err
}
continue
} else {
return err
}
}
// Push metrics and alerting around the age of the event being processed.
pushLagMetricAndAlerting(workflowName, processName, e.CreatedAt, lagAlert, clock)
t2 := clock.Now()
var t Type
err = Unmarshal(record.Object, &t)
if err != nil {
metrics.ProcessSkippedEvents.WithLabelValues(workflowName, processName, "unable to unmarshal object").Inc()
err = ack()
if err != nil {
return err
}
continue
}
err = hook(ctx, &TypedRecord[Type, Status]{
Record: *record,
Status: Status(record.Status),
Object: &t,
})
if err != nil {
return err
}
metrics.ProcessLatency.WithLabelValues(workflowName, processName).Observe(clock.Since(t2).Seconds())
return ack()
}
}