Sha256: 79fe7f662b0c3efb24d800d85228ada8681a15ba0068de679bc8acc9935b7014

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

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, options(attrs))
      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 options(exchange_id, attrs)
      { persistent: @config.exchange_persistent?(exchange_id) }.merge(attrs)
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
totoro-1.0.7 lib/totoro/services/broadcast_service.rb
totoro-1.0.6 lib/totoro/services/broadcast_service.rb