Sha256: 11cc12cb53b3923b26a8b2e091e19bff838c881f5916767db3130d3f53c91a8f

Contents?: true

Size: 935 Bytes

Versions: 3

Compression:

Stored size: 935 Bytes

Contents

require 'yaml'
require 'amqp'

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 ||= new
      end

      private :new

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

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

    def run(&block)
      # Start our event loop and AMQP client
      DaemonKit.logger.debug("AMQP.start(#{@config.inspect})")
      ::AMQP.start(@config) do |connection|
        # Ensure graceful shutdown of the connection to the broker
        hook = Proc.new { connection.close { EventMachine.stop } }
        DaemonKit.trap('INT', hook)
        DaemonKit.trap('TERM', hook)

        yield connection
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
daemon-kit-0.3.3 lib/daemon_kit/dk_amqp.rb
daemon-kit-0.3.2 lib/daemon_kit/dk_amqp.rb
daemon-kit-0.3.1 lib/daemon_kit/dk_amqp.rb