Class: Temporal::Workflow::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/temporal/workflow/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_manager, dispatcher, workflow_class, metadata) ⇒ Context

Returns a new instance of Context.



20
21
22
23
24
25
26
# File 'lib/temporal/workflow/context.rb', line 20

def initialize(state_manager, dispatcher, workflow_class, )
  @state_manager = state_manager
  @dispatcher = dispatcher
  @workflow_class = workflow_class
  @metadata = 
  @completed = false
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/temporal/workflow/context.rb', line 18

def 
  @metadata
end

Instance Method Details

#cancel(target, cancelation_id) ⇒ Object



247
248
249
250
251
252
253
254
255
256
# File 'lib/temporal/workflow/context.rb', line 247

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

#cancel_activity(activity_id) ⇒ Object



241
242
243
244
245
# File 'lib/temporal/workflow/context.rb', line 241

def cancel_activity(activity_id)
  command = Command::RequestActivityCancellation.new(activity_id: activity_id)

  schedule_command(command)
end

#cancel_timer(timer_id) ⇒ Object



174
175
176
177
# File 'lib/temporal/workflow/context.rb', line 174

def cancel_timer(timer_id)
  command = Command::CancelTimer.new(timer_id: timer_id)
  schedule_command(command)
end

#complete(result = nil) ⇒ Object

TODO: check if workflow can be completed



180
181
182
183
184
# File 'lib/temporal/workflow/context.rb', line 180

def complete(result = nil)
  command = Command::CompleteWorkflow.new(result: result)
  schedule_command(command)
  completed!
end

#completed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/temporal/workflow/context.rb', line 28

def completed?
  @completed
end

#continue_as_new(*input, **args) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/temporal/workflow/context.rb', line 193

def continue_as_new(*input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(workflow_class, 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: execution_options.headers
  )
  schedule_command(command)
  completed!
end

#execute_activity(activity_class, *input, **args) ⇒ Object



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
# File 'lib/temporal/workflow/context.rb', line 46

def execute_activity(activity_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(activity_class, options)

  command = Command::ScheduleActivity.new(
    activity_id: options[:activity_id],
    activity_type: execution_options.name,
    input: input,
    namespace: execution_options.namespace,
    task_queue: execution_options.task_queue,
    retry_policy: execution_options.retry_policy,
    timeouts: execution_options.timeouts,
    headers: 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

#execute_activity!(activity_class, *input, **args) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/temporal/workflow/context.rb', line 79

def execute_activity!(activity_class, *input, **args)
  future = execute_activity(activity_class, *input, **args)
  result = future.get

  raise result if future.failed?

  result
end

#execute_local_activity(activity_class, *input, **args) ⇒ Object

TODO: how to handle failures?



89
90
91
92
93
94
95
96
97
# File 'lib/temporal/workflow/context.rb', line 89

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)
    activity_class.execute_in_context(context, input)
  end
end

#execute_workflow(workflow_class, *input, **args) ⇒ Object



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
# File 'lib/temporal/workflow/context.rb', line 99

def execute_workflow(workflow_class, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(workflow_class, 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,
    timeouts: execution_options.timeouts,
    headers: 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

#execute_workflow!(workflow_class, *input, **args) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/temporal/workflow/context.rb', line 132

def execute_workflow!(workflow_class, *input, **args)
  future = execute_workflow(workflow_class, *input, **args)
  result = future.get

  raise result if future.failed?

  result
end

#fail(exception) ⇒ Object

TODO: check if workflow can be failed



187
188
189
190
191
# File 'lib/temporal/workflow/context.rb', line 187

def fail(exception)
  command = Command::FailWorkflow.new(exception: exception)
  schedule_command(command)
  completed!
end

#has_release?(release_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/temporal/workflow/context.rb', line 42

def has_release?(release_name)
  state_manager.release?(release_name.to_s)
end

#headersObject



38
39
40
# File 'lib/temporal/workflow/context.rb', line 38

def headers
  .headers
end

#loggerObject



32
33
34
35
36
# File 'lib/temporal/workflow/context.rb', line 32

def logger
  @logger ||= ReplayAwareLogger.new(Temporal.logger)
  @logger.replay = state_manager.replay?
  @logger
end

#nowObject



229
230
231
# File 'lib/temporal/workflow/context.rb', line 229

def now
  state_manager.local_time
end

#on_signal(&block) ⇒ Object



233
234
235
236
237
238
239
# File 'lib/temporal/workflow/context.rb', line 233

def on_signal(&block)
  target = History::EventTarget.workflow

  dispatcher.register_handler(target, 'signaled') do |signal, input|
    call_in_fiber(block, signal, input)
  end
end

#side_effect(&block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/temporal/workflow/context.rb', line 141

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

#sleep(timeout) ⇒ Object



152
153
154
# File 'lib/temporal/workflow/context.rb', line 152

def sleep(timeout)
  start_timer(timeout).wait
end

#start_timer(timeout, timer_id = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/temporal/workflow/context.rb', line 156

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

#wait_for(future) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/temporal/workflow/context.rb', line 217

def wait_for(future)
  fiber = Fiber.current

  dispatcher.register_handler(future.target, Dispatcher::WILDCARD) do
    fiber.resume if future.finished?
  end

  Fiber.yield

  return
end

#wait_for_all(*futures) ⇒ Object



211
212
213
214
215
# File 'lib/temporal/workflow/context.rb', line 211

def wait_for_all(*futures)
  futures.each(&:wait)

  return
end