Class: Cadence::Activity::TaskProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/cadence/activity/task_processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(task, domain, activity_lookup, client, middleware_chain) ⇒ TaskProcessor

Returns a new instance of TaskProcessor.



8
9
10
11
12
13
14
15
16
# File 'lib/cadence/activity/task_processor.rb', line 8

def initialize(task, domain, activity_lookup, client, middleware_chain)
  @task = task
  @domain = domain
  @task_token = task.taskToken
  @activity_name = task.activityType.name
  @activity_class = activity_lookup.find(activity_name)
  @client = client
  @middleware_chain = middleware_chain
end

Instance Method Details

#processObject



18
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
# File 'lib/cadence/activity/task_processor.rb', line 18

def process
  start_time = Time.now

  Cadence.logger.info("Processing activity task for #{activity_name}")
  Cadence.metrics.timing('activity_task.queue_time', queue_time_ms, activity: activity_name)

  if !activity_class
    respond_failed('ActivityNotRegistered', 'Activity is not registered with this worker')
    return
  end

   = Metadata.generate(Metadata::ACTIVITY_TYPE, task, domain)
  context = Activity::Context.new(client, )

  result = middleware_chain.invoke() do
    activity_class.execute_in_context(context, JSON.deserialize(task.input))
  end

  # Do not complete asynchronous activities, these should be completed manually
  respond_completed(result) unless context.async?
rescue StandardError, ScriptError => error
  respond_failed(error.class.name, error.message)
rescue Exception => error
  Cadence.logger.fatal("Activity #{activity_name} unexpectedly failed with: #{error.inspect}")
  Cadence.logger.debug(error.backtrace.join("\n"))
  raise
ensure
  time_diff_ms = ((Time.now - start_time) * 1000).round
  Cadence.metrics.timing('activity_task.latency', time_diff_ms, activity: activity_name)
  Cadence.logger.debug("Activity task processed in #{time_diff_ms}ms")
end