-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathworkflow.go
56 lines (46 loc) · 1.42 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
package choice
import (
"fmt"
"time"
"go.temporal.io/sdk/workflow"
)
const (
OrderChoiceApple = "apple"
OrderChoiceBanana = "banana"
OrderChoiceCherry = "cherry"
OrderChoiceOrange = "orange"
)
// ExclusiveChoiceWorkflow Workflow definition.
func ExclusiveChoiceWorkflow(ctx workflow.Context) error {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
// Get order.
var orderActivities *OrderActivities // Used to call activities by function pointer
var orderChoice string
err := workflow.ExecuteActivity(ctx, orderActivities.GetOrder).Get(ctx, &orderChoice)
if err != nil {
return err
}
logger := workflow.GetLogger(ctx)
// choose next activity based on order result
switch orderChoice {
case OrderChoiceApple:
err = workflow.ExecuteActivity(ctx, orderActivities.OrderApple, orderChoice).Get(ctx, nil)
case OrderChoiceBanana:
err = workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, orderChoice).Get(ctx, nil)
case OrderChoiceCherry:
err = workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, orderChoice).Get(ctx, nil)
case OrderChoiceOrange:
// Activity can be also called by its name
err = workflow.ExecuteActivity(ctx, "OrderOrange", orderChoice).Get(ctx, nil)
default:
err = fmt.Errorf("unknown order choice: %v", orderChoice)
}
if err != nil {
return err
}
logger.Info("Workflow completed.")
return nil
}