Class: Shoryuken::DefaultWorkerRegistry

Inherits:
WorkerRegistry show all
Defined in:
lib/shoryuken/default_worker_registry.rb

Instance Method Summary collapse

Constructor Details

#initializeDefaultWorkerRegistry

Returns a new instance of DefaultWorkerRegistry


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

def initialize
  @workers = {}
end

Instance Method Details

#batch_receive_messages?(queue) ⇒ Boolean

Returns:

  • (Boolean)

7
8
9
# File 'lib/shoryuken/default_worker_registry.rb', line 7

def batch_receive_messages?(queue)
  !!(@workers[queue] && @workers[queue].get_shoryuken_options['batch'])
end

#clearObject


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

def clear
  @workers.clear
end

#fetch_worker(queue, message) ⇒ Object


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

def fetch_worker(queue, message)
  worker_class = !message.is_a?(Array) &&
    message.message_attributes &&
    message.message_attributes['shoryuken_class'] &&
    message.message_attributes['shoryuken_class'][:string_value]

  worker_class = (worker_class.constantize rescue nil) || @workers[queue]

  worker_class.new
end

#queuesObject


26
27
28
# File 'lib/shoryuken/default_worker_registry.rb', line 26

def queues
  @workers.keys
end

#register_worker(queue, clazz) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/shoryuken/default_worker_registry.rb', line 30

def register_worker(queue, clazz)
  if (worker_class = @workers[queue])
    if worker_class.get_shoryuken_options['batch'] == true || clazz.get_shoryuken_options['batch'] == true
      raise ArgumentError, "Could not register #{clazz} for '#{queue}', "\
        "because #{worker_class} is already registered for this queue, "\
        "and Shoryuken doesn't support a batchable worker for a queue with multiple workers."
    end
  end

  @workers[queue] = clazz
end

#workers(queue) ⇒ Object


42
43
44
# File 'lib/shoryuken/default_worker_registry.rb', line 42

def workers(queue)
  [@workers.fetch(queue, [])].flatten
end