Sha256: c00d82b41da7b92cc91cc17db5c34fe9a09c1ae72e3efb076cdb59fe7386a876

Contents?: true

Size: 1.04 KB

Versions: 3

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Totoro
  class EnqueueService
    def initialize(connection, config)
      @connection = connection
      @config = config
    end

    def enqueue(id, payload)
      @connection.start unless @connection.connected?
      queue = channel.queue(*@config.queue(id))
      payload = JSON.dump payload
      exchange.publish(payload, routing_key: queue.name)
      Rails.logger.info "send message to #{queue.name}"
      STDOUT.flush
      channel.close
    rescue Bunny::TCPConnectionFailedForAllHosts,
           Bunny::NetworkErrorWrapper,
           Bunny::ChannelAlreadyClosed,
           Bunny::ConnectionAlreadyClosed,
           AMQ::Protocol::EmptyResponseError => error
      @channel.close if @channel.present?
      raise(Totoro::ConnectionBreakError, "type: #{error.class}, message: #{error.message}")
    end

    private

    def channel
      @channel ||= @connection.create_channel
    end

    # default exchange is a direct exchange
    def exchange
      @exchange ||= channel.default_exchange
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
totoro-1.0.2 lib/totoro/services/enqueue_service.rb
totoro-1.0.1 lib/totoro/services/enqueue_service.rb
totoro-1.0.0 lib/totoro/services/enqueue_service.rb