Sha256: 4dfee4d934ea843c4d43f19b42f22991f7b43937d59a05e23c2e4522d80e55f0

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Kanina
  # <tt>Kanina::Server</tt> loads configuration, opens a connection to RabbitMQ
  # and opens a channel so messages can be received and sent. This class is
  # automatically called and handled by a Railtie, so you shouldn't have to
  # invoke it by hand.
  class Server
    class << self
      include Kanina::Logger

      attr_reader :connection, :channel
      attr_accessor :config, :loud

      def status
        @status || 'off'
      end

      def start
        set_status 'starting'
        load_config unless @config.present?
        open_connection
        open_channel
        set_status 'started'
      end

      def stop
        set_status 'stopping'
        cleanup
      end

      private

      def set_status(string)
        @status = string
        say "Status changed to #{@status}"
      end

      def cleanup
        @connection.try(:close)
        set_status 'off'
      end

      def config_file_location
        Rails.root + 'config/amqp.yml'
      end

      def load_config
        if Pathname.new(config_file_location).exist?
          @config = HashWithIndifferentAccess.new(
            YAML.load_file(config_file_location)
          )[Rails.env]
          say 'Loaded config.'
        else
          say 'amqp.yml not found! Not loading any configuration'
        end
      end

      def open_connection
        @connection ||= Bunny.new(@config)
        if @connection.start
          say "Opened connection: #{@connection}"
        end
      end

      def open_channel
        if @connection.present?
          @channel = @connection.create_channel
          say "Opened channel: #{@channel}"
        else
          fail 'Connection needs to be opened before opening a channel.'
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kanina-0.6.0 lib/kanina/server.rb