Skip to content

Commit 59acf75

Browse files
chopincodeGroxx
andauthored
Add new worker implementation and a hello world workflow with detailed CLI (#77)
* Add new worker implementation and a hello world workflow with detailed CLI * Add comments to public functions and structs * delete types.go file and directly put type definition into workflow implementation * Directly register workflows and activities inside worker instead of globally registration in main * make hello world sample return the value instead of just printing in log * Use fx as worker host * Match definition order when initializing variables Co-authored-by: Steven L <[email protected]> * Update new_samples/README.md Done Co-authored-by: Steven L <[email protected]> * Use cadence-samples as domain name * Use infinite loop for worker process * Use signal to interrupt cadence worker * Update new_samples/README.md Co-authored-by: Steven L <[email protected]> * Update new_samples/main.go Co-authored-by: Steven L <[email protected]> * Update new_samples/README.md Co-authored-by: Steven L <[email protected]> * update readme * Update new_samples/worker/worker.go Co-authored-by: Steven L <[email protected]> * Update new_samples/worker/worker.go Co-authored-by: Steven L <[email protected]> * Update new_samples/worker/worker.go Co-authored-by: Steven L <[email protected]> * Update new_samples/worker/worker.go Co-authored-by: Steven L <[email protected]> --------- Co-authored-by: Steven L <[email protected]>
1 parent ce920bd commit 59acf75

File tree

6 files changed

+201
-19
lines changed

6 files changed

+201
-19
lines changed

go.mod

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@ require (
88
github.com/opentracing/opentracing-go v1.2.0
99
github.com/pborman/uuid v1.2.1
1010
github.com/pkg/errors v0.9.1
11-
github.com/stretchr/testify v1.7.1
11+
github.com/stretchr/testify v1.8.0
1212
github.com/uber-go/tally v3.4.3+incompatible
1313
github.com/uber/cadence-idl v0.0.0-20220505064515-7bb6b0808383
1414
github.com/uber/jaeger-client-go v2.30.0+incompatible
1515
go.uber.org/cadence v0.19.0
1616
go.uber.org/yarpc v1.60.0
17-
go.uber.org/zap v1.21.0
17+
go.uber.org/zap v1.23.0
1818
gopkg.in/yaml.v2 v2.4.0
1919
)
2020

2121
require (
2222
github.com/BurntSushi/toml v0.3.1 // indirect
2323
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 // indirect
2424
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 // indirect
25-
github.com/benbjohnson/clock v1.1.0 // indirect
25+
github.com/benbjohnson/clock v1.3.0 // indirect
2626
github.com/beorn7/perks v1.0.1 // indirect
2727
github.com/cespare/xxhash/v2 v2.1.1 // indirect
2828
github.com/cristalhq/jwt/v3 v3.1.0 // indirect
@@ -46,27 +46,27 @@ require (
4646
github.com/prometheus/common v0.9.1 // indirect
4747
github.com/prometheus/procfs v0.0.9 // indirect
4848
github.com/robfig/cron v1.2.0 // indirect
49-
github.com/stretchr/objx v0.2.0 // indirect
49+
github.com/stretchr/objx v0.4.0 // indirect
5050
github.com/twmb/murmur3 v1.1.6 // indirect
5151
github.com/uber-go/mapdecode v1.0.0 // indirect
5252
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
5353
github.com/uber/tchannel-go v1.22.2 // indirect
5454
go.uber.org/atomic v1.7.0 // indirect
55-
go.uber.org/dig v1.10.0 // indirect
55+
go.uber.org/dig v1.17.0 // indirect
5656
go.uber.org/fx v1.13.1 // indirect
5757
go.uber.org/multierr v1.6.0 // indirect
5858
go.uber.org/net/metrics v1.3.0 // indirect
5959
go.uber.org/thriftrw v1.29.2 // indirect
6060
golang.org/x/lint v0.0.0-20200130185559-910be7a94367 // indirect
6161
golang.org/x/mod v0.4.2 // indirect
6262
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
63-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
63+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
6464
golang.org/x/text v0.3.3 // indirect
6565
golang.org/x/time v0.0.0-20170927054726-6dc17368e09b // indirect
6666
golang.org/x/tools v0.1.5 // indirect
6767
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
6868
google.golang.org/genproto v0.0.0-20200212174721-66ed5ce911ce // indirect
6969
google.golang.org/grpc v1.28.0 // indirect
70-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
70+
gopkg.in/yaml.v3 v3.0.1 // indirect
7171
honnef.co/go/tools v0.0.1-2019.2.3 // indirect
7272
)

go.sum

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo
99
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
1010
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7 h1:Fv9bK1Q+ly/ROk4aJsVMeuIwPel4bEnD8EPiI91nZMg=
1111
github.com/apache/thrift v0.0.0-20161221203622-b2a4d4ae21c7/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
12-
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
13-
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
12+
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
13+
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
1414
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
1515
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
1616
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -163,14 +163,15 @@ github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25 h1:7z3LSn867ex6
163163
github.com/streadway/quantile v0.0.0-20150917103942-b0c588724d25/go.mod h1:lbP8tGiBjZ5YWIc2fzuRpTaz0b/53vT6PEs3QuAWzuU=
164164
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
165165
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
166-
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
167166
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
167+
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
168+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
168169
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
169170
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
170171
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
171-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
172-
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
173172
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
173+
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
174+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
174175
github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg=
175176
github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ=
176177
github.com/uber-common/bark v1.2.1/go.mod h1:g0ZuPcD7XiExKHynr93Q742G/sbrdVQkghrqLGOoFuY=
@@ -203,15 +204,15 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
203204
go.uber.org/cadence v0.19.0 h1:EvDXwIJ0lAAxL2i8ne/vG/TeoJM6xkAyqgTRFmIWG+c=
204205
go.uber.org/cadence v0.19.0/go.mod h1:s91dOf0kcJbumPscRIVFV/4Xq/exhefzpXmnDiRRTxs=
205206
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
206-
go.uber.org/dig v1.10.0 h1:yLmDDj9/zuDjv3gz8GQGviXMs9TfysIUMUilCpgzUJY=
207207
go.uber.org/dig v1.10.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
208+
go.uber.org/dig v1.17.0 h1:5Chju+tUvcC+N7N6EV08BJz41UZuO3BmHcN4A287ZLI=
209+
go.uber.org/dig v1.17.0/go.mod h1:rTxpf7l5I0eBTlE6/9RL+lDybC7WFwY2QH55ZSjy1mU=
208210
go.uber.org/fx v1.10.0/go.mod h1:vLRicqpG/qQEzno4SYU86iCwfT95EZza+Eba0ItuxqY=
209211
go.uber.org/fx v1.13.1 h1:CFNTr1oin5OJ0VCZ8EycL3wzF29Jz2g0xe55RFsf2a4=
210212
go.uber.org/fx v1.13.1/go.mod h1:bREWhavnedxpJeTq9pQT53BbvwhUv7TcpsOqcH4a+3w=
211213
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
212214
go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
213215
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
214-
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
215216
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
216217
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
217218
go.uber.org/multierr v1.4.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
@@ -229,8 +230,8 @@ go.uber.org/yarpc v1.60.0 h1:rNRrS8C0sDk5EScc9epftMdKQeToJ4Nwl2hqTZ1knTY=
229230
go.uber.org/yarpc v1.60.0/go.mod h1:fZ4SfvI/7sN9rF/H1W31g3yfbUmVv5fe+6xUSFPJC9E=
230231
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
231232
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
232-
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
233-
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
233+
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
234+
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
234235
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
235236
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
236237
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
@@ -285,8 +286,9 @@ golang.org/x/sys v0.0.0-20200212091648-12a6c2dcc1e4/go.mod h1:h1NjWce9XRLGQEsW7w
285286
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
286287
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
287288
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
288-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
289289
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
290+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad h1:ntjMns5wyP/fN65tdBD4g8J5w8n015+iIIs9rtjXkY0=
291+
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
290292
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
291293
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
292294
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -351,8 +353,8 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
351353
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
352354
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
353355
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
354-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
355-
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
356+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
357+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
356358
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
357359
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
358360
honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=

new_samples/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Cadence Samples
2+
These are some samples to demonstrate various capabilities of Cadence client and server. You can learn more about cadence at:
3+
* The website: https://cadenceworkflow.io
4+
* Cadence's server: https://github.com/uber/cadence
5+
* Cadence's Go client: https://github.com/uber-go/cadence-client
6+
7+
## Prerequisite
8+
0. Install Cadence CLI. See instruction [here](https://cadenceworkflow.io/docs/cli/).
9+
1. Run the Cadence server: https://github.com/uber/cadence/blob/master/README.md
10+
2. Register the `cadence-samples` domain: https://cadenceworkflow.io/docs/cli/#quick-start or https://github.com/uber/cadence/blob/master/tools/cli/README.md
11+
12+
13+
14+
## Steps to run samples
15+
Simply start the go binary by running
16+
```bash
17+
go run main.go
18+
```
19+
Then you may interact with the samples using Cadence CLI. You may start multiple workers by running
20+
this binary. Here are a list of example CLIs you may use to play with the samples.
21+
22+
### HelloWorld workflow
23+
This workflow takes an input message and greet you as response. Try the following CLI
24+
```bash
25+
cadence --env development \
26+
--domain cadence-samples \
27+
workflow start \
28+
--workflow_type cadence_samples.HelloWorldWorkflow \
29+
--tl cadence-samples-worker \
30+
--et 60 \
31+
--input '{"message":"Uber"}'
32+
```
33+
Try to inspect the log message for the output.

new_samples/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/uber-common/cadence-samples/new_samples/worker"
6+
"os"
7+
"os/signal"
8+
"syscall"
9+
)
10+
11+
func main() {
12+
worker.StartWorker()
13+
14+
done := make(chan os.Signal, 1)
15+
signal.Notify(done, syscall.SIGINT)
16+
fmt.Println("Cadence worker started, press ctrl+c to terminate...")
17+
<-done
18+
}

new_samples/worker/worker.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Package worker implements a Cadence worker with basic configurations.
2+
package worker
3+
4+
import (
5+
"github.com/uber-common/cadence-samples/new_samples/workflows"
6+
"github.com/uber-go/tally"
7+
apiv1 "github.com/uber/cadence-idl/go/proto/api/v1"
8+
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
9+
"go.uber.org/cadence/activity"
10+
"go.uber.org/cadence/compatibility"
11+
"go.uber.org/cadence/worker"
12+
"go.uber.org/cadence/workflow"
13+
"go.uber.org/yarpc"
14+
"go.uber.org/yarpc/transport/grpc"
15+
"go.uber.org/zap"
16+
"go.uber.org/zap/zapcore"
17+
)
18+
19+
const (
20+
HostPort = "127.0.0.1:7833"
21+
Domain = "cadence-samples"
22+
// TaskListName identifies set of client workflows, activities, and workers.
23+
// It could be your group or client or application name.
24+
TaskListName = "cadence-samples-worker"
25+
ClientName = "cadence-samples-worker"
26+
CadenceService = "cadence-frontend"
27+
)
28+
29+
// StartWorker creates and starts a basic Cadence worker.
30+
func StartWorker() {
31+
logger, cadenceClient := buildLogger(), buildCadenceClient()
32+
workerOptions := worker.Options{
33+
Logger: logger,
34+
MetricsScope: tally.NewTestScope(TaskListName, nil),
35+
}
36+
37+
w := worker.New(
38+
cadenceClient,
39+
Domain,
40+
TaskListName,
41+
workerOptions)
42+
// HelloWorld workflow registration
43+
w.RegisterWorkflowWithOptions(workflows.HelloWorldWorkflow, workflow.RegisterOptions{Name: "cadence_samples.HelloWorldWorkflow"})
44+
w.RegisterActivityWithOptions(workflows.HelloWorldActivity, activity.RegisterOptions{Name: "cadence_samples.HelloWorldActivity"})
45+
46+
err := w.Start()
47+
if err != nil {
48+
panic("Failed to start worker: " + err.Error())
49+
}
50+
logger.Info("Started Worker.", zap.String("worker", TaskListName))
51+
52+
}
53+
54+
func buildCadenceClient() workflowserviceclient.Interface {
55+
dispatcher := yarpc.NewDispatcher(yarpc.Config{
56+
Name: ClientName,
57+
Outbounds: yarpc.Outbounds{
58+
CadenceService: {Unary: grpc.NewTransport().NewSingleOutbound(HostPort)},
59+
},
60+
})
61+
if err := dispatcher.Start(); err != nil {
62+
panic("Failed to start dispatcher: " + err.Error())
63+
}
64+
65+
clientConfig := dispatcher.ClientConfig(CadenceService)
66+
67+
return compatibility.NewThrift2ProtoAdapter(
68+
apiv1.NewDomainAPIYARPCClient(clientConfig),
69+
apiv1.NewWorkflowAPIYARPCClient(clientConfig),
70+
apiv1.NewWorkerAPIYARPCClient(clientConfig),
71+
apiv1.NewVisibilityAPIYARPCClient(clientConfig),
72+
)
73+
}
74+
75+
func buildLogger() *zap.Logger {
76+
config := zap.NewDevelopmentConfig()
77+
config.Level.SetLevel(zapcore.InfoLevel)
78+
79+
var err error
80+
logger, err := config.Build()
81+
if err != nil {
82+
panic("Failed to setup logger: " + err.Error())
83+
}
84+
85+
return logger
86+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package workflows
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"go.uber.org/cadence/activity"
7+
"go.uber.org/cadence/workflow"
8+
"go.uber.org/zap"
9+
"time"
10+
)
11+
12+
type sampleInput struct {
13+
Message string `json:"message"`
14+
}
15+
16+
// HelloWorldWorkflow greets the caller.
17+
func HelloWorldWorkflow(ctx workflow.Context, input sampleInput) (string, error) {
18+
ao := workflow.ActivityOptions{
19+
ScheduleToStartTimeout: time.Minute,
20+
StartToCloseTimeout: time.Minute,
21+
}
22+
ctx = workflow.WithActivityOptions(ctx, ao)
23+
24+
logger := workflow.GetLogger(ctx)
25+
logger.Info("HelloWorldWorkflow started")
26+
27+
var greetingMsg string
28+
err := workflow.ExecuteActivity(ctx, HelloWorldActivity, input).Get(ctx, &greetingMsg)
29+
if err != nil {
30+
logger.Error("HelloWorldActivity failed", zap.Error(err))
31+
return "", err
32+
}
33+
34+
logger.Info("Workflow result", zap.String("greeting", greetingMsg))
35+
return greetingMsg, nil
36+
}
37+
38+
// HelloWorldActivity constructs the greeting message from input.
39+
func HelloWorldActivity(ctx context.Context, input sampleInput) (string, error) {
40+
logger := activity.GetLogger(ctx)
41+
logger.Info("HelloWorldActivity started")
42+
return fmt.Sprintf("Hello, %s!", input.Message), nil
43+
}

0 commit comments

Comments
 (0)