Skip to content

Commit fe9bd5c

Browse files
Add workflow code
Signed-off-by: Ender Demirkaya <[email protected]>
1 parent 53243e1 commit fe9bd5c

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

new_samples/signal/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!-- THIS IS A GENERATED FILE -->
2+
<!-- PLEASE DO NOT EDIT -->
3+
4+
# Signal Workflow Sample
5+
6+
## Prerequisites
7+
8+
0. Install Cadence CLI. See instruction [here](https://cadenceworkflow.io/docs/cli/).
9+
1. Run the Cadence server:
10+
1. Clone the [Cadence](https://github.com/cadence-workflow/cadence) repository if you haven't done already: `git clone https://github.com/cadence-workflow/cadence.git`
11+
2. Run `docker compose -f docker/docker-compose.yml up` to start Cadence server
12+
3. See more details at https://github.com/uber/cadence/blob/master/README.md
13+
2. Once everything is up and running in Docker, open [localhost:8088](localhost:8088) to view Cadence UI.
14+
3. Register the `cadence-samples` domain:
15+
16+
```bash
17+
cadence --env development --domain cadence-samples domain register
18+
```
19+
20+
Refresh the [domains page](http://localhost:8088/domains) from step 2 to verify `cadence-samples` is registered.
21+
22+
## Steps to run sample
23+
24+
Inside the folder this sample is defined, run the following command:
25+
26+
```bash
27+
go run .
28+
```
29+
30+
This will call the main function in main.go which starts the worker, which will be execute the sample workflow code
31+
32+
## Simple Signal Workflow
33+
34+
This workflow takes an input message and greet you as response. Try the following CLI
35+
36+
```bash
37+
cadence --env development \
38+
--domain cadence-samples \
39+
workflow start \
40+
--tl cadence-samples-worker \
41+
--et 60 \
42+
--workflow_type cadence_samples.SimpleSignalWorkflow
43+
```
44+
45+
Verify that your workflow started. Your can find your worklow by looking at the "Workflow type" column.
46+
47+
If this is your first sample, please refer to [HelloWorkflow sample](https://github.com/cadence-workflow/cadence-samples/tree/master/new_samples/hello_world) about how to view your workflows.
48+
49+
50+
### Signal your workflow
51+
52+
This workflow will need a signal to complete successfully. Below is how you can send a signal. In this example, we are sending a `bool` value `true` (JSON formatted) via the signal called `complete`
53+
54+
```bash
55+
cadence --env development \
56+
--domain cadence-samples \
57+
workflow signal \
58+
--wid <workflow_id> \
59+
--name complete \
60+
--input 'true'
61+
```
62+
63+
## References
64+
65+
* The website: https://cadenceworkflow.io
66+
* Cadence's server: https://github.com/uber/cadence
67+
* Cadence's Go client: https://github.com/uber-go/cadence-client
68+

new_samples/signal/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// THIS IS A GENERATED FILE
2+
// PLEASE DO NOT EDIT
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
)
12+
13+
func main() {
14+
StartWorker()
15+
16+
done := make(chan os.Signal, 1)
17+
signal.Notify(done, syscall.SIGINT)
18+
fmt.Println("Cadence worker started, press ctrl+c to terminate...")
19+
<-done
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"go.uber.org/cadence/workflow"
6+
"go.uber.org/cadence/activity"
7+
"strconv"
8+
"time"
9+
"go.uber.org/zap"
10+
)
11+
12+
const (
13+
CompleteSignalChan = "complete"
14+
)
15+
16+
func SimpleSignalWorkflow(ctx workflow.Context) error {
17+
ao := workflow.ActivityOptions{
18+
ScheduleToStartTimeout: time.Minute * 60,
19+
StartToCloseTimeout: time.Minute * 60,
20+
}
21+
ctx = workflow.WithActivityOptions(ctx, ao)
22+
logger := workflow.GetLogger(ctx)
23+
logger.Info("SimpleSignalWorkflow started")
24+
25+
var complete bool
26+
completeChan := workflow.GetSignalChannel(ctx, CompleteSignalChan)
27+
for {
28+
s := workflow.NewSelector(ctx)
29+
s.AddReceive(completeChan, func(ch workflow.Channel, ok bool) {
30+
if ok {
31+
ch.Receive(ctx, &complete)
32+
}
33+
logger.Info("Signal input: " + strconv.FormatBool(complete))
34+
})
35+
s.Select(ctx)
36+
37+
var result string
38+
err := workflow.ExecuteActivity(ctx, SimpleSignalActivity, complete).Get(ctx, &result)
39+
if err != nil {
40+
return err
41+
}
42+
logger.Info("Activity result: " + result)
43+
if complete {
44+
return nil
45+
}
46+
}
47+
}
48+
49+
func SimpleSignalActivity(ctx context.Context, complete bool) (string, error) {
50+
logger := activity.GetLogger(ctx)
51+
logger.Info("SimpleSignalActivity started, a new signal has been received", zap.Bool("complete", complete))
52+
if complete {
53+
return "Workflow will complete now", nil
54+
}
55+
return "Workflow will continue to run", nil
56+
}

new_samples/signal/worker.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// THIS IS A GENERATED FILE
2+
// PLEASE DO NOT EDIT
3+
4+
// Package worker implements a Cadence worker with basic configurations.
5+
package main
6+
7+
import (
8+
"github.com/uber-go/tally"
9+
apiv1 "github.com/uber/cadence-idl/go/proto/api/v1"
10+
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
11+
"go.uber.org/cadence/activity"
12+
"go.uber.org/cadence/compatibility"
13+
"go.uber.org/cadence/worker"
14+
"go.uber.org/cadence/workflow"
15+
"go.uber.org/yarpc"
16+
"go.uber.org/yarpc/peer"
17+
yarpchostport "go.uber.org/yarpc/peer/hostport"
18+
"go.uber.org/yarpc/transport/grpc"
19+
"go.uber.org/zap"
20+
"go.uber.org/zap/zapcore"
21+
)
22+
23+
const (
24+
HostPort = "127.0.0.1:7833"
25+
Domain = "cadence-samples"
26+
// TaskListName identifies set of client workflows, activities, and workers.
27+
// It could be your group or client or application name.
28+
TaskListName = "cadence-samples-worker"
29+
ClientName = "cadence-samples-worker"
30+
CadenceService = "cadence-frontend"
31+
)
32+
33+
// StartWorker creates and starts a basic Cadence worker.
34+
func StartWorker() {
35+
logger, cadenceClient := BuildLogger(), BuildCadenceClient()
36+
workerOptions := worker.Options{
37+
Logger: logger,
38+
MetricsScope: tally.NewTestScope(TaskListName, nil),
39+
}
40+
41+
w := worker.New(
42+
cadenceClient,
43+
Domain,
44+
TaskListName,
45+
workerOptions)
46+
// HelloWorld workflow registration
47+
w.RegisterWorkflowWithOptions(SimpleSignalWorkflow, workflow.RegisterOptions{Name: "cadence_samples.SimpleSignalWorkflow"})
48+
w.RegisterActivityWithOptions(SimpleSignalActivity, activity.RegisterOptions{Name: "cadence_samples.SimpleSignalActivity"})
49+
50+
err := w.Start()
51+
if err != nil {
52+
panic("Failed to start worker: " + err.Error())
53+
}
54+
logger.Info("Started Worker.", zap.String("worker", TaskListName))
55+
56+
}
57+
58+
func BuildCadenceClient(dialOptions ...grpc.DialOption) workflowserviceclient.Interface {
59+
grpcTransport := grpc.NewTransport()
60+
// Create a single peer chooser that identifies the host/port and configures
61+
// a gRPC dialer with TLS credentials
62+
myChooser := peer.NewSingle(
63+
yarpchostport.Identify(HostPort),
64+
grpcTransport.NewDialer(dialOptions...),
65+
)
66+
outbound := grpcTransport.NewOutbound(myChooser)
67+
68+
dispatcher := yarpc.NewDispatcher(yarpc.Config{
69+
Name: ClientName,
70+
Outbounds: yarpc.Outbounds{
71+
CadenceService: {Unary: outbound},
72+
},
73+
})
74+
if err := dispatcher.Start(); err != nil {
75+
panic("Failed to start dispatcher: " + err.Error())
76+
}
77+
78+
clientConfig := dispatcher.ClientConfig(CadenceService)
79+
80+
// Create a compatibility adapter that wraps proto-based YARPC clients
81+
// to provide a unified interface for domain, workflow, worker, and visibility APIs
82+
return compatibility.NewThrift2ProtoAdapter(
83+
apiv1.NewDomainAPIYARPCClient(clientConfig),
84+
apiv1.NewWorkflowAPIYARPCClient(clientConfig),
85+
apiv1.NewWorkerAPIYARPCClient(clientConfig),
86+
apiv1.NewVisibilityAPIYARPCClient(clientConfig),
87+
)
88+
}
89+
90+
func BuildLogger() *zap.Logger {
91+
config := zap.NewDevelopmentConfig()
92+
config.Level.SetLevel(zapcore.InfoLevel)
93+
94+
var err error
95+
logger, err := config.Build()
96+
if err != nil {
97+
panic("Failed to setup logger: " + err.Error())
98+
}
99+
100+
return logger
101+
}

0 commit comments

Comments
 (0)