Module: Shoryuken::Util

Instance Method Summary collapse

Instance Method Details

#elapsed(started_at) ⇒ Object


29
30
31
32
# File 'lib/shoryuken/util.rb', line 29

def elapsed(started_at)
  # elapsed in ms
  (Time.now - started_at) * 1000
end

#fire_event(event, reverse = false) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shoryuken/util.rb', line 15

def fire_event(event, reverse = false)
  logger.debug { "Firing '#{event}' lifecycle event" }
  arr = Shoryuken.options[:lifecycle_events][event]
  arr.reverse! if reverse
  arr.each do |block|
    begin
      block.call
    rescue => ex
      logger.warn(event: event)
      logger.warn "#{ex.class.name}: #{ex.message}"
    end
  end
end

#loggerObject


11
12
13
# File 'lib/shoryuken/util.rb', line 11

def logger
  Shoryuken.logger
end

#unparse_queues(queues) ⇒ Object


34
35
36
37
38
# File 'lib/shoryuken/util.rb', line 34

def unparse_queues(queues)
  queues.each_with_object({}) do |name, queue_and_weights|
    queue_and_weights[name] = queue_and_weights[name].to_i + 1
  end.to_a
end

#watchdog(last_words) ⇒ Object


3
4
5
6
7
8
9
# File 'lib/shoryuken/util.rb', line 3

def watchdog(last_words)
  yield
rescue => ex
  logger.error { last_words }
  logger.error { ex }
  logger.error { ex.backtrace.join("\n") }
end

#worker_name(worker_class, sqs_msg, body = nil) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/shoryuken/util.rb', line 40

def worker_name(worker_class, sqs_msg, body = nil)
  if defined?(::ActiveJob) \
      && !sqs_msg.is_a?(Array) \
      && sqs_msg.message_attributes \
      && sqs_msg.message_attributes['shoryuken_class'] \
      && sqs_msg.message_attributes['shoryuken_class'][:string_value] == ActiveJob::QueueAdapters::ShoryukenAdapter::JobWrapper.to_s

    "ActiveJob/#{body['job_class']}"
  else
    worker_class.to_s
  end
end