forked from temporalio/samples-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 2.44 KB
/
main.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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
synchronousproxy "github.com/temporalio/samples-go/synchronous-proxy"
"go.temporal.io/sdk/client"
)
func main() {
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()
fmt.Println("T-Shirt Order")
status, err := CreateOrder(c)
if err != nil {
log.Fatalln("Unable to create order", err)
}
for {
email := PromptAndReadInput("Please enter you email address:")
status, err = UpdateOrder(c, status.OrderID, synchronousproxy.RegisterStage, email)
if err != nil {
log.Println("invalid email", err)
continue
}
break
}
for {
size := PromptAndReadInput("Please enter your requested size:")
status, err = UpdateOrder(c, status.OrderID, synchronousproxy.SizeStage, size)
if err != nil {
log.Println("invalid size", err)
continue
}
break
}
for {
color := PromptAndReadInput("Please enter your required tshirt color:")
status, err = UpdateOrder(c, status.OrderID, synchronousproxy.ColorStage, color)
if err != nil {
log.Println("invalid color", err)
continue
}
break
}
fmt.Println("Thanks for your order!")
fmt.Println("You will receive an email with shipping details shortly")
}
func PromptAndReadInput(prompt string) string {
fmt.Println(prompt)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
return strings.TrimSpace(text)
}
func CreateOrder(c client.Client) (synchronousproxy.OrderStatus, error) {
workflowOptions := client.StartWorkflowOptions{
TaskQueue: "ui-driven",
}
ctx := context.Background()
var status synchronousproxy.OrderStatus
we, err := c.ExecuteWorkflow(ctx, workflowOptions, synchronousproxy.OrderWorkflow)
if err != nil {
return status, fmt.Errorf("unable to execute order workflow: %w", err)
}
status.OrderID = we.GetID()
return status, nil
}
func UpdateOrder(c client.Client, orderID string, stage string, value string) (synchronousproxy.OrderStatus, error) {
workflowOptions := client.StartWorkflowOptions{
TaskQueue: "ui-driven",
}
ctx := context.Background()
status := synchronousproxy.OrderStatus{OrderID: orderID}
we, err := c.ExecuteWorkflow(ctx, workflowOptions, synchronousproxy.UpdateOrderWorkflow, orderID, stage, value)
if err != nil {
return status, fmt.Errorf("unable to execute workflow: %w", err)
}
err = we.Get(ctx, &status)
if err != nil {
return status, err
}
return status, nil
}