Sha256: d40a2fdcf94aba90f9ad3ce3d9a1148852f541c41018a242c3cb816939139cc5

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module BunnyCarrot
  class RabbitHole
    include BunnyCarrot::Logger

    def self.get_subscribe_channel
      @@subscribe_channel ||= new.get_channel
    end

    def self.get_publish_channel
      @@publish_channel ||= new.get_channel
    end

    def initialize
      @rabbit = Bunny.new(BunnyCarrot.server_url,
                          heartbeat_interval: 5, automatically_recover: true,
                          keepalive:          true)
      @rabbit.start
      logger.info 'Rabbit actor initialized'
    end

    def get_channel
      @rabbit.create_channel
    end

    def self.publish(args)
      queue_name = args.fetch(:queue_name)
      payload    = args.fetch(:payload).to_json
      headers    = args.fetch(:headers, {})
      get_publish_channel.queue(queue_name, durable: true)
      exchange.publish(payload,
                       durable: true,
                       routing_key: queue_name,
                       headers:     headers)
    end

    def self.exchange
      get_publish_channel.default_exchange
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bunny_carrot-0.0.2 lib/bunny_carrot/rabbit_hole.rb