Class: Temporalio::Internal::Worker::ActivityWorker

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

Overview

Worker for handling activity tasks. Upon overarching worker shutdown, #wait_all_complete should be used to wait for the activities to complete.

Defined Under Namespace

Classes: InboundImplementation, OutboundImplementation, RunningActivity

Constant Summary collapse

LOG_TASKS =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker:, bridge_worker:) ⇒ ActivityWorker

Returns a new instance of ActivityWorker.



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

def initialize(worker:, bridge_worker:)
  @worker = worker
  @bridge_worker = bridge_worker
  @runtime_metric_meter = worker.options.client.connection.options.runtime.metric_meter
  @data_converter_without_storage = worker.options.client.data_converter._without_storage

  # Create shared logger that gives scoped activity details
  @scoped_logger = ScopedLogger.new(@worker.options.logger)
  @scoped_logger.scoped_values_getter = proc {
    Activity::Context.current_or_nil&._scoped_logger_info
  }

  # Build up activity hash by name (can be nil for dynamic), failing if any fail validation
  @activities = worker.options.activities.each_with_object({}) do |act, hash|
    # Class means create each time, instance means just call, definition
    # does nothing special
    defn = Activity::Definition::Info.from_activity(act)
    # Confirm name not in use
    raise ArgumentError, 'Only one dynamic activity allowed' if !defn.name && hash.key?(defn.name)
    raise ArgumentError, "Multiple activities named #{defn.name}" if hash.key?(defn.name)

    # Confirm executor is a known executor and let it initialize
    executor = worker.options.activity_executors[defn.executor]
    raise ArgumentError, "Unknown executor '#{defn.executor}'" if executor.nil?

    executor.initialize_activity(defn)

    hash[defn.name] = defn
  end

  # Need mutex for the rest of these
  @running_activities_mutex = Mutex.new
  @running_activities = {}
  @running_activities_empty_condvar = ConditionVariable.new
end

Instance Attribute Details

#bridge_worker ⇒ Object (readonly)

Returns the value of attribute bridge_worker.



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

def bridge_worker
  @bridge_worker
end

#worker ⇒ Object (readonly)

Returns the value of attribute worker.



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

def worker
  @worker
end

Instance Method Details

#assert_valid_activity(activity) ⇒ Object

Raises:

  • (ArgumentError)


393
394
395
396
397
398
399
400
401
402
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 393

def assert_valid_activity(activity)
  defn = @activities[activity]
  defn = @activities[nil] if !defn && !Internal::ProtoUtils.reserved_name?(activity)

  return unless defn.nil?

  raise ArgumentError,
        "Activity #{activity} " \
        "is not registered on this worker, available activities: #{@activities.keys.sort.join(', ')}"
end

#execute_activity(task_token, defn, start) ⇒ Object



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

