-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathworkflow.go
60 lines (52 loc) · 1.55 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
package choice_multi
import (
"errors"
"time"
"go.temporal.io/sdk/workflow"
)
const (
OrderChoiceApple = "apple"
OrderChoiceBanana = "banana"
OrderChoiceCherry = "cherry"
OrderChoiceOrange = "orange"
)
// MultiChoiceWorkflow Workflow definition.
func MultiChoiceWorkflow(ctx workflow.Context) error {
// Get basket order.
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
var orderActivities *OrderActivities // Used to call activities by function pointer
var choices []string
err := workflow.ExecuteActivity(ctx, orderActivities.GetBasketOrder).Get(ctx, &choices)
if err != nil {
return err
}
logger := workflow.GetLogger(ctx)
var futures []workflow.Future
for _, item := range choices {
// choose next activity based on order result
var f workflow.Future
switch item {
case OrderChoiceApple:
f = workflow.ExecuteActivity(ctx, orderActivities.OrderApple, item)
case OrderChoiceBanana:
f = workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, item)
case OrderChoiceCherry:
f = workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, item)
case OrderChoiceOrange:
f = workflow.ExecuteActivity(ctx, orderActivities.OrderOrange, item)
default:
logger.Error("Unexpected order.", "Order", item)
return errors.New("invalid choice-multi")
}
futures = append(futures, f)
}
// wait until all items in the basket order are processed
for _, future := range futures {
_ = future.Get(ctx, nil)
}
logger.Info("Workflow completed.")
return nil
}