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 = Concurrent::Hash.new
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
25
26
27
28
# 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 = begin
                   worker_class.constantize
                 rescue
                   @workers[queue]
                 end

  worker_class.new if worker_class
end

#queuesObject


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

def queues
  @workers.keys
end

#register_worker(queue, clazz) ⇒ Object


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

def register_worker(queue, clazz)
  if (worker_class = @workers[queue])
    if worker_class.get_shoryuken_options['batch'] == true || clazz.get_shoryuken_options['batch'] == true
      fail 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


46
47
48
# File 'lib/shoryuken/default_worker_registry.rb', line 46

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