From c529a081b15fc56e70f3259effbf0156c9a1bdd4 Mon Sep 17 00:00:00 2001 From: Antonio Lain Date: Wed, 6 Mar 2024 13:09:47 -0800 Subject: [PATCH 1/2] Add eager workflow start sample --- .gitignore | 1 + README.md | 2 ++ eager-workflow-start/README.md | 10 ++++++ eager-workflow-start/main.go | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 eager-workflow-start/README.md create mode 100644 eager-workflow-start/main.go diff --git a/.gitignore b/.gitignore index fb369eea..ed980c9c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ vendor/ # Executables produced by temporal-go-samples repo bin/ .fossa.yml +*~ diff --git a/README.md b/README.md index 056bce9f..76a5ad96 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/eager-workflow-start/README.md b/eager-workflow-start/README.md new file mode 100644 index 00000000..667671b2 --- /dev/null +++ b/eager-workflow-start/README.md @@ -0,0 +1,10 @@ +### 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 +``` + diff --git a/eager-workflow-start/main.go b/eager-workflow-start/main.go new file mode 100644 index 00000000..0d583e2c --- /dev/null +++ b/eager-workflow-start/main.go @@ -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) +} From 22ecc06896de2cb5ae4b5651e830ef0b0c58d767 Mon Sep 17 00:00:00 2001 From: Antonio Lain Date: Wed, 6 Mar 2024 21:20:13 -0800 Subject: [PATCH 2/2] Modify readme --- eager-workflow-start/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/eager-workflow-start/README.md b/eager-workflow-start/README.md index 667671b2..0e35340a 100644 --- a/eager-workflow-start/README.md +++ b/eager-workflow-start/README.md @@ -1,4 +1,13 @@ +### 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