Class: DaemonKit::AMQP

Inherits:
Object show all
Defined in:
lib/daemon_kit/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.



25
26
27
# File 'lib/daemon_kit/amqp.rb', line 25

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

Class Method Details

.instanceObject



14
15
16
# File 'lib/daemon_kit/amqp.rb', line 14

def instance
  @instance ||= new
end

.run(&block) ⇒ Object



20
21
22
# File 'lib/daemon_kit/amqp.rb', line 20

def run(&block)
  instance.run(&block)
end

Instance Method Details

#run(&block) ⇒ Object



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

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