Sha256: 6a110f19fe6b7d51969ee30800304359e305bc053f66b86b87b6a300f83a37dd

Contents?: true

Size: 1019 Bytes

Versions: 5

Compression:

Stored size: 1019 Bytes

Contents

require 'yaml'

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

    @@instance = nil

    class << self

      def instance
        @instance ||= (
          config = YAML.load_file( "#{DAEMON_ROOT}/config/amqp.yml" )[DAEMON_ENV]
          raise ArgumentError, "Missing AMQP configuration for #{DAEMON_ENV} environment" if config.nil?
          new( config )
        )
      end

      private :new

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

    def initialize( config = {} )
      @config = config.inject({}) { |m,c| m[c[0].to_sym] = c[1]; m } # symbolize_keys 
    end

    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
      ::AMQP.start(@config, &block)
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
kennethkalmer-daemon-kit-0.1.1 lib/daemon_kit/amqp.rb
kennethkalmer-daemon-kit-0.1.2 lib/daemon_kit/amqp.rb
kennethkalmer-daemon-kit-0.1.3 lib/daemon_kit/amqp.rb
kennethkalmer-daemon-kit-0.1.4 lib/daemon_kit/amqp.rb
daemon-kit-0.1.3 lib/daemon_kit/amqp.rb