Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eager workflow start sample #334

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ vendor/
# Executables produced by temporal-go-samples repo
bin/
.fossa.yml
*~
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ Each sample demonstrates one feature of the SDK, together with tests.
- [**Update**](./update): Demonstrates how to create a workflow that reacts
to workflow update requests.

- [**Eager Workflow Start**](./eager-workflow-start): Demonstrates how to start a workflow in eager mode, an experimental latency optimization.

### Dynamic Workflow logic examples

These samples demonstrate some common control flow patterns using Temporal's Go SDK API.
Expand Down
10 changes: 10 additions & 0 deletions eager-workflow-start/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Steps to run this sample:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a short description here about eager workflow start and how the sample works like some of our other samples?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

1) Run a [Temporal service](https://github.com/temporalio/samples-go/tree/main/#how-to-use) with eager workflow start enabled by a dynamic config value.
```
temporal server start-dev --dynamic-config-value system.enableEagerWorkflowStart=true
```
2) Run the following command to start the combined worker and example
```
go run eager-workflow-start/main.go
```

59 changes: 59 additions & 0 deletions eager-workflow-start/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"context"
"log"

"github.com/pborman/uuid"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"

"github.com/temporalio/samples-go/helloworld"
)

const taskQueueName = "eager_wf_start"

func main() {
// 1. Create the shared client.
c, err := client.Dial(client.Options{})
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

// 2. Start the worker in a non-blocking manner before the workflow.
workerOptions := worker.Options{
OnFatalError: func(err error) { log.Fatalln("Worker error", err) },
}
w := worker.New(c, taskQueueName, workerOptions)
w.RegisterWorkflow(helloworld.Workflow)
w.RegisterActivity(helloworld.Activity)
err = w.Start()
if err != nil {
log.Fatalln("Unable to start worker", err)
}
defer w.Stop()

workflowOptions := client.StartWorkflowOptions{
ID: "eager_wf" + uuid.New(),
TaskQueue: taskQueueName,

// 3. Set this flag to true to enable EWS.
EnableEagerStart: true,
Comment on lines +41 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrmm, wonder if it's a bit much to have an entire sample just to set true on a single workflow option (I wouldn't expect separate samples for every other start option). But I don't mind I guess if it makes sense generally.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth making a sample for since it also shows you have to reuse the client

}

// 4. Reuse the client connection.
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions,
helloworld.Workflow, "Temporal")
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}

// 5. Wait for workflow completion.
var result string
err = we.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Unable to get workflow result", err)
}
log.Println("Workflow result:", result)
}
Loading