Module: Shoryuken::Worker::ClassMethods

Defined in:
lib/shoryuken/worker.rb

Instance Method Summary collapse

Instance Method Details

#auto_visibility_timeout?Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/shoryuken/worker.rb', line 53

def auto_visibility_timeout?
  !!get_shoryuken_options['auto_visibility_timeout']
end

#get_shoryuken_optionsObject

:nodoc:


57
58
59
# File 'lib/shoryuken/worker.rb', line 57

def get_shoryuken_options # :nodoc:
  @shoryuken_options || Shoryuken.default_worker_options
end

#perform_async(body, options = {}) ⇒ Object


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

def perform_async(body, options = {})
  options[:message_attributes] ||= {}
  options[:message_attributes]['shoryuken_class'] = {
    string_value: self.to_s,
    data_type: 'String'
  }

  options[:message_body] = body

  queue = options.delete(:queue) || get_shoryuken_options['queue']

  Shoryuken::Client.queues(queue).send_message(options)
end

#perform_in(interval, body, options = {}) ⇒ Object Also known as: perform_at


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/shoryuken/worker.rb', line 22

def perform_in(interval, body, options = {})
  interval = interval.to_f
  now = Time.now.to_f
  ts = (interval < 1_000_000_000 ? (now + interval).to_f : interval)

  delay = (ts - now).ceil

  raise 'The maximum allowed delay is 15 minutes' if delay > 15 * 60

  perform_async(body, options.merge(delay_seconds: delay))
end

#server_middleware {|@server_chain| ... } ⇒ Object

Yields:

  • (@server_chain)

36
37
38
39
40
# File 'lib/shoryuken/worker.rb', line 36

def server_middleware
  @server_chain ||= Shoryuken.server_middleware.dup
  yield @server_chain if block_given?
  @server_chain
end

#shoryuken_options(opts = {}) ⇒ Object


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

def shoryuken_options(opts = {})
  @shoryuken_options = get_shoryuken_options.merge(stringify_keys(opts || {}))
  queue = @shoryuken_options['queue']
  if queue.respond_to? :call
    queue = queue.call
    @shoryuken_options['queue'] = queue
  end

  Shoryuken.register_worker(queue, self)
end

#stringify_keys(hash) ⇒ Object

:nodoc:


61
62
63
64
65
66
# File 'lib/shoryuken/worker.rb', line 61

def stringify_keys(hash) # :nodoc:
  hash.keys.each do |key|
    hash[key.to_s] = hash.delete(key)
  end
  hash
end