Skip to content

Commit

Permalink
Add eager workflow start sample (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
antlai-temporal authored Mar 7, 2024
1 parent f25b9cb commit 3cc0870
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
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
19 changes: 19 additions & 0 deletions eager-workflow-start/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Eager Workflow Start Sample

Eager Workflow Start (EWS) is an experimental latency optimization with the goal of reducing the time it takes to start a workflow.

When the starter and worker are collocated and aware of each other, they can interact while bypassing the server, saving a few time-intensive operations.

Here we modify the `helloworld` sample, ensuring that starter and worker run in the same process, and share the client. Also, the request eager mode flag is set in the start workflow options.

### Steps to run this sample:

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,
}

// 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)
}

0 comments on commit 3cc0870

Please sign in to comment.