|
| 1 | +import CodeBlock from '@theme/CodeBlock'; |
| 2 | +import Proto from '!!raw-loader!../../../examples/shoppingcart/proto/example/shoppingcart/v1/shoppingcart.proto'; |
| 3 | +import Implementation from '!!raw-loader!../../../examples/shoppingcart/workflows.go'; |
| 4 | +import Main from '!!raw-loader!../../../examples/shoppingcart/cmd/shoppingcart/main.go'; |
| 5 | + |
| 6 | + |
| 7 | +# Shopping Cart |
| 8 | + |
| 9 | +A stateful shopping cart example demonstrating workflows with queries, signals, updates, and cross-namespace activities. |
| 10 | + |
| 11 | +This example showcases: |
| 12 | +- **Stateful workflows** that maintain cart state across updates |
| 13 | +- **Queries** to inspect current cart contents |
| 14 | +- **Signals** to trigger checkout |
| 15 | +- **Updates** to modify cart contents with validation |
| 16 | +- **Cross-namespace execution** with update-with-start patterns |
| 17 | +- **Continue-as-new** handling for long-running workflows |
| 18 | + |
| 19 | +<CodeBlock language="protobuf" title="shoppingcart.proto">{Proto}</CodeBlock> |
| 20 | + |
| 21 | +<CodeBlock language="go" title="workflows.go">{Implementation}</CodeBlock> |
| 22 | + |
| 23 | +<CodeBlock language="go" title="main.go">{Main}</CodeBlock> |
| 24 | + |
| 25 | + |
| 26 | +## Run this example |
| 27 | + |
| 28 | +1. Clone the examples |
| 29 | + ```sh |
| 30 | + git clone https://github.com/cludden/protoc-gen-go-temporal && cd protoc-gen-go-temporal |
| 31 | + ``` |
| 32 | +2. Run a local Temporal server |
| 33 | + ```sh |
| 34 | + temporal server start-dev |
| 35 | + ``` |
| 36 | +3. Start the example workers |
| 37 | + ```sh |
| 38 | + go run examples/shoppingcart/cmd/shoppingcart/main.go start |
| 39 | + ``` |
| 40 | +4. In a different shell, start a shopping cart workflow and add an item |
| 41 | + ```sh |
| 42 | + go run examples/shoppingcart/cmd/shoppingcart/main.go shopping-cart-with-update-cart \ |
| 43 | + --req='{}' \ |
| 44 | + --update-cart-input='{"action": "UPDATE_CART_ACTION_ADD", "itemId": "apple"}' \ |
| 45 | + -d |
| 46 | + ``` |
| 47 | +5. Query the cart contents |
| 48 | + ```sh |
| 49 | + go run examples/shoppingcart/cmd/shoppingcart/main.go describe \ |
| 50 | + -w <workflow-id-from-step-4> |
| 51 | + ``` |
| 52 | +6. Add more items using updates |
| 53 | + ```sh |
| 54 | + go run examples/shoppingcart/cmd/shoppingcart/main.go update-cart \ |
| 55 | + -w <workflow-id-from-step-4> \ |
| 56 | + --input='{"action": "UPDATE_CART_ACTION_ADD", "itemId": "banana"}' |
| 57 | + ``` |
| 58 | +7. Checkout the cart (completes the workflow) |
| 59 | + ```sh |
| 60 | + go run examples/shoppingcart/cmd/shoppingcart/main.go checkout \ |
| 61 | + -w <workflow-id-from-step-4> |
| 62 | + ``` |
| 63 | + |
| 64 | +## Key Features Demonstrated |
| 65 | + |
| 66 | +### Stateful Workflow |
| 67 | +The `ShoppingCart` workflow maintains cart state across its execution, persisting item quantities in a map. |
| 68 | + |
| 69 | +### Query Support |
| 70 | +The `Describe` query allows external inspection of the current cart state without affecting workflow execution. |
| 71 | + |
| 72 | +### Signal Handling |
| 73 | +The `Checkout` signal triggers workflow completion, demonstrating asynchronous communication patterns. |
| 74 | + |
| 75 | +### Update with Validation |
| 76 | +The `UpdateCart` update includes validation logic that prevents invalid operations (e.g., removing items not in the cart). |
| 77 | + |
| 78 | +### Cross-Namespace Activities |
| 79 | +The example demonstrates cross-namespace execution patterns using update-with-start, allowing workflows to be initiated from different namespaces. |
| 80 | + |
| 81 | +### Continue-as-New |
| 82 | +The workflow monitors for continue-as-new suggestions and handles them gracefully, ensuring long-running cart sessions don't hit history size limits. |
0 commit comments