PageRenderTime 28ms CodeModel.GetById 22ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/daemon_kit/lib/daemon_kit/amqp.rb

http://github.com/mperham/qanat
Ruby | 38 lines | 24 code | 9 blank | 5 comment | 0 complexity | 20d2a908dd4e2810a2f1be4683ba51d0 MD5 | raw file
 1require 'yaml'
 2
 3module DaemonKit
 4  # Thin wrapper around the amqp gem, specifically designed to ease
 5  # configuration of a AMQP consumer daemon and provide some added
 6  # simplicity
 7  class AMQP
 8
 9    @@instance = nil
10
11    class << self
12
13      def instance
14        @instance ||= new
15      end
16
17      private :new
18
19      def run(&block)
20        instance.run(&block)
21      end
22    end
23
24    def initialize( config = {} )
25      @config = DaemonKit::Config.load('amqp').to_h( true )
26    end
27
28    def run(&block)
29      # Ensure graceful shutdown of the connection to the broker
30      DaemonKit.trap('INT') { ::AMQP.stop { ::EM.stop } }
31      DaemonKit.trap('TERM') { ::AMQP.stop { ::EM.stop } }
32
33      # Start our event loop and AMQP client
34      DaemonKit.logger.debug("AMQP.start(#{@config.inspect})")
35      ::AMQP.start(@config, &block)
36    end
37  end
38end