-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add eager workflow start sample (#334)
- Loading branch information
1 parent
f25b9cb
commit 3cc0870
Showing
4 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,4 @@ vendor/ | |
# Executables produced by temporal-go-samples repo | ||
bin/ | ||
.fossa.yml | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |