Class: Totoro::EnqueueService

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

Instance Method Summary collapse

Constructor Details

#initialize(connection, config) ⇒ EnqueueService

Returns a new instance of EnqueueService.



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

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

Instance Method Details

#enqueue(id, payload, attrs = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/totoro/services/enqueue_service.rb', line 10

def enqueue(id, payload, attrs = {})
  @connection.start unless @connection.connected?
  queue = channel.queue(*@config.queue(id))
  payload = JSON.dump payload
  exchange.publish(payload, options(id, queue.name, attrs))
  Rails.logger.debug "send message to #{queue.name}"
  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