Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (100 loc) · 2.6 KB
/
Copy pathmain.go
File metadata and controls
113 lines (100 loc) · 2.6 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
package main
import (
"context"
"fmt"
"strings"
"sync"
"time"
"github.com/luno/workflow"
)
func main() {
fmt.Println("🚀 Starting Order Processing Workflow Example")
fmt.Println(strings.Repeat("=", 60))
// Create service instances
paymentService := &MockPaymentService{}
inventoryService := &MockInventoryService{}
shippingService := &MockShippingService{}
wf := NewOrderWorkflow(paymentService, inventoryService, shippingService)
ctx := context.Background()
wf.Run(ctx)
defer wf.Stop()
// Create sample orders
orders := []*Order{
{
ID: "order-001",
CustomerID: "customer-123",
Total: 99.99,
PaymentMethod: "credit_card",
Items: []OrderItem{
{ProductID: "laptop", Quantity: 1, Price: 99.99},
},
ShippingAddress: Address{
Street: "123 Main St",
City: "San Francisco",
State: "CA",
ZipCode: "94105",
Country: "USA",
},
CreatedAt: time.Now(),
},
{
ID: "order-002",
CustomerID: "premium_customer",
Total: 1500.00,
PaymentMethod: "credit_card", // Will fail and retry with alternative
Items: []OrderItem{
{ProductID: "desktop", Quantity: 1, Price: 1500.00},
},
ShippingAddress: Address{
Street: "456 Tech Ave",
City: "Austin",
State: "TX",
ZipCode: "78701",
Country: "USA",
},
CreatedAt: time.Now(),
},
{
ID: "order-003",
CustomerID: "customer-456",
Total: 49.99,
PaymentMethod: "credit_card",
Items: []OrderItem{
{ProductID: "out_of_stock", Quantity: 1, Price: 49.99},
},
ShippingAddress: Address{
Street: "789 Commerce Blvd",
City: "Seattle",
State: "WA",
ZipCode: "98101",
Country: "USA",
},
CreatedAt: time.Now(),
},
}
var wg sync.WaitGroup
wg.Add(len(orders))
// Process orders
for _, order := range orders {
fmt.Printf("🚀 [%s] Triggering order workflow\n", order.ID)
runID, err := wf.Trigger(ctx, order.ID, workflow.WithInitialValue[Order, OrderStatus](order))
if err != nil {
panic(err)
}
// Monitor progress (in real app, this would be done asynchronously)
go func(orderID, runId string) {
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
run, err := wf.Await(ctx, orderID, runId, OrderStatusFulfilled)
if err != nil {
panic(err)
}
wg.Done()
fmt.Printf("✅ [%s] Workflow completed with status: %s\n", orderID, run.Status)
}(order.ID, runID)
}
// Let workflows complete
fmt.Println("\n⏳ Waiting for workflows to complete...")
wg.Wait()
fmt.Println("\n🎉 Order Processing Example completed!")
}