diff --git a/new_samples/hello_world/README.md b/new_samples/hello_world/README.md new file mode 100644 index 0000000..b466931 --- /dev/null +++ b/new_samples/hello_world/README.md @@ -0,0 +1,102 @@ + + + +# Hello World Sample + +## Prerequisites + +0. Install Cadence CLI. See instruction [here](https://cadenceworkflow.io/docs/cli/). +1. Run the Cadence server: + 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` + 2. Run `docker compose -f docker/docker-compose.yml up` to start Cadence server + 3. See more details at https://github.com/uber/cadence/blob/master/README.md +2. Once everything is up and running in Docker, open [localhost:8088](localhost:8088) to view Cadence UI. +3. Register the `cadence-samples` domain: + +```bash +cadence --env development --domain cadence-samples domain register +``` + +Refresh the [domains page](http://localhost:8088/domains) from step 2 to verify `cadence-samples` is registered. + +## Steps to run sample + +Inside the folder this sample is defined, run the following command: + +```bash +go run . +``` + +This will call the main function in main.go which starts the worker, which will be execute the sample workflow code + +### Start your workflow + +This workflow takes an input message and greet you as response. Try the following CLI + +```bash +cadence --env development \ + --domain cadence-samples \ + workflow start \ + --workflow_type cadence_samples.HelloWorldWorkflow \ + --tl cadence-samples-worker \ + --et 60 \ + --input '{"message":"Cadence"}' +``` + +Here are the details to this command: + +* `--domain` option describes under which domain to run this workflow +* `--env development` calls the "local" cadence server +* `--workflow_type` option describes which workflow to execute +* `-tl` (or `--tasklist`) tells cadence-server which tasklist to schedule tasks with. This is the same tasklist the worker polls tasks from. See worker.go +* `--et` (or `--execution_timeout`) tells cadence server how long to wait until timing out the workflow +* `--input` is the input to your workflow + +To see more options run `cadence --help` + +### View your workflow + +#### Cadence UI (cadence-web) + +Click on `cadence-samples` domain in cadence-web to view your workflow. + +* In Summary tab, you will see the input and output to your workflow +* Click on History tab to see individual steps. + +#### CLI + +List workflows using the following command: + +```bash + cadence --env development --domain cadence-samples --workflow list +``` + +You can view an individual workflow by using the following command: + +```bash +cadence --env development \ + --domain cadence-samples \ + --workflow describe \ + --wid +``` + +* `workflow` is the noun to run commands within workflow scope +* `describe` is the verb to return the summary of the workflow +* `--wid` (or `--workflow_id`) is the option to pass the workflow id. If there are multiple "run"s, it will return the latest one. +* (optional) `--rid` (or `--run_id`) is the option to pass the run id to describe a specific run, instead of the latest. + +To view the entire history of the workflow, use the following command: + +```bash +cadence --env development \ + --domain cadence-samples \ + --workflow show \ + --wid +``` + +## References + +* The website: https://cadenceworkflow.io +* Cadence's server: https://github.com/uber/cadence +* Cadence's Go client: https://github.com/uber-go/cadence-client + diff --git a/new_samples/hello_world/generator/README.md b/new_samples/hello_world/generator/README.md new file mode 100644 index 0000000..1da3502 --- /dev/null +++ b/new_samples/hello_world/generator/README.md @@ -0,0 +1,23 @@ + + + +# Sample Generator + +This folder is NOT part of the actual sample. It exists only for contributors who work on this sample. Please disregard it if you are trying to learn about Cadence. + +To create a better learning experience for Cadence users, each sample folder is designed to be self contained. Users can view every part of writing and running workflows, including: + +* Cadence client initialization +* Worker with workflow and activity registrations +* Workflow starter +* and the workflow code itself + +Some samples may have more or fewer parts depending on what they need to demonstrate. + +In most cases, the workflow code (e.g. `workflow.go`) is the part that users care about. The rest is boilerplate needed to run that workflow. For each sample folder, the workflow code should be written by hand. The boilerplate can be generated. Keeping all parts inside one folder gives early learners more value because they can see everything together rather than jumping across directories. + +## Contributing + +* When creating a new sample, follow the steps mentioned in the README file in the main samples folder. +* To update the sample workflow code, edit the workflow file directly. +* To update the worker, client, or other boilerplate logic, edit the generator file. If your change applies to all samples, update the common generator file inside the `template` folder. Edit the generator file in this folder only when the change should affect this sample alone. diff --git a/new_samples/hello_world/generator/README_specific.md b/new_samples/hello_world/generator/README_specific.md new file mode 100644 index 0000000..4833782 --- /dev/null +++ b/new_samples/hello_world/generator/README_specific.md @@ -0,0 +1,64 @@ +### Start your workflow + +This workflow takes an input message and greet you as response. Try the following CLI + +```bash +cadence --env development \ + --domain cadence-samples \ + workflow start \ + --workflow_type cadence_samples.HelloWorldWorkflow \ + --tl cadence-samples-worker \ + --et 60 \ + --input '{"message":"Cadence"}' +``` + +Here are the details to this command: + +* `--domain` option describes under which domain to run this workflow +* `--env development` calls the "local" cadence server +* `--workflow_type` option describes which workflow to execute +* `-tl` (or `--tasklist`) tells cadence-server which tasklist to schedule tasks with. This is the same tasklist the worker polls tasks from. See worker.go +* `--et` (or `--execution_timeout`) tells cadence server how long to wait until timing out the workflow +* `--input` is the input to your workflow + +To see more options run `cadence --help` + +### View your workflow + +#### Cadence UI (cadence-web) + +Click on `cadence-samples` domain in cadence-web to view your workflow. + +* In Summary tab, you will see the input and output to your workflow +* Click on History tab to see individual steps. + +#### CLI + +List workflows using the following command: + +```bash + cadence --env development --domain cadence-samples --workflow list +``` + +You can view an individual workflow by using the following command: + +```bash +cadence --env development \ + --domain cadence-samples \ + --workflow describe \ + --wid +``` + +* `workflow` is the noun to run commands within workflow scope +* `describe` is the verb to return the summary of the workflow +* `--wid` (or `--workflow_id`) is the option to pass the workflow id. If there are multiple "run"s, it will return the latest one. +* (optional) `--rid` (or `--run_id`) is the option to pass the run id to describe a specific run, instead of the latest. + +To view the entire history of the workflow, use the following command: + +```bash +cadence --env development \ + --domain cadence-samples \ + --workflow show \ + --wid +``` diff --git a/new_samples/hello_world/generator/generate.go b/new_samples/hello_world/generator/generate.go new file mode 100644 index 0000000..0667645 --- /dev/null +++ b/new_samples/hello_world/generator/generate.go @@ -0,0 +1,16 @@ +package main + +import "github.com/uber-common/cadence-samples/new_samples/template" + +func main() { + // Define the data for HelloWorld + data := template.TemplateData{ + SampleName: "Hello World", + Workflows: []string{"HelloWorldWorkflow"}, + Activities: []string{"HelloWorldActivity"}, + } + + template.GenerateAll(data) +} + +// Implement custom generator below \ No newline at end of file diff --git a/new_samples/hello_world/main.go b/new_samples/hello_world/main.go new file mode 100644 index 0000000..5893999 --- /dev/null +++ b/new_samples/hello_world/main.go @@ -0,0 +1,20 @@ +// THIS IS A GENERATED FILE +// PLEASE DO NOT EDIT + +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" +) + +func main() { + StartWorker() + + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGINT) + fmt.Println("Cadence worker started, press ctrl+c to terminate...") + <-done +} diff --git a/new_samples/hello_world/worker.go b/new_samples/hello_world/worker.go new file mode 100644 index 0000000..c81932c --- /dev/null +++ b/new_samples/hello_world/worker.go @@ -0,0 +1,101 @@ +// THIS IS A GENERATED FILE +// PLEASE DO NOT EDIT + +// Package worker implements a Cadence worker with basic configurations. +package main + +import ( + "github.com/uber-go/tally" + apiv1 "github.com/uber/cadence-idl/go/proto/api/v1" + "go.uber.org/cadence/.gen/go/cadence/workflowserviceclient" + "go.uber.org/cadence/activity" + "go.uber.org/cadence/compatibility" + "go.uber.org/cadence/worker" + "go.uber.org/cadence/workflow" + "go.uber.org/yarpc" + "go.uber.org/yarpc/peer" + yarpchostport "go.uber.org/yarpc/peer/hostport" + "go.uber.org/yarpc/transport/grpc" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +const ( + HostPort = "127.0.0.1:7833" + Domain = "cadence-samples" + // TaskListName identifies set of client workflows, activities, and workers. + // It could be your group or client or application name. + TaskListName = "cadence-samples-worker" + ClientName = "cadence-samples-worker" + CadenceService = "cadence-frontend" +) + +// StartWorker creates and starts a basic Cadence worker. +func StartWorker() { + logger, cadenceClient := BuildLogger(), BuildCadenceClient() + workerOptions := worker.Options{ + Logger: logger, + MetricsScope: tally.NewTestScope(TaskListName, nil), + } + + w := worker.New( + cadenceClient, + Domain, + TaskListName, + workerOptions) + // HelloWorld workflow registration + w.RegisterWorkflowWithOptions(HelloWorldWorkflow, workflow.RegisterOptions{Name: "cadence_samples.HelloWorldWorkflow"}) + w.RegisterActivityWithOptions(HelloWorldActivity, activity.RegisterOptions{Name: "cadence_samples.HelloWorldActivity"}) + + err := w.Start() + if err != nil { + panic("Failed to start worker: " + err.Error()) + } + logger.Info("Started Worker.", zap.String("worker", TaskListName)) + +} + +func BuildCadenceClient(dialOptions ...grpc.DialOption) workflowserviceclient.Interface { + grpcTransport := grpc.NewTransport() + // Create a single peer chooser that identifies the host/port and configures + // a gRPC dialer with TLS credentials + myChooser := peer.NewSingle( + yarpchostport.Identify(HostPort), + grpcTransport.NewDialer(dialOptions...), + ) + outbound := grpcTransport.NewOutbound(myChooser) + + dispatcher := yarpc.NewDispatcher(yarpc.Config{ + Name: ClientName, + Outbounds: yarpc.Outbounds{ + CadenceService: {Unary: outbound}, + }, + }) + if err := dispatcher.Start(); err != nil { + panic("Failed to start dispatcher: " + err.Error()) + } + + clientConfig := dispatcher.ClientConfig(CadenceService) + + // Create a compatibility adapter that wraps proto-based YARPC clients + // to provide a unified interface for domain, workflow, worker, and visibility APIs + return compatibility.NewThrift2ProtoAdapter( + apiv1.NewDomainAPIYARPCClient(clientConfig), + apiv1.NewWorkflowAPIYARPCClient(clientConfig), + apiv1.NewWorkerAPIYARPCClient(clientConfig), + apiv1.NewVisibilityAPIYARPCClient(clientConfig), + ) +} + +func BuildLogger() *zap.Logger { + config := zap.NewDevelopmentConfig() + config.Level.SetLevel(zapcore.InfoLevel) + + var err error + logger, err := config.Build() + if err != nil { + panic("Failed to setup logger: " + err.Error()) + } + + return logger +} diff --git a/new_samples/hello_world/workflow.go b/new_samples/hello_world/workflow.go new file mode 100644 index 0000000..3f90d78 --- /dev/null +++ b/new_samples/hello_world/workflow.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + "fmt" + "go.uber.org/cadence/activity" + "go.uber.org/cadence/workflow" + "go.uber.org/zap" + "time" +) + +type sampleInput struct { + Message string `json:"message"` +} + +// This is the workflow function +// Given an input, HelloWorldWorkflow returns "Hello !" +func HelloWorldWorkflow(ctx workflow.Context, input sampleInput) (string, error) { + ao := workflow.ActivityOptions{ + ScheduleToStartTimeout: time.Minute, + StartToCloseTimeout: time.Minute, + } + ctx = workflow.WithActivityOptions(ctx, ao) + + logger := workflow.GetLogger(ctx) + logger.Info("HelloWorldWorkflow started") + + var greetingMsg string + err := workflow.ExecuteActivity(ctx, HelloWorldActivity, input).Get(ctx, &greetingMsg) + if err != nil { + logger.Error("HelloWorldActivity failed", zap.Error(err)) + return "", err + } + + logger.Info("Workflow result", zap.String("greeting", greetingMsg)) + return greetingMsg, nil +} + +// This is the activity function +// Given an input, HelloWorldActivity returns "Hello !" +func HelloWorldActivity(ctx context.Context, input sampleInput) (string, error) { + logger := activity.GetLogger(ctx) + logger.Info("HelloWorldActivity started") + return fmt.Sprintf("Hello, %s!", input.Message), nil +} diff --git a/new_samples/template/README.tmpl b/new_samples/template/README.tmpl new file mode 100644 index 0000000..a291806 --- /dev/null +++ b/new_samples/template/README.tmpl @@ -0,0 +1,31 @@ + + + +# {{.SampleName}} Sample + +## Prerequisites + +0. Install Cadence CLI. See instruction [here](https://cadenceworkflow.io/docs/cli/). +1. Run the Cadence server: + 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` + 2. Run `docker compose -f docker/docker-compose.yml up` to start Cadence server + 3. See more details at https://github.com/uber/cadence/blob/master/README.md +2. Once everything is up and running in Docker, open [localhost:8088](localhost:8088) to view Cadence UI. +3. Register the `cadence-samples` domain: + +```bash +cadence --env development --domain cadence-samples domain register +``` + +Refresh the [domains page](http://localhost:8088/domains) from step 2 to verify `cadence-samples` is registered. + +## Steps to run sample + +Inside the folder this sample is defined, run the following command: + +```bash +go run . +``` + +This will call the main function in main.go which starts the worker, which will be execute the sample workflow code + diff --git a/new_samples/template/README_generator.tmpl b/new_samples/template/README_generator.tmpl new file mode 100644 index 0000000..1da3502 --- /dev/null +++ b/new_samples/template/README_generator.tmpl @@ -0,0 +1,23 @@ + + + +# Sample Generator + +This folder is NOT part of the actual sample. It exists only for contributors who work on this sample. Please disregard it if you are trying to learn about Cadence. + +To create a better learning experience for Cadence users, each sample folder is designed to be self contained. Users can view every part of writing and running workflows, including: + +* Cadence client initialization +* Worker with workflow and activity registrations +* Workflow starter +* and the workflow code itself + +Some samples may have more or fewer parts depending on what they need to demonstrate. + +In most cases, the workflow code (e.g. `workflow.go`) is the part that users care about. The rest is boilerplate needed to run that workflow. For each sample folder, the workflow code should be written by hand. The boilerplate can be generated. Keeping all parts inside one folder gives early learners more value because they can see everything together rather than jumping across directories. + +## Contributing + +* When creating a new sample, follow the steps mentioned in the README file in the main samples folder. +* To update the sample workflow code, edit the workflow file directly. +* To update the worker, client, or other boilerplate logic, edit the generator file. If your change applies to all samples, update the common generator file inside the `template` folder. Edit the generator file in this folder only when the change should affect this sample alone. diff --git a/new_samples/template/README_references.tmpl b/new_samples/template/README_references.tmpl new file mode 100644 index 0000000..8044aff --- /dev/null +++ b/new_samples/template/README_references.tmpl @@ -0,0 +1,7 @@ + +## References + +* The website: https://cadenceworkflow.io +* Cadence's server: https://github.com/uber/cadence +* Cadence's Go client: https://github.com/uber-go/cadence-client + diff --git a/new_samples/template/generator.go b/new_samples/template/generator.go new file mode 100644 index 0000000..ce8a7f5 --- /dev/null +++ b/new_samples/template/generator.go @@ -0,0 +1,79 @@ +package template + +import ( + "os" + "text/template" +) + +type TemplateData struct { + SampleName string + Workflows []string + Activities []string +} + +func GenerateAll(data TemplateData) { + GenerateWorker(data) + GenerateMain(data) + GenerateSampleReadMe(data) + GenerateGeneratorReadMe(data) +} + +func GenerateWorker(data TemplateData) { + GenerateFile("../../template/worker.tmpl", "../worker.go", data) + println("Generated worker.go") +} + +func GenerateMain(data TemplateData) { + GenerateFile("../../template/main.tmpl", "../main.go", data) + println("Generated main.go") +} + +func GenerateSampleReadMe(data TemplateData) { + inputs := []string{"../../template/README.tmpl", "README_specific.md", "../../template/README_references.tmpl"} + GenerateREADME(inputs, "../README.md", data) +} + +func GenerateGeneratorReadMe(data TemplateData) { + GenerateFile("../../template/README_generator.tmpl", "README.md", data) + println("Generated README.md") +} + +func GenerateFile(templatePath, outputPath string, data TemplateData) { + tmpl, err := template.ParseFiles(templatePath) + if err != nil { + panic("Failed to parse template " + templatePath + ": " + err.Error()) + } + + f, err := os.Create(outputPath) + if err != nil { + panic("Failed to create output file " + outputPath + ": " + err.Error()) + } + defer f.Close() + + err = tmpl.Execute(f, data) + if err != nil { + panic("Failed to execute template: " + err.Error()) + } +} + +func GenerateREADME(inputs []string, outputPath string, data TemplateData) { + // Create output file + f, err := os.Create(outputPath) + if err != nil { + panic("Failed to create README file: " + err.Error()) + } + defer f.Close() + + for _, input := range inputs { + tmpl, err := template.ParseFiles(input) + if err != nil { + panic("Failed to parse README template: " + err.Error()) + } + + err = tmpl.Execute(f, data) + if err != nil { + panic(input + ": Failed to append README content: " + err.Error()) + } + } + +} diff --git a/new_samples/template/main.tmpl b/new_samples/template/main.tmpl new file mode 100644 index 0000000..5893999 --- /dev/null +++ b/new_samples/template/main.tmpl @@ -0,0 +1,20 @@ +// THIS IS A GENERATED FILE +// PLEASE DO NOT EDIT + +package main + +import ( + "fmt" + "os" + "os/signal" + "syscall" +) + +func main() { + StartWorker() + + done := make(chan os.Signal, 1) + signal.Notify(done, syscall.SIGINT) + fmt.Println("Cadence worker started, press ctrl+c to terminate...") + <-done +} diff --git a/new_samples/template/worker.tmpl b/new_samples/template/worker.tmpl new file mode 100644 index 0000000..aa9ab90 --- /dev/null +++ b/new_samples/template/worker.tmpl @@ -0,0 +1,105 @@ +// THIS IS A GENERATED FILE +// PLEASE DO NOT EDIT + +// Package worker implements a Cadence worker with basic configurations. +package main + +import ( + "github.com/uber-go/tally" + apiv1 "github.com/uber/cadence-idl/go/proto/api/v1" + "go.uber.org/cadence/.gen/go/cadence/workflowserviceclient" + "go.uber.org/cadence/activity" + "go.uber.org/cadence/compatibility" + "go.uber.org/cadence/worker" + "go.uber.org/cadence/workflow" + "go.uber.org/yarpc" + "go.uber.org/yarpc/peer" + yarpchostport "go.uber.org/yarpc/peer/hostport" + "go.uber.org/yarpc/transport/grpc" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +const ( + HostPort = "127.0.0.1:7833" + Domain = "cadence-samples" + // TaskListName identifies set of client workflows, activities, and workers. + // It could be your group or client or application name. + TaskListName = "cadence-samples-worker" + ClientName = "cadence-samples-worker" + CadenceService = "cadence-frontend" +) + +// StartWorker creates and starts a basic Cadence worker. +func StartWorker() { + logger, cadenceClient := BuildLogger(), BuildCadenceClient() + workerOptions := worker.Options{ + Logger: logger, + MetricsScope: tally.NewTestScope(TaskListName, nil), + } + + w := worker.New( + cadenceClient, + Domain, + TaskListName, + workerOptions) + // HelloWorld workflow registration + {{- range .Workflows}} + w.RegisterWorkflowWithOptions({{.}}, workflow.RegisterOptions{Name: "cadence_samples.{{.}}"}) + {{- end}} + {{- range .Activities}} + w.RegisterActivityWithOptions({{.}}, activity.RegisterOptions{Name: "cadence_samples.{{.}}"}) + {{- end}} + + err := w.Start() + if err != nil { + panic("Failed to start worker: " + err.Error()) + } + logger.Info("Started Worker.", zap.String("worker", TaskListName)) + +} + +func BuildCadenceClient(dialOptions ...grpc.DialOption) workflowserviceclient.Interface { + grpcTransport := grpc.NewTransport() + // Create a single peer chooser that identifies the host/port and configures + // a gRPC dialer with TLS credentials + myChooser := peer.NewSingle( + yarpchostport.Identify(HostPort), + grpcTransport.NewDialer(dialOptions...), + ) + outbound := grpcTransport.NewOutbound(myChooser) + + dispatcher := yarpc.NewDispatcher(yarpc.Config{ + Name: ClientName, + Outbounds: yarpc.Outbounds{ + CadenceService: {Unary: outbound}, + }, + }) + if err := dispatcher.Start(); err != nil { + panic("Failed to start dispatcher: " + err.Error()) + } + + clientConfig := dispatcher.ClientConfig(CadenceService) + + // Create a compatibility adapter that wraps proto-based YARPC clients + // to provide a unified interface for domain, workflow, worker, and visibility APIs + return compatibility.NewThrift2ProtoAdapter( + apiv1.NewDomainAPIYARPCClient(clientConfig), + apiv1.NewWorkflowAPIYARPCClient(clientConfig), + apiv1.NewWorkerAPIYARPCClient(clientConfig), + apiv1.NewVisibilityAPIYARPCClient(clientConfig), + ) +} + +func BuildLogger() *zap.Logger { + config := zap.NewDevelopmentConfig() + config.Level.SetLevel(zapcore.InfoLevel) + + var err error + logger, err := config.Build() + if err != nil { + panic("Failed to setup logger: " + err.Error()) + } + + return logger +}