Sha256: ff1a2854abab46ee9e9503302bffeab6e6fae69c0f96f30f2622c1beda00a955

Contents?: true

Size: 980 Bytes

Versions: 3

Compression:

Stored size: 980 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.info "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

3 entries across 3 versions & 1 rubygems

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