From ab4ef629572723ccf977a08fcdfd5d6db2210337 Mon Sep 17 00:00:00 2001 From: Thomas Hardy Date: Thu, 9 Oct 2025 09:44:32 -0700 Subject: [PATCH] Eager workflow start sample --- README.md | 1 + eager_wf_start/README.md | 14 +++++++++++++ eager_wf_start/eager_workflow.rb | 16 +++++++++++++++ eager_wf_start/greeting_activity.rb | 11 ++++++++++ eager_wf_start/run.rb | 32 +++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+) create mode 100644 eager_wf_start/README.md create mode 100644 eager_wf_start/eager_workflow.rb create mode 100644 eager_wf_start/greeting_activity.rb create mode 100644 eager_wf_start/run.rb diff --git a/README.md b/README.md index 5df5ec4..64c24df 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Prerequisites: * [context_propagation](context_propagation) - Use interceptors to propagate thread/fiber local data from clients through workflows/activities. * [dsl](dsl) - Demonstrates having a workflow interpret/invoke arbitrary steps defined in a DSL. +* [eager_wf_start](eager_wf_start) - Demonstrates Eager Workflow Start to reduce latency for workflows that start with a local activity. * [encryption](encryption) - Demonstrates how to make a codec for end-to-end encryption. * [env_config](env_config) - Load client configuration from TOML files with programmatic overrides. * [message_passing_simple](message_passing_simple) - Simple workflow that accepts signals, queries, and updates. diff --git a/eager_wf_start/README.md b/eager_wf_start/README.md new file mode 100644 index 0000000..8603375 --- /dev/null +++ b/eager_wf_start/README.md @@ -0,0 +1,14 @@ +# Eager Workflow Start + +This sample shows how to create a workflow that uses Eager Workflow Start. + +The target use case is workflows whose first task needs to execute quickly (ex: payment verification in an online checkout workflow). That work typically can't be done directly in the workflow (ex: using web APIs, databases, etc.), and also needs to avoid the overhead of dispatching another task. Using a Local Activity suffices both needs, which this sample demonstrates. + +You can read more about Eager Workflow Start in our: + +- [Eager Workflow Start blog](https://temporal.io/blog/improving-latency-with-eager-workflow-start) +- [Worker Performance Docs](https://docs.temporal.io/develop/worker-performance#eager-workflow-start) + +To run, first see [README.md](../README.md) for prerequisites. Then run the sample via: + + bundle exec ruby eager_wf_start/run.rb diff --git a/eager_wf_start/eager_workflow.rb b/eager_wf_start/eager_workflow.rb new file mode 100644 index 0000000..8b64134 --- /dev/null +++ b/eager_wf_start/eager_workflow.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'temporalio/workflow' +require_relative 'greeting_activity' + +module EagerWfStart + class EagerWorkflow < Temporalio::Workflow::Definition + def execute(name) + Temporalio::Workflow.execute_local_activity( + GreetingActivity, + name, + schedule_to_close_timeout: 5 + ) + end + end +end diff --git a/eager_wf_start/greeting_activity.rb b/eager_wf_start/greeting_activity.rb new file mode 100644 index 0000000..1d61bce --- /dev/null +++ b/eager_wf_start/greeting_activity.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require 'temporalio/activity' + +module EagerWfStart + class GreetingActivity < Temporalio::Activity::Definition + def execute(name) + "Hello, #{name}!" + end + end +end diff --git a/eager_wf_start/run.rb b/eager_wf_start/run.rb new file mode 100644 index 0000000..76b46d5 --- /dev/null +++ b/eager_wf_start/run.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +require 'securerandom' +require 'temporalio/client' +require 'temporalio/worker' +require_relative 'eager_workflow' +require_relative 'greeting_activity' + +TASK_QUEUE = 'eager-wf-start-task-queue' + +# Note that the worker and client run in the same process and share the same client connection +client = Temporalio::Client.connect('localhost:7233', 'default') + +worker = Temporalio::Worker.new( + client:, + task_queue: TASK_QUEUE, + workflows: [EagerWfStart::EagerWorkflow], + activities: [EagerWfStart::GreetingActivity] +) + +# Run worker in the background while we start the workflow +worker.run do + handle = client.start_workflow( + EagerWfStart::EagerWorkflow, + 'Temporal', + id: "eager-workflow-id-#{SecureRandom.uuid}", + task_queue: TASK_QUEUE, + request_eager_start: true + ) + + puts handle.result +end