Module: Shoryuken::Util

Instance Method Summary collapse

Instance Method Details

#elapsed(started_at) ⇒ Object


21
22
23
24
# File 'lib/shoryuken/util.rb', line 21

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

#fire_event(event, reverse = false, event_options = {}) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/shoryuken/util.rb', line 7

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

#loggerObject


3
4
5
# File 'lib/shoryuken/util.rb', line 3

def logger
  Shoryuken.logger
end

#unparse_queues(queues) ⇒ Object


26
27
28
29
30
# File 'lib/shoryuken/util.rb', line 26

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

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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/shoryuken/util.rb', line 32

def worker_name(worker_class, sqs_msg, body = nil)
  if Shoryuken.active_job? \
      && !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 \
      && body

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