-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathworkflows.rb
More file actions
186 lines (160 loc) · 6.29 KB
/
workflows.rb
File metadata and controls
186 lines (160 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true
require 'temporalio/common_enums'
require 'temporalio/workflow'
require 'temporalio/workflow/definition'
require_relative 'activities'
module WorkerVersioning
module Workflows
# AutoUpgradingWorkflowV1 will automatically move to the latest worker version. We'll be making
# changes to it, which must be replay safe.
#
# Note that generally you won't want or need to include a version number in your workflow name if
# you're using the worker versioning feature. This sample does it to illustrate changes to the
# same code over time - but really what we're demonstrating here is the evolution of what would
# have been one workflow definition.
class AutoUpgradingWorkflowV1 < Temporalio::Workflow::Definition
workflow_name :AutoUpgrading
workflow_versioning_behavior Temporalio::VersioningBehavior::AUTO_UPGRADE
def initialize
@signals = []
end
def execute
Temporalio::Workflow.logger.info('Changing workflow v1 started')
# This workflow will listen for signals from our starter, and upon each signal either run
# an activity, or conclude execution.
loop do
Temporalio::Workflow.wait_condition { @signals.any? }
signal = @signals.shift
if signal == 'do-activity'
Temporalio::Workflow.logger.info('Changing workflow v1 running activity')
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeActivity,
'v1',
start_to_close_timeout: 10
)
else
Temporalio::Workflow.logger.info('Concluding workflow v1')
return
end
end
end
workflow_signal
def do_next_signal(signal)
@signals << signal
end
end
# AutoUpgradingWorkflowV1b represents us having made *compatible* changes to
# AutoUpgradingWorkflowV1.
#
# The compatible changes we've made are:
# - Altering the log lines
# - Using the workflow.patched API to properly introduce branching behavior while maintaining
# compatibility
class AutoUpgradingWorkflowV1b < Temporalio::Workflow::Definition
workflow_name :AutoUpgrading
workflow_versioning_behavior Temporalio::VersioningBehavior::AUTO_UPGRADE
def initialize
@signals = []
end
def execute
Temporalio::Workflow.logger.info('Changing workflow v1b started')
# This workflow will listen for signals from our starter, and upon each signal either run
# an activity, or conclude execution.
loop do
Temporalio::Workflow.wait_condition { @signals.any? }
signal = @signals.shift
if signal == 'do-activity'
Temporalio::Workflow.logger.info('Changing workflow v1b running activity')
if Temporalio::Workflow.patched('DifferentActivity')
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeIncompatibleActivity,
WorkerVersioning::IncompatibleActivityInput.new(called_by: 'v1b', more_data: 'hello!'),
start_to_close_timeout: 10
)
else
# Note it is a valid compatible change to alter the input to an activity.
# However, because we're using the patched API, this branch will never be
# taken.
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeActivity,
'v1b',
start_to_close_timeout: 10
)
end
else
Temporalio::Workflow.logger.info('Concluding workflow v1b')
break
end
end
end
workflow_signal
def do_next_signal(signal)
@signals << signal
end
end
# PinnedWorkflowV1 demonstrates a workflow that likely has a short lifetime, and we want to always
# stay pinned to the same version it began on.
#
# Note that generally you won't want or need to include a version number in your workflow name if
# you're using the worker versioning feature. This sample does it to illustrate changes to the
# same code over time - but really what we're demonstrating here is the evolution of what would
# have been one workflow definition.
class PinnedWorkflowV1 < Temporalio::Workflow::Definition
workflow_name :Pinned
workflow_versioning_behavior Temporalio::VersioningBehavior::PINNED
def initialize
@signals = []
end
def execute
Temporalio::Workflow.logger.info('Pinned Workflow v1 started')
loop do
Temporalio::Workflow.wait_condition { @signals.any? }
signal = @signals.shift
break if signal == 'conclude'
end
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeActivity,
'Pinned-v1',
start_to_close_timeout: 10
)
end
workflow_signal
def do_next_signal(signal)
@signals << signal
end
end
# PinnedWorkflowV2 has changes that would make it incompatible with v1, and aren't protected by
# a patch.
class PinnedWorkflowV2 < Temporalio::Workflow::Definition
workflow_name :Pinned
workflow_versioning_behavior Temporalio::VersioningBehavior::PINNED
def initialize
@signals = []
end
def execute
Temporalio::Workflow.logger.info('Pinned Workflow v2 started')
# Here we call an activity where we didn't before, which is an incompatible change.
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeActivity,
'Pinned-v2',
start_to_close_timeout: 10
)
loop do
Temporalio::Workflow.wait_condition { @signals.any? }
signal = @signals.shift
break if signal == 'conclude'
end
# We've also changed the activity type here, another incompatible change
Temporalio::Workflow.execute_activity(
WorkerVersioning::Activities::SomeIncompatibleActivity,
{ called_by: 'Pinned-v2', more_data: 'hi' },
start_to_close_timeout: 10
)
end
workflow_signal
def do_next_signal(signal)
@signals << signal
end
end
end
end