|
| 1 | +# Choice Sample |
| 2 | + |
| 3 | +This sample demonstrates **conditional workflow logic** - executing different activities based on runtime decisions. |
| 4 | + |
| 5 | +> **Looking for a visual guide?** See [new_samples/hello_world](../../../../new_samples/hello_world/) for a step-by-step tutorial with screenshots. |
| 6 | +
|
| 7 | +## Two Patterns |
| 8 | + |
| 9 | +### 1. Exclusive Choice (`-c single`) |
| 10 | +Execute ONE activity based on a decision: |
| 11 | + |
| 12 | +``` |
| 13 | +┌───────────────┐ |
| 14 | +│ getOrder() │ |
| 15 | +│ returns "apple"│ |
| 16 | +└───────┬───────┘ |
| 17 | + │ |
| 18 | + ┌────┴────┬────────┬────────┐ |
| 19 | + ▼ ▼ ▼ ▼ |
| 20 | +┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ |
| 21 | +│apple │ │banana│ │cherry│ │orange│ |
| 22 | +│ ✓ │ │ │ │ │ │ │ |
| 23 | +└──────┘ └──────┘ └──────┘ └──────┘ |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Multi Choice (`-c multi`) |
| 27 | +Execute MULTIPLE activities in parallel based on a basket: |
| 28 | + |
| 29 | +``` |
| 30 | +┌───────────────────┐ |
| 31 | +│ getBasketOrder() │ |
| 32 | +│ returns ["apple", │ |
| 33 | +│ "cherry","orange"]│ |
| 34 | +└─────────┬─────────┘ |
| 35 | + ┌─────┼─────┐ |
| 36 | + ▼ ▼ ▼ |
| 37 | +┌──────┐┌──────┐┌──────┐ |
| 38 | +│apple ││cherry││orange│ (parallel) |
| 39 | +│ ✓ ││ ✓ ││ ✓ │ |
| 40 | +└──────┘└──────┘└──────┘ |
| 41 | +``` |
| 42 | + |
| 43 | +## Prerequisites |
| 44 | + |
| 45 | +1. Cadence server running (see [main README](../../../../README.md)) |
| 46 | +2. Build the samples: `make` |
| 47 | + |
| 48 | +## Running the Sample |
| 49 | + |
| 50 | +```bash |
| 51 | +# Terminal 1: Start worker |
| 52 | +./bin/choice -m worker |
| 53 | + |
| 54 | +# Terminal 2: Trigger workflows |
| 55 | +./bin/choice -m trigger -c single # Exclusive choice (default) |
| 56 | +./bin/choice -m trigger -c multi # Multi choice |
| 57 | +``` |
| 58 | + |
| 59 | +## Key Code |
| 60 | + |
| 61 | +### Exclusive Choice |
| 62 | +```go |
| 63 | +var orderChoice string |
| 64 | +workflow.ExecuteActivity(ctx, getOrderActivity).Get(ctx, &orderChoice) |
| 65 | + |
| 66 | +switch orderChoice { |
| 67 | +case "apple": |
| 68 | + workflow.ExecuteActivity(ctx, orderAppleActivity, orderChoice) |
| 69 | +case "banana": |
| 70 | + workflow.ExecuteActivity(ctx, orderBananaActivity, orderChoice) |
| 71 | +// ... |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Multi Choice |
| 76 | +```go |
| 77 | +var choices []string |
| 78 | +workflow.ExecuteActivity(ctx, getBasketOrderActivity).Get(ctx, &choices) |
| 79 | + |
| 80 | +var futures []workflow.Future |
| 81 | +for _, item := range choices { |
| 82 | + switch item { |
| 83 | + case "apple": |
| 84 | + f = workflow.ExecuteActivity(ctx, orderAppleActivity, item) |
| 85 | + // ... |
| 86 | + } |
| 87 | + futures = append(futures, f) |
| 88 | +} |
| 89 | + |
| 90 | +// Wait for all |
| 91 | +for _, future := range futures { |
| 92 | + future.Get(ctx, nil) |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## Testing |
| 97 | + |
| 98 | +```bash |
| 99 | +go test -v ./cmd/samples/recipes/choice/ |
| 100 | +``` |
| 101 | + |
| 102 | +## References |
| 103 | + |
| 104 | +- [Cadence Documentation](https://cadenceworkflow.io) |
| 105 | + |
0 commit comments