/
main.go
144 lines (129 loc) · 3.71 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
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
138
139
140
141
142
143
144
package main
import (
"context"
"flag"
"github.com/google/uuid"
apiv1 "github.com/uber/cadence-idl/go/proto/api/v1"
"go.uber.org/cadence/compatibility"
"go.uber.org/yarpc"
"go.uber.org/yarpc/transport/grpc"
"go.uber.org/zap"
"time"
"go.uber.org/cadence/client"
"go.uber.org/cadence/worker"
)
const (
tasklist0 = "cross-domain-tl0"
tasklist1 = "cross-domain-tl1"
tasklist2 = "cross-domain-tl2"
domain0 = "domain0"
domain1 = "domain1"
domain2 = "domain2"
portCluster0 = "127.0.0.1:7833"
portCluster1 = "127.0.0.1:8833"
portCluster2 = "127.0.0.1:9833"
)
func main() {
var mode string
flag.StringVar(&mode, "m", "trigger", "Mode is worker, trigger.")
flag.Parse()
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
switch mode {
case "worker0":
setupWorker(domain0, tasklist0, portCluster0, []interface{}{wf0}, []interface{}{})
logger.Info("workers running for cluster 0....")
select {}
case "worker1":
setupWorker(domain1, tasklist1, portCluster1, []interface{}{wf1}, []interface{}{activity1})
logger.Info("workers running for cluster 1....")
select {}
case "worker2":
setupWorker(domain2, tasklist2, portCluster2, []interface{}{wf2}, []interface{}{activity2})
logger.Info("workers running for cluster 1....")
select {}
case "start":
client1 := setupClient(domain0, portCluster0)
id := uuid.New().String()
ctx, close := context.WithTimeout(context.Background(), time.Second*30)
defer close()
res, err := client1.StartWorkflow(ctx, client.StartWorkflowOptions{
ID: id,
TaskList: tasklist0,
ExecutionStartToCloseTimeout: 30 * time.Second,
}, wf0)
if err != nil {
logger.Error("error starting workflow", zap.Error(err))
}
logger.Info("started workflow for domain0", zap.String("wf-id", id), zap.Any("start-wf", res))
}
}
func setupClient(domain string, hostport string) client.Client {
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "client",
Outbounds: yarpc.Outbounds{
"cadence-frontend": {Unary: grpc.NewTransport().NewSingleOutbound(hostport)},
},
})
err := dispatcher.Start()
if err != nil {
panic(err)
}
clientConfig := dispatcher.ClientConfig("cadence-frontend")
svc := compatibility.NewThrift2ProtoAdapter(
apiv1.NewDomainAPIYARPCClient(clientConfig),
apiv1.NewWorkflowAPIYARPCClient(clientConfig),
apiv1.NewWorkerAPIYARPCClient(clientConfig),
apiv1.NewVisibilityAPIYARPCClient(clientConfig),
)
return client.NewClient(
svc,
domain,
&client.Options{
FeatureFlags: client.FeatureFlags{
WorkflowExecutionAlreadyCompletedErrorEnabled: true,
},
})
}
func setupWorker(domain string, tl string, hostport string, wfs []interface{}, activities []interface{}) {
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Name: "client",
Outbounds: yarpc.Outbounds{
"cadence-frontend": {Unary: grpc.NewTransport().NewSingleOutbound(hostport)},
},
})
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
err = dispatcher.Start()
if err != nil {
panic(err)
}
workerOptions := worker.Options{
Logger: logger,
FeatureFlags: client.FeatureFlags{
WorkflowExecutionAlreadyCompletedErrorEnabled: true,
},
}
clientConfig := dispatcher.ClientConfig("cadence-frontend")
svc := compatibility.NewThrift2ProtoAdapter(
apiv1.NewDomainAPIYARPCClient(clientConfig),
apiv1.NewWorkflowAPIYARPCClient(clientConfig),
apiv1.NewWorkerAPIYARPCClient(clientConfig),
apiv1.NewVisibilityAPIYARPCClient(clientConfig),
)
worker := worker.New(svc, domain, tl, workerOptions)
for i := range wfs {
worker.RegisterWorkflow(wfs[i])
}
for i := range activities {
worker.RegisterActivity(activities[i])
}
err = worker.Start()
if err != nil {
panic(err)
}
}