Class: Temporalio::Internal::Worker::WorkflowInstance::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/temporalio/internal/worker/workflow_instance/context.rb

Overview

Context for all workflow calls. All calls in the Workflow class should call a method on this class and then this class can delegate the call as needed to other parts of the workflow instance system.

Instance Method Summary collapse

Constructor Details

#initialize(instance) ⇒ Context

Returns a new instance of Context.



19
20
21
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 19

def initialize(instance)
  @instance = instance
end

Instance Method Details

#_cancel_external_workflow(id:, run_id:) ⇒ Object



295
296
297
298
299
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 295

def _cancel_external_workflow(id:, run_id:)
  @outbound.cancel_external_workflow(
    Temporalio::Worker::Interceptor::Workflow::CancelExternalWorkflowInput.new(id:, run_id:)
  )
end

#_outbound=(outbound) ⇒ Object



301
302
303
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 301

def _outbound=(outbound)
  @outbound = outbound
end

#_signal_child_workflow(id:, signal:, args:, cancellation:) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 305

def _signal_child_workflow(id:, signal:, args:, cancellation:)
  @outbound.signal_child_workflow(
    Temporalio::Worker::Interceptor::Workflow::SignalChildWorkflowInput.new(
      id:,
      signal:,
      args:,
      cancellation:,
      headers: {}
    )
  )
end

#_signal_external_workflow(id:, run_id:, signal:, args:, cancellation:) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 317

def _signal_external_workflow(id:, run_id:, signal:, args:, cancellation:)
  @outbound.signal_external_workflow(
    Temporalio::Worker::Interceptor::Workflow::SignalExternalWorkflowInput.new(
      id:,
      run_id:,
      signal:,
      args:,
      cancellation:,
      headers: {}
    )
  )
end

#all_handlers_finished?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 23

def all_handlers_finished?
  @instance.in_progress_handlers.empty?
end

#cancellationObject



27
28
29
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 27

def cancellation
  @instance.cancellation
end

#continue_as_new_suggestedObject



31
32
33
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 31

def continue_as_new_suggested
  @instance.continue_as_new_suggested
end

#current_history_lengthObject



35
36
37
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 35

def current_history_length
  @instance.current_history_length
end

#current_history_sizeObject



39
40
41
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 39

def current_history_size
  @instance.current_history_size
end

#current_update_infoObject



43
44
45
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 43

def current_update_info
  Fiber[:__temporal_update_info]
end

#deprecate_patch(patch_id) ⇒ Object



47
48
49
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 47

def deprecate_patch(patch_id)
  @instance.patch(patch_id:, deprecated: true)
end

#execute_activity(activity, *args, task_queue:, schedule_to_close_timeout:, schedule_to_start_timeout:, start_to_close_timeout:, heartbeat_timeout:, retry_policy:, cancellation:, cancellation_type:, activity_id:, disable_eager_execution:) ⇒ Object



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
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 51

def execute_activity(
  activity,
  *args,
  task_queue:,
  schedule_to_close_timeout:,
  schedule_to_start_timeout:,
  start_to_close_timeout:,
  heartbeat_timeout:,
  retry_policy:,
  cancellation:,
  cancellation_type:,
  activity_id:,
  disable_eager_execution:
)
  @outbound.execute_activity(
    Temporalio::Worker::Interceptor::Workflow::ExecuteActivityInput.new(
      activity:,
      args:,
      task_queue: task_queue || info.task_queue,
      schedule_to_close_timeout:,
      schedule_to_start_timeout:,
      start_to_close_timeout:,
      heartbeat_timeout:,
      retry_policy:,
      cancellation:,
      cancellation_type:,
      activity_id:,
      disable_eager_execution: disable_eager_execution || @instance.disable_eager_activity_execution,
      headers: {}
    )
  )
end

#execute_local_activity(activity, *args, schedule_to_close_timeout:, schedule_to_start_timeout:, start_to_close_timeout:, retry_policy:, local_retry_threshold:, cancellation:, cancellation_type:, activity_id:) ⇒ Object



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
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 84

def execute_local_activity(
  activity,
  *args,
  schedule_to_close_timeout:,
  schedule_to_start_timeout:,
  start_to_close_timeout:,
  retry_policy:,
  local_retry_threshold:,
  cancellation:,
  cancellation_type:,
  activity_id:
)
  @outbound.execute_local_activity(
    Temporalio::Worker::Interceptor::Workflow::ExecuteLocalActivityInput.new(
      activity:,
      args:,
      schedule_to_close_timeout:,
      schedule_to_start_timeout:,
      start_to_close_timeout:,
      retry_policy:,
      local_retry_threshold:,
      cancellation:,
      cancellation_type:,
      activity_id:,
      headers: {}
    )
  )
end

#external_workflow_handle(workflow_id, run_id: nil) ⇒ Object



113
114
115
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 113

def external_workflow_handle(workflow_id, run_id: nil)
  ExternalWorkflowHandle.new(id: workflow_id, run_id:, instance: @instance)
end

#illegal_call_tracing_disabledObject



117
118
119
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 117

def illegal_call_tracing_disabled(&)
  @instance.illegal_call_tracing_disabled(&)
end

#infoObject



121
122
123
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 121

def info
  @instance.info
end

#initialize_continue_as_new_error(error) ⇒ Object