def execute_activity(task_token, defn, start)
  # Build info. Standalone activities have some empty fields on the wire; translate
  # empty strings to nil for the user-facing Info fields.
  workflow_id = Internal::ProtoUtils.string_or(start.workflow_execution&.workflow_id, nil)
  workflow_run_id = Internal::ProtoUtils.string_or(start.workflow_execution&.run_id, nil)
  workflow_type = Internal::ProtoUtils.string_or(start.workflow_type, nil)
  activity_run_id = Internal::ProtoUtils.string_or(start.run_id, nil)
  # `namespace` is always set. `workflow_namespace` is the deprecated accessor, nil for standalone activities.
  namespace = Internal::ProtoUtils.string_or(start.workflow_namespace, @worker.options.client.namespace)
  workflow_namespace = workflow_id.nil? ? nil : namespace
  info = Activity::Info.new(
    activity_id: start.activity_id,
    activity_run_id: activity_run_id,
    activity_type: start.activity_type,
    attempt: start.attempt,
    current_attempt_scheduled_time: Internal::ProtoUtils.timestamp_to_time(
      start.current_attempt_scheduled_time
    ) || raise, # Never nil
    heartbeat_timeout: Internal::ProtoUtils.duration_to_seconds(start.heartbeat_timeout),
    local?: start.is_local,
    namespace: namespace,
    priority: Priority._from_proto(start.priority),
    raw_heartbeat_details: begin
      payloads = start.heartbeat_details.to_ary
      data_converter = @worker.options.client.data_converter
      payloads = ExternalStorageRunner.new(data_converter.external_storage, namespace: nil).retrieve(payloads)
      codec = data_converter.payload_codec
      payloads = codec.decode(payloads) if codec
      payloads.map { |p| Temporalio::Converters::RawValue.new(p) }
    end,
    retry_policy: (RetryPolicy._from_proto(start.retry_policy) if start.retry_policy),
    schedule_to_close_timeout: Internal::ProtoUtils.duration_to_seconds(start.schedule_to_close_timeout),
    scheduled_time: Internal::ProtoUtils.timestamp_to_time(start.scheduled_time) || raise, # Never nil
    start_to_close_timeout: Internal::ProtoUtils.duration_to_seconds(start.start_to_close_timeout),
    started_time: Internal::ProtoUtils.timestamp_to_time(start.started_time) || raise, # Never nil
    task_queue: @worker.options.task_queue,
    task_token:,
    workflow_id: workflow_id,
    workflow_namespace: workflow_namespace,
    workflow_run_id: workflow_run_id,
    workflow_type: workflow_type
  )

  # Build input
  input = Temporalio::Worker::Interceptor::Activity::ExecuteInput.new(
    proc: defn.proc,
    # If the activity wants raw_args, we only decode we don't convert
    args: if defn.raw_args
            payloads = start.input.to_ary
            data_converter = @worker.options.client.data_converter
            payloads = ExternalStorageRunner.new(data_converter.external_storage, namespace: nil)
                                            .retrieve(payloads)
            codec = data_converter.payload_codec
            payloads = codec.decode(payloads) if codec
            payloads.map { |p| Temporalio::Converters::RawValue.new(p) }
          else
            ProtoUtils.convert_from_payload_array(
              @worker.options.client.data_converter,
              start.input.to_ary,
              hints: defn.arg_hints
            )
          end,
    result_hint: defn.result_hint,
    headers: ProtoUtils.headers_from_proto_map(start.header_fields, @worker.options.client.data_converter) || {}
  )

  # Run
  activity = RunningActivity.new(
    worker: @worker,
    info:,
    cancellation: Cancellation.new,
    worker_shutdown_cancellation: @worker._worker_shutdown_cancellation,
    payload_converter: @worker.options.client.data_converter.payload_converter,
    logger: @scoped_logger,
    runtime_metric_meter: @runtime_metric_meter
  )
  Activity::Context._current_executor&.set_activity_context(defn, activity)
  set_running_activity(task_token, activity)
  run_activity(defn, activity, input, storage_data_converter(start))
rescue Exception => e # rubocop:disable Lint/RescueException -- We are intending to catch everything here
  @scoped_logger.warn("Failed starting or sending completion for activity #{start.activity_type}")
  @scoped_logger.warn(e)
  # This means that the activity couldn't start or send completion (run
  # handles its own errors).
  begin
    @bridge_worker.complete_activity_task(
      Bridge::Api::CoreInterface::ActivityTaskCompletion.new(
        task_token:,
        result: failed_result(e, storage_data_converter(start))
      )
    )
  rescue StandardError => e_inner
    @scoped_logger.error("Failed sending failure for activity #{start.activity_type}")
    @scoped_logger.error(e_inner)
  end
ensure
  Activity::Context._current_executor&.set_activity_context(defn, nil)
  remove_running_activity(task_token)
end

#failed_result(error, data_converter) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 357

def failed_result(error, data_converter)
  if error.is_a?(Error::ExternalStorageError)
    Bridge::Api::ActivityResult::ActivityExecutionResult.new(
      failed: Bridge::Api::ActivityResult::Failure.new(
        failure: @data_converter_without_storage.to_failure(error),
        cause: :ACTIVITY_TASK_FAILED_CAUSE_EXTERNAL_STORAGE_FAILURE
      )
    )
  else
    Bridge::Api::ActivityResult::ActivityExecutionResult.new(
      failed: Bridge::Api::ActivityResult::Failure.new(failure: data_converter.to_failure(error))
    )
  end
end

#get_running_activity(task_token) ⇒ Object



67
68
69
70
71
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 67

def get_running_activity(task_token)
  @running_activities_mutex.synchronize do
    @running_activities[task_token]
  end
end

#handle_cancel_task(task_token, cancel) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 149

