Class: Totoro::Queue

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

Class Method Summary collapse

Class Method Details

.channelObject



8
9
10
# File 'lib/totoro/queue.rb', line 8

def channel
  @channel ||= connection.create_channel
end

.connectionObject



4
5
6
# File 'lib/totoro/queue.rb', line 4

def connection
  @connection ||= Bunny.new(Totoro::Config.connect).tap(&:start)
end

.enqueue(id, payload) ⇒ Object

enqueue = publish to direct exchange



17
18
19
20
21
# File 'lib/totoro/queue.rb', line 17

def enqueue(id, payload)
  queue = channel.queue(*Totoro::Config.queue(id))
  payload = JSON.dump payload
  exchange.publish(payload, routing_key: queue.name)
end

.exchangeObject



12
13
14
# File 'lib/totoro/queue.rb', line 12

def exchange
  @exchanges ||= channel.default_exchange
end

.subscribe(id) ⇒ Object



23
24
25
26
27
28
# File 'lib/totoro/queue.rb', line 23

def subscribe(id)
  queue = channel.queue(*Totoro::Config.queue(id))
  queue.subscribe do |delivery_info, , payload|
    yield(delivery_info, , payload)
  end
end