Sha256: 20fca479f3fadec5681d42ede513642c6b55d6e20d753d39d24fe19ce29afe4a

Contents?: true

Size: 957 Bytes

Versions: 2

Compression:

Stored size: 957 Bytes

Contents

# frozen_string_literal: true

module Totoro
  class BaseQueue
    class <<self
      def config
        @config ||= Totoro::Config.new
      end

      def connection
        @connection ||= Bunny.new(config.connect).tap(&:start)
      end

      def channel
        @channel ||= connection.create_channel
      end

      def exchange
        @exchanges ||= channel.default_exchange
      end

      # enqueue = publish to direct exchange
      def enqueue(id, payload)
        queue = channel.queue(*config.queue(id))
        payload = JSON.dump payload
        exchange.publish(payload, routing_key: queue.name)
      end

      def subscribe(id)
        queue = channel.queue(*config.queue(id))
        queue.subscribe do |delivery_info, metadata, payload|
          yield(delivery_info, metadata, payload)
        end
      end

      def get_worker(worker_class)
        ::Worker.const_get(worker_class.to_s.camelize).new
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
totoro-0.1.5 lib/totoro/base_queue.rb
totoro-0.1.4 lib/totoro/base_queue.rb