129
130
131
132
133
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 129

def initialize_continue_as_new_error(error)
  @outbound.initialize_continue_as_new_error(
    Temporalio::Worker::Interceptor::Workflow::InitializeContinueAsNewErrorInput.new(error:)
  )
end

#instanceObject



125
126
127
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 125

def instance
  @instance.instance
end

#loggerObject



135
136
137
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 135

def logger
  @instance.logger
end

#memoObject



139
140
141
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 139

def memo
  @instance.memo
end

#metric_meterObject



143
144
145
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 143

def metric_meter
  @instance.metric_meter
end

#nowObject



147
148
149
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 147

def now
  @instance.now
end

#patched(patch_id) ⇒ Object



151
152
153
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 151

def patched(patch_id)
  @instance.patch(patch_id:, deprecated: false)
end

#payload_converterObject



155
156
157
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 155

def payload_converter
  @instance.payload_converter
end

#query_handlersObject



159
160
161
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 159

def query_handlers
  @instance.query_handlers
end

#randomObject



163
164
165
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 163

def random
  @instance.random
end

#replaying?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 167

def replaying?
  @instance.replaying
end

#search_attributesObject



171
172
173
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 171

def search_attributes
  @instance.search_attributes
end

#signal_handlersObject



175
176
177
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 175

def signal_handlers
  @instance.signal_handlers
end

#sleep(duration, summary:, cancellation:) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 179

def sleep(duration, summary:, cancellation:)
  @outbound.sleep(
    Temporalio::Worker::Interceptor::Workflow::SleepInput.new(
      duration:,
      summary:,
      cancellation:
    )
  )
end

#start_child_workflow(workflow, *args, id:, task_queue:, cancellation:, cancellation_type:, parent_close_policy:, execution_timeout:, run_timeout:, task_timeout:, id_reuse_policy:, retry_policy:, cron_schedule:, memo:, search_attributes:) ⇒ Object



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
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 189

def start_child_workflow(
  workflow,
  *args,
  id:,
  task_queue:,
  cancellation:,
  cancellation_type:,
  parent_close_policy:,
  execution_timeout:,
  run_timeout:,
  task_timeout:,
  id_reuse_policy:,
  retry_policy:,
  cron_schedule:,
  memo:,
  search_attributes:
)
  @outbound.start_child_workflow(
    Temporalio::Worker::Interceptor::Workflow::StartChildWorkflowInput.new(
      workflow:,
      args:,
      id:,
      task_queue:,
      cancellation:,
      cancellation_type:,
      parent_close_policy:,
      execution_timeout:,
      run_timeout:,
      task_timeout:,
      id_reuse_policy:,
      retry_policy:,
      cron_schedule:,
      memo:,
      search_attributes:,
      headers: {}
    )
  )
end

#timeout(duration, exception_class, *exception_args, summary:) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 228

def timeout(duration, exception_class, *exception_args, summary:, &)
  raise 'Block required for timeout' unless block_given?

  # Run timer in background and block in foreground. This gives better stack traces than a future any-of race.
  # We make a detached cancellation because we don't want to link to workflow cancellation.
  sleep_cancel, sleep_cancel_proc = Cancellation.new
  fiber = Fiber.current
  Workflow::Future.new do
    Workflow.sleep(duration, summary:, cancellation: sleep_cancel)
    fiber.raise(exception_class, *exception_args) if fiber.alive? # steep:ignore
  rescue Exception => e # rubocop:disable Lint/RescueException
    # Re-raise in fiber
    fiber.raise(e) if fiber.alive?
  end

  begin
    yield
  ensure
    sleep_cancel_proc.call
  end
end

#update_handlersObject



250
251
252
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 250

def update_handlers
  @instance.update_handlers
end

#upsert_memo(hash) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 254

def upsert_memo(hash)
  # Convert to memo, apply updates, then add the command (so command adding is post validation)
  upserted_memo = ProtoUtils.memo_to_proto(hash, payload_converter)
  memo._update do |new_hash|
    hash.each do |key, val|
      # Nil means delete
      if val.nil?
        new_hash.delete(key.to_s)
      else
        new_hash[key.to_s] = val
      end
    end
  end
  @instance.add_command(
    Bridge::Api::WorkflowCommands::WorkflowCommand.new(
      modify_workflow_properties: Bridge::Api::WorkflowCommands::ModifyWorkflowProperties.new(
        upserted_memo:
      )
    )
  )
end

#upsert_search_attributes(*updates) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 276

def upsert_search_attributes(*updates)
  # Apply updates then add the command (so command adding is post validation)
  search_attributes._disable_mutations = false
  search_attributes.update!(*updates)
  @instance.add_command(
    Bridge::Api::WorkflowCommands::WorkflowCommand.new(
      upsert_workflow_search_attributes: Bridge::Api::WorkflowCommands::UpsertWorkflowSearchAttributes.new(
        search_attributes: updates.to_h(&:_to_proto_pair)
      )
    )
  )
ensure
  search_attributes._disable_mutations = true
end

#wait_condition(cancellation:) ⇒ Object



291
292
293
# File 'lib/temporalio/internal/worker/workflow_instance/context.rb', line 291

def wait_condition(cancellation:, &)
  @instance.scheduler.wait_condition(cancellation:, &)
end