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

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

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.



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

def initialize(worker, bridge_worker)
  @worker = worker
  @bridge_worker = bridge_worker

  # 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, 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.from_activity(act)
    # Confirm name not in use
    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.



17
18
19
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 17

def bridge_worker
  @bridge_worker
end

#workerObject (readonly)

Returns the value of attribute worker.



17
18
19
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 17

def worker
  @worker
end

Instance Method Details

#execute_activity(task_token, defn, start) ⇒ Object



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

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: start.current_attempt_scheduled_time.to_time,
    heartbeat_details: ProtoUtils.convert_from_payload_array(
      @worker.options.client.data_converter,
      start.heartbeat_details.to_ary
    ),
    heartbeat_timeout: start.heartbeat_timeout&.to_f,
    local?: start.is_local,
    schedule_to_close_timeout: start.schedule_to_close_timeout&.to_f,
    scheduled_time: start.scheduled_time.to_time,
    start_to_close_timeout: start.start_to_close_timeout&.to_f,
    started_time: start.started_time.to_time,
    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::ExecuteActivityInput.new(
    proc: defn.proc,
    args: ProtoUtils.convert_from_payload_array(
      @worker.options.client.data_converter,
      start.input.to_ary
    ),
    headers: start.header_fields
  )

  # Run
  activity = RunningActivity.new(
    info:,
    cancellation: Cancellation.new,
    worker_shutdown_cancellation: @worker._worker_shutdown_cancellation,
    payload_converter: @worker.options.client.data_converter.payload_converter,
    logger: @scoped_logger
  )
  Activity::Context._current_executor&.set_activity_context(defn, activity)
  set_running_activity(task_token, activity)
  run_activity(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



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

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

#handle_cancel_task(task_token, cancel) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 138

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



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

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

  # Find activity definition
  defn = @activities[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



77
78
79
80
81
82
83
84
85
86
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 77

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



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

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(activity, input) ⇒ Object



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

def run_activity(activity, input)
  result = begin
    # Build impl with interceptors
    # @type var impl: Temporalio::Worker::Interceptor::ActivityInbound
    impl = InboundImplementation.new(self)
    impl = @worker._all_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



52
53
54
55
56
# File 'lib/temporalio/internal/worker/activity_worker.rb', line 52

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

#wait_all_completeObject



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

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