Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
293 lines (269 loc) · 5.73 KB
/
Copy pathclient.go
File metadata and controls
293 lines (269 loc) · 5.73 KB
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
// Legacy Materials in this file remain under their original licenses.
// See LEGACY_NOTICES.md.
// Modifications Copyright (c) 2026 Super Durable, Inc.
//
// Modifications after the Legacy Cutoff are licensed under the
// Sustainable Use License 1.0.
// Legacy Materials remain under their original licenses.
// See LICENSE and LEGACY_NOTICES.md.
package main
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/superdurable/dex/sdk-go/dex"
"github.com/superdurable/dex/sdk-go/dex/ptr"
)
type UpdateOrderInput struct {
Status string
}
type UpdateOrderOutput struct {
Status string
}
func (OrderFlow) UpdateOrder(
ctx dex.Context,
input UpdateOrderInput,
) (*dex.RPCResult[UpdateOrderOutput], error) {
if err := OrderStatus.Set(ctx, input.Status); err != nil {
return nil, err
}
return &dex.RPCResult[UpdateOrderOutput]{
Output: UpdateOrderOutput{Status: input.Status},
}, nil
}
var _ dex.RPC[UpdateOrderInput, UpdateOrderOutput] = Orders.UpdateOrder
// startOrder retries one start with the same startAttempt; a rerun after a failed run uses the next one.
func startOrder(
ctx context.Context,
client *dex.Client,
flowID string,
startAttempt int,
) (string, error) {
initialStatus, err := dex.InitialAttribute(OrderStatus, "created")
if err != nil {
return "", err
}
initialQuantity, err := dex.InitialAttributeMapValue(
ItemQuantities,
"sku-1",
2,
)
if err != nil {
return "", err
}
return client.StartFlow(
ctx,
Orders,
flowID,
OrderInput{OrderID: flowID},
dex.StartFlowOptions{
Timeout: ptr.Any(24 * time.Hour),
IDReusePolicy: dex.IDReuseAllowIfPreviousFailed,
StartDelay: ptr.Any(5 * time.Second),
RetryPolicy: &dex.FlowRetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2,
MaximumInterval: time.Minute,
MaximumAttempts: 3,
},
Attributes: []dex.InitialAttributeDef{
initialStatus,
initialQuantity,
},
ConfigOverride: &dex.FlowConfig{
ActiveStepSearchMode: ptr.Any(dex.SearchAllActiveSteps),
StepDurability: ptr.Any(dex.StepDurabilitySync),
},
AlreadyStarted: &dex.AlreadyStartedOptions{
IgnoreError: true,
},
RequestID: ptr.Any(fmt.Sprintf("start-order/%s/%d", flowID, startAttempt)),
},
)
}
func writeOrderProgress(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
return client.WriteStream(
ctx,
flowID,
OrderProgress,
"frontend/started",
"waiting for inventory",
)
}
func readOrderProgress(
ctx context.Context,
client *dex.Client,
flowID string,
resumeToken string,
) (string, dex.StreamMessage, error) {
var progress string
message, err := client.ReadStream(
ctx,
flowID,
OrderProgress,
resumeToken,
&progress,
)
return progress, message, err
}
func invokeUpdateOrder(
ctx context.Context,
client *dex.Client,
flowID string,
) (UpdateOrderOutput, error) {
var output UpdateOrderOutput
err := client.InvokeRPC(
ctx,
flowID,
Orders.UpdateOrder,
UpdateOrderInput{Status: "processing"},
&output,
)
return output, err
}
func waitForOrderStatus(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
var matched string
return client.WaitForAttributeMatch(
ctx,
flowID,
OrderStatus,
dex.AttributeMatchEqual("shipped"),
&matched,
dex.WaitForAttributeOptions{RequestID: uuid.NewString(), RequestTimeout: 10 * time.Second},
)
}
func waitForItemQuantity(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
var matched int
return client.WaitForAttributeMapInstanceMatch(
ctx,
flowID,
ItemQuantities,
"sku-1",
dex.AttributeMatchEqual(3),
&matched,
dex.WaitForAttributeOptions{RequestID: uuid.NewString(), RequestTimeout: 10 * time.Second},
)
}
func stopOrder(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
return client.StopFlow(
ctx,
flowID,
dex.StopOptions{
Type: dex.CancelFlow,
Reason: "customer requested cancellation",
},
)
}
func waitForOrder(
ctx context.Context,
client *dex.Client,
flowID string,
) (dex.FlowResult, error) {
return client.WaitForFlow(
ctx,
flowID,
dex.WaitForFlowOptions{
NeedsResults: true,
},
)
}
func searchOrders(
ctx context.Context,
client *dex.Client,
) (dex.SearchFlowsPage, error) {
return client.SearchFlows(
ctx,
"order-status = 'shipped'",
100,
"",
)
}
func timeTravelOrder(
ctx context.Context,
client *dex.Client,
flowID string,
) (string, error) {
return client.TimeTravel(
ctx,
flowID,
dex.TimeTravelOptions{
Type: dex.TimeTravelToBeginning,
Reason: "reprocess order",
},
)
}
func skipOrderTimer(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
return client.SkipTimer(
ctx,
flowID,
dex.StepExecutionID{
StepType: dex.GetFinalStepType(WaitForCommand),
},
dex.TimerID{ConditionID: "timeout"},
)
}
func updateOrderConfig(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
return client.UpdateFlowConfig(
ctx,
flowID,
dex.FlowConfig{
StepDurability: ptr.Any(dex.StepDurabilityAsync),
},
)
}
func waitForOrderStep(
ctx context.Context,
client *dex.Client,
flowID string,
) error {
return client.WaitForStepCompletion(
ctx,
flowID,
dex.StepExecutionID{
StepType: dex.GetFinalStepType(WaitForCommand),
},
dex.WaitForStepCompletionOptions{RequestID: uuid.NewString(), RequestTimeout: 10 * time.Second},
)
}
func checkHealth(
ctx context.Context,
client *dex.Client,
) (dex.HealthInfo, error) {
return client.HealthCheck(ctx)
}
func decodeOrderStatus(values map[string]dex.Value) (string, error) {
value, found := values[OrderStatus.AttributeName()]
if !found {
return "", fmt.Errorf("order status is missing")
}
var status string
if err := value.Decode(&status); err != nil {
return "", err
}
return status, nil
}