class Totoro::BroadcastService

Public Class Methods

new(connection, config) click to toggle source
# File lib/totoro/services/broadcast_service.rb, line 5
def initialize(connection, config)
  @connection = connection
  @config = config
end

Public Instance Methods

broadcast(exchange_id, payload) click to toggle source
# File lib/totoro/services/broadcast_service.rb, line 10
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 Instance Methods

channel() click to toggle source
# File lib/totoro/services/broadcast_service.rb, line 33
def channel
  @channel ||= @connection.create_channel
end
options(exchange_id, attrs) click to toggle source
# File lib/totoro/services/broadcast_service.rb, line 29
def options(exchange_id, attrs)
  { persistent: @config.exchange_persistent?(exchange_id) }.merge(attrs)
end