-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpizza.go
137 lines (121 loc) · 3.02 KB
/
pizza.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"time"
"github.com/gorchestrate/async"
"github.com/gorchestrate/gasync"
)
// syntax sugar to make workflow definition more readable and less repetitive
var S = async.S
var If = async.If
var Switch = async.Switch
var Case = async.Case
var Step = async.Step
var For = async.For
var On = async.On
var Go = async.Go
var Wait = async.Wait
var WaitFor = async.WaitFor
var Return = async.Return
var Break = async.Break
type Empty struct {
}
type Pizza struct {
Name string
Qty int
}
type CookingRecord struct {
CookName string
}
type LogRecord struct {
Time time.Time
Log string
}
type ConfirmRecord struct {
ManagerName string
Amount float64
}
type PizzaOrderWorkflow struct {
Cart []Pizza
Status string
PaidAmount float64
Amount float64
Location string
CookName string
ManagerName string
}
type PaymentRecord struct {
PaidAmount float64
DeliveryAddress string
}
func (wf *PizzaOrderWorkflow) Definition() async.Section {
return S(
Step("setup", func() error {
wf.Status = "setup"
return nil
}),
For("order not yet submitted", wf.Status != "submitted",
Wait("for user input",
gs.Timeout("cart timed out", 24*3600*time.Second, S(
Return(), //stop workflow
)),
gasync.Event("Customer", "AddToCart", func(in Pizza) (PizzaOrderWorkflow, error) {
wf.Cart = append(wf.Cart, in)
return *wf, nil
}),
gasync.Event("Customer", "EmptyCart", func(in Empty) (PizzaOrderWorkflow, error) {
wf.Cart = []Pizza{}
return *wf, nil
}),
gasync.Event("Customer", "SubmitCart", func(in Empty) (PizzaOrderWorkflow, error) {
wf.Status = "submitted"
return *wf, nil
}),
),
),
Wait("manager to confirm order",
gasync.Event("Manager", "ConfirmOrder", func(in ConfirmRecord) (PizzaOrderWorkflow, error) {
wf.Status = "confirmed"
wf.ManagerName = in.ManagerName
wf.Amount = in.Amount
return *wf, nil
}),
),
Go("customer pays while order is cooking", S(
Wait("customer pays money",
gasync.Event("Manager", "ConfirmPayment", func(in PaymentRecord) (PizzaOrderWorkflow, error) {
wf.PaidAmount = in.PaidAmount
wf.Location = in.DeliveryAddress
return *wf, nil
}),
),
)),
Wait("for kitchen to take order",
gasync.Event("Cook", "StartCooking", func(in CookingRecord) (PizzaOrderWorkflow, error) {
wf.Status = "cooking"
wf.CookName = in.CookName
return *wf, nil
}),
),
Wait("pizzas to be cooked",
gasync.Event("Cook", "Cooked", func(in Empty) (PizzaOrderWorkflow, error) {
wf.Status = "cooked"
return *wf, nil
}),
),
Wait("to be taken for delivery",
gasync.Event("Delivery", "TakeForDelivery", func(in Empty) (PizzaOrderWorkflow, error) {
wf.Status = "delivering"
return *wf, nil
}),
),
Wait("for delivered",
gasync.Event("Delivery", "Delivered", func(in Empty) (PizzaOrderWorkflow, error) {
wf.Status = "delivered"
return *wf, nil
}),
),
WaitFor("payment", wf.PaidAmount > 0, func() {
wf.Status = "completed"
}),
)
}