Class: SimpleRabbit::ConsumerWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_rabbit/consumer_worker.rb

Class Method Summary collapse

Class Method Details

.run(queue_name, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simple_rabbit/consumer_worker.rb', line 5

def self.run(queue_name, &block)
  raise ConnectionNotEstablished unless $simplerabbit_connection
  queue = $simplerabbit_channel.queue(queue_name, durable: true)
  begin
    puts " [*] Waiting for messages. To exit press CTRL+C"
    queue.subscribe(block: true, manual_ack: true) do |delivery_info, properties, body|
      begin
        block.call(body)
        $simplerabbit_channel.ack(delivery_info.delivery_tag)
      rescue => e
        puts "\n\n#{e.message}\n\n#{e.backtrace.join("\n")}\n\n"
      end
    end
  rescue Interrupt => _
    exit(0)
  end
end