def handle_cancel_task(task_token, cancel)
  activity = get_running_activity(task_token)
  if activity.nil?
    @scoped_logger.warn("Cannot find activity to cancel for token #{task_token}")
    return
  end
  begin
    activity._cancel(
      reason: cancel.reason.to_s,
      details: Activity::CancellationDetails.new(
        gone_from_server: cancel.details.is_not_found,
        cancel_requested: cancel.details.is_cancelled,
        timed_out: cancel.details.is_timed_out,
        worker_shutdown: cancel.details.is_worker_shutdown,
        paused: cancel.details.is_paused,
        reset: cancel.details.is_reset
      )
    )
  rescue StandardError => e
    @scoped_logger.warn("Failed cancelling activity #{activity.info.activity_type} \
      with ID #{activity.info.activity_id}")
    @scoped_logger.warn(e)
  end
end

#handle_start_task(task_token, start) ⇒ Object



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

def handle_start_task(task_token, start)
  set_running_activity(task_token, nil)

  # Find activity definition, falling back to dynamic if not found and not reserved name
  defn = @activities[start.activity_type]
  defn = @activities[nil] if !defn && !Internal::ProtoUtils.reserved_name?(start.activity_type)

  if defn.nil?
    raise Error::ApplicationError.new(
      "Activity #{start.activity_type} for workflow #{start.workflow_execution.workflow_id} " \
      "is not registered on this worker, available activities: #{@activities.keys.sort.join(', ')}",
      type: 'NotFoundError'
    )
  end

  # Run everything else in the excecutor
  executor = @worker.options.activity_executors[defn.executor]
  executor.execute_activity(defn) do
    # Set current executor
    Activity::Context._current_executor = executor
    # Execute with error handling
    execute_activity(task_token, defn, start)
  ensure
    # Unset at the end
    Activity::Context._current_executor = nil
  end
rescue Exception => e # rubocop:disable Lint/RescueException -- We are intending to catch everything here
  remove_running_activity(task_token)
  @scoped_logger.warn("Failed starting activity #{start.activity_type}")
  @scoped_logger.warn(e)

  # We need to complete the activity task as failed, but this is on the
  # hot path for polling, so we want to complete it in the background
  begin
    @bridge_worker.complete_activity_task_in_background(
      Bridge::Api::CoreInterface::ActivityTaskCompletion.new(
        task_token:,
        result: Bridge::Api::ActivityResult::ActivityExecutionResult.new(
          failed: Bridge::Api::ActivityResult::Failure.new(
            # TODO(cretz): If failure conversion does slow failure
            # encoding, it can gum up the system
            failure: storage_data_converter(start).to_failure(e)
          )
        )
      )
    )
  rescue StandardError => e_inner
    @scoped_logger.error("Failed building start failure to return for #{start.activity_type}")
    @scoped_logger.error(e_inner)
  end
end

#handle_task(task) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 86

def handle_task(task)
  @scoped_logger.debug("Received activity task: #{task}") if LOG_TASKS
  if !task.start.nil?
    handle_start_task(task.task_token, task.start)
  elsif !task.cancel.nil?
    handle_cancel_task(task.task_token, task.cancel)
  else
    raise "Unrecognized activity task: #{task}"
  end
end

#remove_running_activity(task_token) ⇒ Object



73
74
75
76
77
78
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 73

def remove_running_activity(task_token)
  @running_activities_mutex.synchronize do
    @running_activities.delete(task_token)
    @running_activities_empty_condvar.broadcast if @running_activities.empty?
  end
end

#run_activity(defn, activity, input, data_converter) ⇒ Object



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

