Sha256: 610499b43ec54595c1bfce77f1b1aa6389407d7b024125d704eafc67118661c7

Contents?: true

Size: 981 Bytes

Versions: 4

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

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

    def broadcast(exchange_id, payload)
      @connection.start unless @connection.connected?
      exchange = channel.fanout(@config.exchange(exchange_id))
      payload = JSON.dump payload
      exchange.publish(payload)
      Rails.logger.debug "send message to exchange #{@config.exchange(exchange_id)}"
      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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
totoro-1.0.6 pkg/totoro-1.0.5/lib/totoro/services/broadcast_service.rb
totoro-1.0.5 lib/totoro/services/broadcast_service.rb
totoro-1.0.4 lib/totoro/services/broadcast_service.rb
totoro-1.0.3 lib/totoro/services/broadcast_service.rb