Skip to content

Commit 9bbcc7c

Browse files
add patching sample (#56)
* add patching sample * add comments * pr feedback
1 parent 6788b0e commit 9bbcc7c

10 files changed

Lines changed: 380 additions & 1 deletion

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Prerequisites:
2727
* [encryption](encryption) - Demonstrates how to make a codec for end-to-end encryption.
2828
* [env_config](env_config) - Load client configuration from TOML files with programmatic overrides.
2929
* [message_passing_simple](message_passing_simple) - Simple workflow that accepts signals, queries, and updates.
30+
* [patching](patching) - Demonstrates how to safely alter a workflow.
3031
* [polling/infrequent](polling/infrequent) - Implement an infrequent polling mechanism using Temporal's automatic Activity Retry feature.
3132
* [rails_app](rails_app) - Basic Rails API application using Temporal workflows and activities.
3233
* [saga](saga) - Using undo/compensation using a very simplistic Saga pattern.
@@ -40,4 +41,3 @@ Prerequisites:
4041
To check format and test this repository, run:
4142

4243
bundle exec rake
43-

patching/README.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Patching
2+
3+
This sample shows how to safely update a workflow in stages using `Temporalio::Workflow.patch` and `Temporalio::Workflow.deprecate_patch`.
4+
5+
To run, first see [README.md](../README.md) for prerequisites. Then follow the stages below.
6+
7+
## Stage 1
8+
9+
To simulate our initial workflow, start a worker in another terminal
10+
11+
```bash
12+
bundle exec ruby worker.rb initial
13+
```
14+
15+
Now start a workflow:
16+
17+
```bash
18+
bundle exec ruby starter.rb start initial-id
19+
```
20+
21+
This will output `Started workflow with id initial-id`.
22+
23+
Now query the workflow:
24+
25+
```bash
26+
bundle exec ruby starter.rb query initial-id
27+
```
28+
29+
This will output `Query result for id initial-id: pre-patch`
30+
31+
## Stage 2
32+
33+
This stage is for needing to run old and new workflows at the same time.
34+
To simulate our patched workflow, stop the worker from before and start it again with the patched workflow:
35+
36+
```bash
37+
bundle exec ruby worker.rb patched
38+
```
39+
40+
Now let's start another workflow with this patched code:
41+
42+
```bash
43+
bundle exec ruby starter.rb start patched-id
44+
```
45+
46+
This will output `Started workflow with id patched-id-workflow-id`.
47+
48+
Now query the old workflow that's still running:
49+
50+
```bash
51+
bundle exec starter.rb query initial-id
52+
```
53+
54+
This will output "Query result for id initial-id: pre-patch" since it is pre-patch.
55+
56+
But if we execute a query against the new code:
57+
58+
```bash
59+
bundle exec starter.rb query patched-id
60+
```
61+
62+
We get "Query result for id patched-id: post-patch".
63+
64+
This is how old workflow code can take old paths and new workflow code can take new paths.
65+
66+
## Stage 3
67+
68+
Once we know that all workflows that started with the initial code from "Stage 1" are no longer running,
69+
we don't need the patch so we can deprecate it.
70+
To use the patch deprecated workflow, stop the worker from before and start it again with:
71+
72+
```bash
73+
bundle exec ruby worker.rb deprecated
74+
```
75+
76+
Querying the initial workflow should error now.
77+
78+
```bash
79+
bundle exec ruby starter.rb query initial-id
80+
```
81+
82+
Throws an error: `[TMPRL1100] Nondeterminism error: Activity type of scheduled event 'PrePatch' does not match activity type of activity command 'PostPatch'`
83+
84+
All workflows in "Stage 2" and any new workflows can be queried.
85+
86+
Now let's start another workflow with this patch deprecated code:
87+
88+
```bash
89+
bundle exec ruby starter.rb start deprecated-id
90+
```
91+
92+
This will output `Started workflow with id deprecated-id`.
93+
Now query the patched workflow from "Stage 2"
94+
95+
```bash
96+
bundle exec ruby starter.rb query patched-id
97+
```
98+
99+
This will output "Query result for id patched-id: post-patch".
100+
101+
And if we execute a query against the latest workflow:
102+
103+
```bash
104+
bundle exec ruby starter.rb query deprecated-id
105+
```
106+
107+
As expected, this will output "Query result for id deprecated-id: post-patch".
108+
109+
## Stage 4
110+
111+
Once we know we don't even have any workflows running on "Stage 2" or before (i.e. the workflow with the patch with both code paths), we can just remove the patch deprecation altogether.
112+
To use the patch complete workflow, stop the worker from before and start it again with:
113+
114+
```bash
115+
bundle exec ruby worker.rb complete
116+
```
117+
118+
All workflows in "Stage 3" and any new workflows will work.
119+
Now let's start another workflow with this patch complete code:
120+
121+
```bash
122+
bundle exec ruby starter.rb start complete-id
123+
```
124+
125+
Now query the patch deprecated workflow that's still running:
126+
127+
```bash
128+
bundle exec ruby starter.rb query deprecated-id
129+
```
130+
131+
This will output "Query result for id deprecated-id: post-patch".
132+
133+
And if we execute a query against the latest workflow:
134+
135+
```bash
136+
bundle exec ruby starter.rb query completed-id
137+
```
138+
139+
As expected, this will output "Query result for id complete-id: post-patch".
140+
141+
Following these stages, we have successfully altered our workflow code.

patching/my_activities.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/activity'
4+
5+
module Patching
6+
module MyActivities
7+
class PrePatch < Temporalio::Activity::Definition
8+
def execute
9+
'pre-patch'
10+
end
11+
end
12+
13+
class PostPatch < Temporalio::Activity::Definition
14+
def execute
15+
'post-patch'
16+
end
17+
end
18+
end
19+
end

patching/starter.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/client'
4+
5+
# Create a client
6+
client = Temporalio::Client.connect('localhost:7233', 'default')
7+
8+
command, workflow_id = ARGV
9+
raise('Missing command argument. Valid commands are start and query') if command.nil?
10+
raise('Missing workflow_id') if workflow_id.nil?
11+
12+
case command
13+
when 'start'
14+
# Start a workflow with the given id
15+
client.start_workflow(
16+
:MyWorkflow,
17+
id: workflow_id,
18+
task_queue: 'patching-sample'
19+
)
20+
puts "Started workflow with id #{workflow_id}"
21+
when 'query'
22+
# Obtain a workflow handle for the given id and query the result
23+
handle = client.workflow_handle(workflow_id)
24+
result = handle.query(:result)
25+
puts "Query result for id #{workflow_id}: #{result}"
26+
else
27+
raise('Invalid command. Valid commands are start and query')
28+
end

patching/worker.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
require_relative 'my_activities'
4+
require_relative 'workflow_1_initial'
5+
require_relative 'workflow_2_patched'
6+
require_relative 'workflow_3_deprecated'
7+
require_relative 'workflow_4_complete'
8+
require 'logger'
9+
require 'temporalio/client'
10+
require 'temporalio/worker'
11+
12+
# Create a Temporal client
13+
client = Temporalio::Client.connect(
14+
'localhost:7233',
15+
'default',
16+
logger: Logger.new($stdout, level: Logger::INFO)
17+
)
18+
19+
workflow_versions = {
20+
'initial' => Patching::MyWorkflow1Initial,
21+
'patched' => Patching::MyWorkflow2Patched,
22+
'deprecated' => Patching::MyWorkflow3Deprecated,
23+
'complete' => Patching::MyWorkflow4Complete
24+
}
25+
26+
# Select which version of the workflow the worker should use
27+
workflow_version = ARGV.first || raise('Missing argument for workflow version')
28+
workflow = workflow_versions[workflow_version] ||
29+
raise("Unrecognized workflow #{workflow_version}. Accepted values are #{workflow_versions.keys.join(', ')}")
30+
# Create worker with the activities and workflow
31+
worker = Temporalio::Worker.new(
32+
client:,
33+
task_queue: 'patching-sample',
34+
activities: [Patching::MyActivities::PrePatch, Patching::MyActivities::PostPatch],
35+
workflows: [
36+
workflow
37+
]
38+
)
39+
40+
# Run the worker until SIGINT
41+
puts 'Starting worker (ctrl+c to exit)'
42+
worker.run(shutdown_signals: ['SIGINT'])

patching/workflow_1_initial.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/workflow'
4+
require_relative 'my_activities'
5+
6+
module Patching
7+
class MyWorkflow1Initial < Temporalio::Workflow::Definition
8+
workflow_name :MyWorkflow
9+
workflow_query_attr_reader :result
10+
11+
def execute
12+
@result = Temporalio::Workflow.execute_activity(
13+
MyActivities::PrePatch,
14+
start_to_close_timeout: 5 * 60
15+
)
16+
end
17+
end
18+
end

patching/workflow_2_patched.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/workflow'
4+
require_relative 'my_activities'
5+
6+
module Patching
7+
class MyWorkflow2Patched < Temporalio::Workflow::Definition
8+
workflow_name :MyWorkflow
9+
workflow_query_attr_reader :result
10+
11+
def execute
12+
# Decide which activity to use based on workflow's patch status
13+
@result = if Temporalio::Workflow.patched(:my_patch)
14+
Temporalio::Workflow.execute_activity(
15+
MyActivities::PostPatch,
16+
start_to_close_timeout: 5 * 60
17+
)
18+
else
19+
Temporalio::Workflow.execute_activity(
20+
MyActivities::PrePatch,
21+
start_to_close_timeout: 5 * 60
22+
)
23+
end
24+
end
25+
end
26+
end

patching/workflow_3_deprecated.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/workflow'
4+
require_relative 'my_activities'
5+
6+
module Patching
7+
class MyWorkflow3Deprecated < Temporalio::Workflow::Definition
8+
workflow_name :MyWorkflow
9+
workflow_query_attr_reader :result
10+
11+
def execute
12+
Temporalio::Workflow.deprecate_patch(:my_patch)
13+
@result = Temporalio::Workflow.execute_activity(
14+
MyActivities::PostPatch,
15+
start_to_close_timeout: 5 * 60
16+
)
17+
end
18+
end
19+
end

patching/workflow_4_complete.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/workflow'
4+
require_relative 'my_activities'
5+
6+
module Patching
7+
class MyWorkflow4Complete < Temporalio::Workflow::Definition
8+
workflow_name :MyWorkflow
9+
workflow_query_attr_reader :result
10+
11+
def execute
12+
@result = Temporalio::Workflow.execute_activity(
13+
MyActivities::PostPatch,
14+
start_to_close_timeout: 5 * 60
15+
)
16+
end
17+
end
18+
end

0 commit comments

Comments
 (0)