Class: Totoro::BroadcastService

Inherits:
Object
  • Object
show all
Defined in:
lib/totoro/services/broadcast_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, config) ⇒ BroadcastService

Returns a new instance of BroadcastService.



5
6
7
8
# File 'lib/totoro/services/broadcast_service.rb', line 5

def initialize(connection, config)
  @connection = connection
  @config = config
end

Instance Method Details

#broadcast(exchange_id, payload) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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