-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathworkflow.go
170 lines (131 loc) · 3.04 KB
/
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
166
167
168
169
170
package synchronousproxy
import (
"time"
"github.com/temporalio/samples-go/synchronous-proxy/proxy"
"go.temporal.io/sdk/workflow"
)
const (
RegisterStage = "register"
SizeStage = "size"
ColorStage = "color"
ShippingStage = "shipping"
)
var (
TShirtSizes = []string{
"small",
"medium",
"large",
}
TShirtColors = []string{
"red",
"blue",
"black",
}
)
type TShirtOrder struct {
Email string
Size string
Color string
}
type OrderStatus struct {
OrderID string
Stage string
}
// Workflow is a workflow driven by interaction from a UI.
func OrderWorkflow(ctx workflow.Context) error {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 5 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
order := TShirtOrder{}
// Loop until we receive a valid email
for {
id, _, email := proxy.ReceiveRequest(ctx)
err := workflow.ExecuteActivity(ctx, RegisterEmail, email).Get(ctx, nil)
if err != nil {
err := proxy.SendErrorResponse(ctx, id, err)
if err != nil {
return err
}
continue
}
order.Email = email
err = proxy.SendResponse(ctx, id, SizeStage, "")
if err != nil {
return err
}
break
}
// Loop until we receive a valid size
for {
id, _, size := proxy.ReceiveRequest(ctx)
err := workflow.ExecuteActivity(ctx, ValidateSize, size).Get(ctx, nil)
if err != nil {
err := proxy.SendErrorResponse(ctx, id, err)
if err != nil {
return err
}
continue
}
order.Size = size
err = proxy.SendResponse(ctx, id, ColorStage, "")
if err != nil {
return err
}
break
}
// Loop until we receive a valid color
for {
id, _, color := proxy.ReceiveRequest(ctx)
err := workflow.ExecuteActivity(ctx, ValidateColor, color).Get(ctx, nil)
if err != nil {
err := proxy.SendErrorResponse(ctx, id, err)
if err != nil {
return err
}
continue
}
order.Color = color
// Tell the UI the order is pending shipping
err = proxy.SendResponse(ctx, id, ShippingStage, "")
if err != nil {
return err
}
break
}
cw := workflow.ExecuteChildWorkflow(ctx, ShippingWorkflow, order)
err := cw.Get(ctx, nil)
if err != nil {
return err
}
return nil
}
func ShippingWorkflow(ctx workflow.Context, order TShirtOrder) error {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 5 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
var deliveryDate time.Time
err := workflow.ExecuteActivity(ctx, ScheduleDelivery, order).Get(ctx, &deliveryDate)
if err != nil {
return err
}
err = workflow.ExecuteActivity(ctx, SendDeliveryEmail, order, deliveryDate).Get(ctx, nil)
if err != nil {
return err
}
return nil
}
func UpdateOrderWorkflow(ctx workflow.Context, orderWorkflowID string, stage string, value string) (OrderStatus, error) {
status := OrderStatus{OrderID: orderWorkflowID, Stage: stage}
err := proxy.SendRequest(ctx, orderWorkflowID, stage, value)
if err != nil {
return status, err
}
nextStage, _, err := proxy.ReceiveResponse(ctx)
if err != nil {
return status, err
}
status.Stage = nextStage
return status, nil
}