Skip to content

Commit 6788b0e

Browse files
authored
Eager workflow start sample (#55)
1 parent 2489e17 commit 6788b0e

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Prerequisites:
2323
* [context_propagation](context_propagation) - Use interceptors to propagate thread/fiber local data from clients
2424
through workflows/activities.
2525
* [dsl](dsl) - Demonstrates having a workflow interpret/invoke arbitrary steps defined in a DSL.
26+
* [eager_wf_start](eager_wf_start) - Demonstrates Eager Workflow Start to reduce latency for workflows that start with a local activity.
2627
* [encryption](encryption) - Demonstrates how to make a codec for end-to-end encryption.
2728
* [env_config](env_config) - Load client configuration from TOML files with programmatic overrides.
2829
* [message_passing_simple](message_passing_simple) - Simple workflow that accepts signals, queries, and updates.

eager_wf_start/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Eager Workflow Start
2+
3+
This sample shows how to create a workflow that uses Eager Workflow Start.
4+
5+
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.
6+
7+
You can read more about Eager Workflow Start in our:
8+
9+
- [Eager Workflow Start blog](https://temporal.io/blog/improving-latency-with-eager-workflow-start)
10+
- [Worker Performance Docs](https://docs.temporal.io/develop/worker-performance#eager-workflow-start)
11+
12+
To run, first see [README.md](../README.md) for prerequisites. Then run the sample via:
13+
14+
bundle exec ruby eager_wf_start/run.rb

eager_wf_start/eager_workflow.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/workflow'
4+
require_relative 'greeting_activity'
5+
6+
module EagerWfStart
7+
class EagerWorkflow < Temporalio::Workflow::Definition
8+
def execute(name)
9+
Temporalio::Workflow.execute_local_activity(
10+
GreetingActivity,
11+
name,
12+
schedule_to_close_timeout: 5
13+
)
14+
end
15+
end
16+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/activity'
4+
5+
module EagerWfStart
6+
class GreetingActivity < Temporalio::Activity::Definition
7+
def execute(name)
8+
"Hello, #{name}!"
9+
end
10+
end
11+
end

eager_wf_start/run.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'securerandom'
4+
require 'temporalio/client'
5+
require 'temporalio/worker'
6+
require_relative 'eager_workflow'
7+
require_relative 'greeting_activity'
8+
9+
TASK_QUEUE = 'eager-wf-start-task-queue'
10+
11+
# Note that the worker and client run in the same process and share the same client connection
12+
client = Temporalio::Client.connect('localhost:7233', 'default')
13+
14+
worker = Temporalio::Worker.new(
15+
client:,
16+
task_queue: TASK_QUEUE,
17+
workflows: [EagerWfStart::EagerWorkflow],
18+
activities: [EagerWfStart::GreetingActivity]
19+
)
20+
21+
# Run worker in the background while we start the workflow
22+
worker.run do
23+
handle = client.start_workflow(
24+
EagerWfStart::EagerWorkflow,
25+
'Temporal',
26+
id: "eager-workflow-id-#{SecureRandom.uuid}",
27+
task_queue: TASK_QUEUE,
28+
request_eager_start: true
29+
)
30+
31+
puts handle.result
32+
end

0 commit comments

Comments
 (0)