-
Notifications
You must be signed in to change notification settings - Fork 200
/
await_signals_workflow.go
165 lines (152 loc) · 5.05 KB
/
await_signals_workflow.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
package await_signals
import (
"go.temporal.io/sdk/temporal"
"time"
"go.temporal.io/sdk/workflow"
)
/**
* The sample demonstrates how to deal with multiple signals that can come out of order and require actions
* if a certain signal not received in a specified time interval.
*
* This specific sample receives three signals: Signal1, Signal2, Signal3. They have to be processed in the
* sequential order, but they can be received out of order.
* There are two timeouts to enforce.
* The first one is the maximum time between signals.
* The second limits the total time since the first signal received.
*
* A naive implementation of such use case would use a single loop that contains a Selector to listen on three
* signals and a timer. Something like:
* for {
* selector := workflow.NewSelector(ctx)
* selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal1"), func(c workflow.ReceiveChannel, more bool) {
* // Process signal1
* })
* selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal2"), func(c workflow.ReceiveChannel, more bool) {
* // Process signal2
* }
* selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal3"), func(c workflow.ReceiveChannel, more bool) {
* // Process signal3
* }
* cCtx, cancel := workflow.WithCancel(ctx)
* timer := workflow.NewTimer(cCtx, timeToNextSignal)
* selector.AddFuture(timer, func(f workflow.Future) {
* // Process timeout
* })
* selector.Select(ctx)
* cancel()
* // break out of the loop on certain condition
* }
*
* The above implementation works. But it quickly becomes pretty convoluted if the number of signals
* and rules around order of their arrivals and timeouts increases.
*
* The following example demonstrates an alternative approach. It receives signals in a separate goroutine.
* Each signal handler just updates a correspondent shared variable with the signal data.
* The main workflow function awaits the next step using `workflow.AwaitWithTimeout` using condition composed of
* the shared variables. This makes the main workflow method free from signal callbacks and makes the business logic
* clear.
*/
// SignalToSignalTimeout is them maximum time between signals
var SignalToSignalTimeout = 30 * time.Second
// FromFirstSignalTimeout is the maximum time to receive all signals
var FromFirstSignalTimeout = 60 * time.Second
type AwaitSignals struct {
FirstSignalTime time.Time
Signal1Received bool
Signal2Received bool
Signal3Received bool
}
// Listen to signals Signal1, Signal2, and Signal3
func (a *AwaitSignals) Listen(ctx workflow.Context) {
log := workflow.GetLogger(ctx)
for {
selector := workflow.NewSelector(ctx)
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal1"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal1Received = true
log.Info("Signal1 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal2"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal2Received = true
log.Info("Signal2 Received")
})
selector.AddReceive(workflow.GetSignalChannel(ctx, "Signal3"), func(c workflow.ReceiveChannel, more bool) {
c.Receive(ctx, nil)
a.Signal3Received = true
log.Info("Signal3 Received")
})
selector.Select(ctx)
if a.FirstSignalTime.IsZero() {
a.FirstSignalTime = workflow.Now(ctx)
}
}
}
// GetNextTimeout returns the maximum time allowed to wait for the next signal.
func (a *AwaitSignals) GetNextTimeout(ctx workflow.Context) (time.Duration, error) {
if a.FirstSignalTime.IsZero() {
panic("FirstSignalTime is not yet set")
}
total := workflow.Now(ctx).Sub(a.FirstSignalTime)
totalLeft := FromFirstSignalTimeout - total
if totalLeft <= 0 {
return 0, temporal.NewApplicationError("FromFirstSignalTimeout", "timeout")
}
if SignalToSignalTimeout < totalLeft {
return SignalToSignalTimeout, nil
}
return totalLeft, nil
}
// AwaitSignalsWorkflow workflow definition
func AwaitSignalsWorkflow(ctx workflow.Context) (err error) {
log := workflow.GetLogger(ctx)
var a AwaitSignals
// Listen to signals in a different goroutine
workflow.Go(ctx, a.Listen)
// Wait for Signal1
err = workflow.Await(ctx, func() bool {
return a.Signal1Received
})
// Cancellation
if err != nil {
return
}
log.Info("Signal1 Processed")
// Wait for Signal2
timeout, err := a.GetNextTimeout(ctx)
// No time left. At this point this cannot really happen.
if err != nil {
return
}
ok, err := workflow.AwaitWithTimeout(ctx, timeout, func() bool {
return a.Signal2Received
})
// Cancellation
if err != nil {
return
}
// timeout
if !ok {
return temporal.NewApplicationError("Timed out waiting for signal2", "timeout")
}
log.Info("Signal2 Processed")
// Wait for Signal3
timeout, err = a.GetNextTimeout(ctx)
// No time left.
if err != nil {
return
}
ok, err = workflow.AwaitWithTimeout(ctx, timeout, func() bool {
return a.Signal3Received
})
// Cancellation
if err != nil {
return
}
// timeout
if !ok {
return temporal.NewApplicationError("Timed out waiting for signal3", "timeout")
}
log.Info("Signal3 Processed")
return nil
}