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.



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

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

  # 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_workerObject (readonly)

Returns the value of attribute bridge_worker.



20
21
22
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 20

def bridge_worker
  @bridge_worker
end

#workerObject (readonly)

Returns the value of attribute worker.



20
21
22
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 20

def worker
  @worker
end

Instance Method Details

#execute_activity(task_token, defn, start) ⇒ Object



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

def execute_activity(task_token, defn, start)
  # Build info
  info = Activity::Info.new(
    activity_id: start.activity_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_details: ProtoUtils.convert_from_payload_array(
      @worker.options.client.data_converter,
      start.heartbeat_details.to_ary
    ),
    heartbeat_timeout: Internal::ProtoUtils.duration_to_seconds(start.heartbeat_timeout),
    local?: start.is_local,
    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: start.workflow_execution.workflow_id,
    workflow_namespace: start.workflow_namespace,
    workflow_run_id: start.workflow_execution.run_id,
    workflow_type: start.workflow_type
  ).freeze

  # 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
            codec = @worker.options.client.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)
          end,
    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)
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: Bridge::Api::ActivityResult::ActivityExecutionResult.new(
          failed: Bridge::Api::ActivityResult::Failure.new(
            failure: @worker.options.client.data_converter.to_failure(e)
          )
        )
      )
    )
  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

#get_running_activity(task_token) ⇒ Object



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

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

#handle_cancel_task(task_token, cancel) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 145

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
  activity._server_requested_cancel = true
  _, cancel_proc = activity.cancellation
  begin
    cancel_proc.call(reason: cancel.reason.to_s)
  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



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

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: @worker.options.client.data_converter.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



82
83
84
85
86
87
88
89
90
91
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 82

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



69
70
71
72
73
74
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 69

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) ⇒ Object



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

def run_activity(defn, activity, input)
  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))

    # Execute
    result = impl.execute(input)

    # Success
    Bridge::Api::ActivityResult::ActivityExecutionResult.new(
      completed: Bridge::Api::ActivityResult::Success.new(
        result: @worker.options.client.data_converter.to_payload(result)
      )
    )
  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._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: @worker.options.client.data_converter.to_failure(e)
        )
      )
    else
      # General failure
      @scoped_logger.warn('Completing activity as failed')
      @scoped_logger.warn(e)
      Bridge::Api::ActivityResult::ActivityExecutionResult.new(
        failed: Bridge::Api::ActivityResult::Failure.new(
          failure: @worker.options.client.data_converter.to_failure(e)
        )
      )
    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



57
58
59
60
61
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 57

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

#wait_all_completeObject



76
77
78
79
80
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 76

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