-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcontext.rb
More file actions
505 lines (417 loc) · 17.9 KB
/
context.rb
File metadata and controls
505 lines (417 loc) · 17.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
require 'securerandom'
require 'temporal/activity/context'
require 'temporal/execution_options'
require 'temporal/errors'
require 'temporal/thread_local_context'
require 'temporal/workflow/history/event_target'
require 'temporal/workflow/command'
require 'temporal/workflow/context_helpers'
require 'temporal/workflow/future'
require 'temporal/workflow/child_workflow_future'
require 'temporal/workflow/replay_aware_logger'
require 'temporal/workflow/stack_trace_tracker'
require 'temporal/workflow/state_manager'
require 'temporal/workflow/signal'
# This context class is available in the workflow implementation
# and provides context and methods for interacting with Temporal
#
module Temporal
class Workflow
class Context
attr_reader :metadata, :config
def initialize(state_manager, dispatcher, workflow_class, metadata, config, query_registry, track_stack_trace)
@state_manager = state_manager
@dispatcher = dispatcher
@query_registry = query_registry
@workflow_class = workflow_class
@metadata = metadata
@completed = false
@config = config
if track_stack_trace
@stack_trace_tracker = StackTraceTracker.new
else
@stack_trace_tracker = nil
end
query_registry.register(StackTraceTracker::STACK_TRACE_QUERY_NAME) do
stack_trace_tracker&.to_s
end
end
def completed?
@completed
end
def logger
@logger ||= ReplayAwareLogger.new(
@config.logger,
replaying: -> { state_manager.replay? && !@config.log_on_workflow_replay }
)
@logger
end
def headers
metadata.headers
end
# Retrieves a hash of all current search attributes on this workflow run. Attributes
# can be set in a workflow by calling upsert_search_attributes or when starting a
# workflow by specifying the search_attributes option.
def search_attributes
state_manager.search_attributes
end
def has_release?(release_name)
state_manager.release?(release_name.to_s)
end
# Returns information about the workflow run's history up to this point. This can be used to
# determine when to continue as new.
def history_size
state_manager.history_size
end
def execute_activity(activity_class, *input, **args)
options = args.delete(:options) || {}
input << args unless args.empty?
execution_options = ExecutionOptions.new(activity_class, options, config.default_execution_options)
command = Command::ScheduleActivity.new(
activity_id: options[:activity_id],
activity_type: execution_options.name,
input: input,
task_queue: execution_options.task_queue,
retry_policy: execution_options.retry_policy,
timeouts: execution_options.timeouts,
headers: config.header_propagator_chain.inject(execution_options.headers)
)
target, cancelation_id = schedule_command(command)
future = Future.new(target, self, cancelation_id: cancelation_id)
dispatcher.register_handler(target, 'completed') do |result|
future.set(result)
future.success_callbacks.each { |callback| call_in_fiber(callback, result) }
end
dispatcher.register_handler(target, 'failed') do |exception|
future.fail(exception)
future.failure_callbacks.each { |callback| call_in_fiber(callback, exception) }
end
future
end
def execute_activity!(activity_class, *input, **args)
future = execute_activity(activity_class, *input, **args)
result = future.get
raise result if future.failed?
result
end
# TODO: how to handle failures?
def execute_local_activity(activity_class, *input, **args)
input << args unless args.empty?
side_effect do
# TODO: this probably requires a local context implementation
context = Activity::Context.new(nil, nil, nil, nil)
activity_class.execute_in_context(context, input)
end
end
def execute_workflow(workflow_class, *input, **args)
options = args.delete(:options) || {}
input << args unless args.empty?
parent_close_policy = options.delete(:parent_close_policy)
cron_schedule = options.delete(:cron_schedule)
workflow_id_reuse_policy = options.delete(:workflow_id_reuse_policy)
execution_options = ExecutionOptions.new(workflow_class, options, config.default_execution_options)
command = Command::StartChildWorkflow.new(
workflow_id: options[:workflow_id] || SecureRandom.uuid,
workflow_type: execution_options.name,
input: input,
namespace: execution_options.namespace,
task_queue: execution_options.task_queue,
retry_policy: execution_options.retry_policy,
priority: execution_options.priority,
parent_close_policy: parent_close_policy,
timeouts: execution_options.timeouts,
headers: config.header_propagator_chain.inject(execution_options.headers),
cron_schedule: cron_schedule,
memo: execution_options.memo,
workflow_id_reuse_policy: workflow_id_reuse_policy,
search_attributes: Helpers.process_search_attributes(execution_options.search_attributes),
)
target, cancelation_id = schedule_command(command)
child_workflow_future = ChildWorkflowFuture.new(target, self, cancelation_id: cancelation_id)
dispatcher.register_handler(target, 'completed') do |result|
child_workflow_future.set(result)
child_workflow_future.success_callbacks.each { |callback| call_in_fiber(callback, result) }
end
dispatcher.register_handler(target, 'failed') do |exception|
# if the child workflow didn't start already then also fail that future
unless child_workflow_future.child_workflow_execution_future.ready?
child_workflow_future.child_workflow_execution_future.fail(exception)
child_workflow_future.child_workflow_execution_future.failure_callbacks.each { |callback| call_in_fiber(callback, exception) }
end
child_workflow_future.fail(exception)
child_workflow_future.failure_callbacks.each { |callback| call_in_fiber(callback, exception) }
end
dispatcher.register_handler(target, 'started') do |event|
# once the workflow starts, complete the child workflow execution future
child_workflow_future.child_workflow_execution_future.set(event)
child_workflow_future.child_workflow_execution_future.success_callbacks.each { |callback| call_in_fiber(callback, result) }
end
child_workflow_future
end
def execute_workflow!(workflow_class, *input, **args)
future = execute_workflow(workflow_class, *input, **args)
result = future.get
raise result if future.failed?
result
end
def schedule_workflow(workflow_class, cron_schedule, *input, **args)
args[:options] = (args[:options] || {}).merge(cron_schedule: cron_schedule)
execute_workflow(workflow_class, *input, **args)
end
def side_effect(&block)
marker = state_manager.next_side_effect
return marker.last if marker
result = block.call
command = Command::RecordMarker.new(name: StateManager::SIDE_EFFECT_MARKER, details: result)
schedule_command(command)
result
end
def sleep(timeout)
start_timer(timeout).wait
end
def start_timer(timeout, timer_id = nil)
command = Command::StartTimer.new(timeout: timeout, timer_id: timer_id)
target, cancelation_id = schedule_command(command)
future = Future.new(target, self, cancelation_id: cancelation_id)
dispatcher.register_handler(target, 'fired') do |result|
future.set(result)
future.success_callbacks.each { |callback| call_in_fiber(callback, result) }
end
dispatcher.register_handler(target, 'canceled') do |exception|
future.fail(exception)
future.failure_callbacks.each { |callback| call_in_fiber(callback, exception) }
end
future
end
def name
@metadata.name
end
def cancel_timer(timer_id)
command = Command::CancelTimer.new(timer_id: timer_id)
schedule_command(command)
end
# TODO: check if workflow can be completed
def complete(result = nil)
command = Command::CompleteWorkflow.new(result: result)
schedule_command(command)
completed!
end
# TODO: check if workflow can be failed
def fail(exception)
command = Command::FailWorkflow.new(exception: exception)
schedule_command(command)
completed!
end
def continue_as_new(*input, **args)
options = args.delete(:options) || {}
input << args unless args.empty?
# If memo or headers are not overridden, use those from the current run
options_from_metadata = {
memo: metadata.memo,
headers: metadata.headers,
}
options = options_from_metadata.merge(options)
execution_options = ExecutionOptions.new(workflow_class, options, config.default_execution_options)
command = Command::ContinueAsNew.new(
workflow_type: execution_options.name,
task_queue: execution_options.task_queue,
input: input,
timeouts: execution_options.timeouts,
retry_policy: execution_options.retry_policy,
headers: config.header_propagator_chain.inject(execution_options.headers),
memo: execution_options.memo,
search_attributes: Helpers.process_search_attributes(execution_options.search_attributes)
)
schedule_command(command)
completed!
end
# Block workflow progress until all futures finish
def wait_for_all(*futures)
futures.each(&:wait)
return
end
# Block workflow progress until one of the futures completes. Passing
# in an empty array will immediately unblock.
def wait_for_any(*futures)
return if futures.empty? || futures.any?(&:finished?)
fiber = Fiber.current
handlers = futures.map do |future|
dispatcher.register_handler(future.target, Dispatcher::WILDCARD) do
fiber.resume if future.finished?
end
end
stack_trace_tracker&.record
begin
Fiber.yield
ensure
stack_trace_tracker&.clear
handlers.each(&:unregister)
end
return
end
# Block workflow progress until the specified block evaluates to true.
def wait_until(&unblock_condition)
raise 'You must pass a block to wait_until' if unblock_condition.nil?
return if unblock_condition.call
fiber = Fiber.current
# wait_until condition blocks often read state modified by target-specfic handlers like
# signal handlers or callbacks for timer or activity completion. Running the wait_until
# handlers after the other handlers ensures that state is correctly updated before being
# read.
handler = dispatcher.register_handler(
Dispatcher::WILDCARD, # any target
Dispatcher::WILDCARD, # any event type
Dispatcher::Order::AT_END) do
fiber.resume if unblock_condition.call
end
stack_trace_tracker&.record
begin
Fiber.yield
ensure
stack_trace_tracker&.clear
handler.unregister
end
return
end
def now
state_manager.local_time
end
# Define a signal handler to receive signals onto the workflow. When
# +name+ is defined, this creates a named signal handler which will be
# invoked whenever a signal named +name+ is received. A handler without
# a set name (defaults to nil) will be the default handler and will receive
# all signals that do not match a named signal handler.
#
# @param signal_name [String, Symbol, nil] an optional signal name; converted to a String
def on_signal(signal_name = nil, &block)
first_task_signals = if state_manager.sdk_flags.include?(SDKFlags::SAVE_FIRST_TASK_SIGNALS)
state_manager.first_task_signals
else
[]
end
if signal_name
target = Signal.new(signal_name)
dispatcher.register_handler(target, 'signaled') do |_, input|
# do not pass signal name when triggering a named handler
call_in_fiber(block, input)
end
first_task_signals.each do |name, input|
if name == signal_name
call_in_fiber(block, input)
end
end
else
dispatcher.register_handler(Dispatcher::WILDCARD, 'signaled') do |signal, input|
call_in_fiber(block, signal, input)
end
first_task_signals.each do |name, input|
call_in_fiber(block, name, input)
end
end
return
end
def on_query(query, &block)
query_registry.register(query, &block)
end
def cancel_activity(activity_id)
command = Command::RequestActivityCancellation.new(activity_id: activity_id)
schedule_command(command)
end
def cancel(target, cancelation_id)
case target.type
when History::EventTarget::ACTIVITY_TYPE
cancel_activity(cancelation_id)
when History::EventTarget::TIMER_TYPE
cancel_timer(cancelation_id)
else
raise "#{target} can not be canceled"
end
end
# Send a signal from inside a workflow to another workflow. Not to be confused with
# Client#signal_workflow which sends a signal from outside a workflow to a workflow.
#
# @param workflow [Temporal::Workflow, nil] workflow class or nil
# @param signal [String] name of the signal to send
# @param workflow_id [String]
# @param run_id [String]
# @param input [String, Array, nil] optional arguments for the signal
# @param namespace [String, nil] if nil, choose the one declared on the workflow class or the
# global default
# @param child_workflow_only [Boolean] indicates whether the signal should only be delivered to a
# child workflow; defaults to false
#
# @return [Future] future
def signal_external_workflow(workflow, signal, workflow_id, run_id = nil, input = nil, namespace: nil, child_workflow_only: false)
execution_options = ExecutionOptions.new(workflow, {}, config.default_execution_options)
command = Command::SignalExternalWorkflow.new(
namespace: namespace || execution_options.namespace,
execution: {
workflow_id: workflow_id,
run_id: run_id
},
signal_name: signal,
input: input,
child_workflow_only: child_workflow_only
)
target, cancelation_id = schedule_command(command)
future = Future.new(target, self, cancelation_id: cancelation_id)
dispatcher.register_handler(target, 'completed') do |result|
future.set(result)
future.success_callbacks.each { |callback| call_in_fiber(callback, result) }
end
dispatcher.register_handler(target, 'failed') do |exception|
future.fail(exception)
future.failure_callbacks.each { |callback| call_in_fiber(callback, exception) }
end
future
end
# Replaces or adds the values of your custom search attributes specified during a workflow's execution.
# To use this your server must enable advanced visibility using SQL starting with version 1.20 or
# Elasticsearch on all versions. The attributes must be pre-configured.
# See https://docs.temporal.io/docs/concepts/what-is-a-search-attribute/
#
# Do be aware that non-deterministic upserting of search attributes can lead to "phantom"
# attributes that are available in code but not on Temporal server. For example, if your code
# upserted {"foo" => 1} then changed to upsert {"bar" => 2} without proper versioning, you
# will see {"foo" => 1, "bar" => 2} in search attributes in workflow code even though
# {"bar" => 2} was never upserted on Temporal server. When the same search attribute
# name is used with a different value, you will see a similar case where the new value will
# be present until the end of the history window, then change to the old version after that. This
# does at least match the "old" value that will be present on the server.
#
# @param search_attributes [Hash]
# If an attribute is registered as a Datetime, you can pass in a Time: e.g.
# workflow.now
# or as a string in UTC ISO-8601 format:
# workflow.now.utc.iso8601
# It would look like: "2022-03-01T17:39:06Z"
# @return [Hash] the search attributes after any preprocessing.
#
def upsert_search_attributes(search_attributes)
search_attributes = Helpers.process_search_attributes(search_attributes)
if search_attributes.empty?
raise ArgumentError, "Cannot upsert an empty hash for search_attributes, as this would do nothing."
end
command = Command::UpsertSearchAttributes.new(
search_attributes: search_attributes
)
schedule_command(command)
search_attributes
end
private
attr_reader :state_manager, :dispatcher, :workflow_class, :query_registry, :stack_trace_tracker
def completed!
@completed = true
end
def schedule_command(command)
state_manager.schedule(command)
end
def call_in_fiber(block, *args)
Fiber.new do
Temporal::ThreadLocalContext.set(self)
block.call(*args)
end.resume
end
end
end
end