def run_activity(defn, activity, input, data_converter)
  result = begin
    # Create the instance. We choose to do this before interceptors so that it is available in the interceptor.
    activity.instance = defn.instance.is_a?(Proc) ? defn.instance.call : defn.instance # steep:ignore

    # Build impl with interceptors
    # @type var impl: Temporalio::Worker::Interceptor::Activity::Inbound
    impl = InboundImplementation.new(self)
    impl = @worker._activity_interceptors.reverse_each.reduce(impl) do |acc, int|
      int.intercept_activity(acc)
    end
    impl.init(OutboundImplementation.new(self, activity.info.task_token, data_converter))

    # Execute
    result = impl.execute(input)

    # Success
    Bridge::Api::ActivityResult::ActivityExecutionResult.new(
      completed: Bridge::Api::ActivityResult::Success.new(
        result: data_converter.to_payload(result, hint: input.result_hint)
      )
    )
  rescue Exception => e # rubocop:disable Lint/RescueException -- We are intending to catch everything here
    if e.is_a?(Activity::CompleteAsyncError)
      # Wanting to complete async
      @scoped_logger.debug('Completing activity asynchronously')
      Bridge::Api::ActivityResult::ActivityExecutionResult.new(
        will_complete_async: Bridge::Api::ActivityResult::WillCompleteAsync.new
      )
    elsif e.is_a?(Error::CanceledError) && activity.cancellation_details&.paused?
      # Server requested pause
      @scoped_logger.debug('Completing activity as failed due to exception caused by pause')
      Bridge::Api::ActivityResult::ActivityExecutionResult.new(
        failed: Bridge::Api::ActivityResult::Failure.new(
          failure: data_converter.to_failure(
            Error._with_backtrace_and_cause(
              Error::ApplicationError.new('Activity paused', type: 'ActivityPause'), backtrace: nil, cause: e
            )
          )
        )
      )
    elsif e.is_a?(Error::CanceledError) && activity.cancellation_details&.reset?
      # Server requested reset
      @scoped_logger.debug('Completing activity as failed due to exception caused by reset')
      Bridge::Api::ActivityResult::ActivityExecutionResult.new(
        failed: Bridge::Api::ActivityResult::Failure.new(
          failure: data_converter.to_failure(
            Error._with_backtrace_and_cause(
              Error::ApplicationError.new('Activity reset', type: 'ActivityReset'), backtrace: nil, cause: e
            )
          )
        )
      )
    elsif e.is_a?(Error::CanceledError) && activity._server_requested_cancel
      # Server requested cancel
      @scoped_logger.debug('Completing activity as canceled')
      Bridge::Api::ActivityResult::ActivityExecutionResult.new(
        cancelled: Bridge::Api::ActivityResult::Cancellation.new(
          failure: data_converter.to_failure(e)
        )
      )
    else
      # General failure
      log_level = if e.is_a?(Error::ApplicationError) && e.category == Error::ApplicationError::Category::BENIGN
                    Logger::DEBUG
                  else
                    Logger::WARN
                  end
      @scoped_logger.add(log_level, 'Completing activity as failed')
      @scoped_logger.add(log_level, e)
      failed_result(e, data_converter)
    end
  end

  @scoped_logger.debug("Sending activity completion: #{result}") if LOG_TASKS
  @bridge_worker.complete_activity_task(
    Bridge::Api::CoreInterface::ActivityTaskCompletion.new(
      task_token: activity.info.task_token,
      result:
    )
  )
end

#set_running_activity(task_token, activity) ⇒ Object



61
62
63
64
65
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 61

def set_running_activity(task_token, activity)
  @running_activities_mutex.synchronize do
    @running_activities[task_token] = activity
  end
end

#storage_data_converter(start) ⇒ Object



372
373
374
375
376
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 372

def storage_data_converter(start)
  @worker.options.client.data_converter._with_storage_target(
    namespace: @worker.options.client.namespace, target: storage_target_for_task(start)
  )
end

#storage_target_for_task(start) ⇒ Object



378
379
380
381
382
383
384
385
386
387
388
389
390
391
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 378

def storage_target_for_task(start)
  namespace = ProtoUtils.string_or(start.workflow_namespace, @worker.options.client.namespace)
  scheduling_workflow_id = ProtoUtils.string_or(start.workflow_execution&.workflow_id)
  if scheduling_workflow_id
    Converters::StorageDriverWorkflowInfo.new(
      namespace:, id: scheduling_workflow_id, run_id: ProtoUtils.string_or(start.workflow_execution&.run_id),
      type: ProtoUtils.string_or(start.workflow_type)
    )
  else
    Converters::StorageDriverActivityInfo.new(
      namespace:, id: start.activity_id, run_id: ProtoUtils.string_or(start.run_id), type: start.activity_type
    )
  end
end

#wait_all_complete ⇒ Object



80
81
82
83
84
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 80

def wait_all_complete
  @running_activities_mutex.synchronize do
    @running_activities_empty_condvar.wait(@running_activities_mutex) until @running_activities.empty?
  end
end