Class: Bunnicula::AMQP

Inherits:
Object show all
Defined in:
lib/bunnicula/amqp.rb

Overview

Thin wrapper around the amqp gem, specifically designed to ease configuration of a AMQP consumer daemon and provide some added simplicity

Constant Summary collapse

@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ AMQP

Returns a new instance of AMQP.



24
25
26
# File 'lib/bunnicula/amqp.rb', line 24

def initialize( config = {} )
  @config = config || DaemonKit::Config.load('amqp').to_h( true )
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/bunnicula/amqp.rb', line 12

def instance
  @instance ||= new
end

.run(config = nil, &block) ⇒ Object



18
19
20
21
# File 'lib/bunnicula/amqp.rb', line 18

def run(config=nil,&block)
  @instance = new(config) if config
  instance.run(&block)
end

Instance Method Details

#run(&block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/bunnicula/amqp.rb', line 28

def run(&block)
  # Ensure graceful shutdown of the connection to the broker
  DaemonKit.trap('INT') { ::AMQP.stop { ::EM.stop } }
  DaemonKit.trap('TERM') { ::AMQP.stop { ::EM.stop } }

  # Start our event loop and AMQP client
  DaemonKit.logger.debug("AMQP.start(#{@config.inspect})")
  ::AMQP.start(@config, &block)
end