Class: Postman::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/postman/channel.rb

Overview

Configures and wraps a Bunny Channel/Queue

Instance Method Summary collapse

Constructor Details

#initialize(client:, config:, type: :topic) ⇒ Channel

Returns a new instance of Channel


6
7
8
9
10
11
12
13
14
# File 'lib/postman/channel.rb', line 6

def initialize(client:, config:, type: :topic)
  @client = client
  @exchange_name = config[:exchange]
  @queue_name = config[:queue]
  @routing_keys = config[:routing_keys]
  @deadletter_exchange = config[:deadletter_exchange]
  @ttl = config[:ttl]
  @type = type
end

Instance Method Details

#activate!(keys: []) ⇒ Object

Ensures the queues and channels are set up to receive messages keys: additional routing_keys to bind


31
32
33
34
# File 'lib/postman/channel.rb', line 31

def activate!(keys: [])
  establish_bindings!
  keys.each { |key| add_routing_key(key) }
end

#add_routing_key(key) ⇒ Object


36
37
38
# File 'lib/postman/channel.rb', line 36

def add_routing_key(key)
  queue.bind(exchange, routing_key: key)
end

#publish(payload, options) ⇒ Object

Publishes a message to the configured queue


24
25
26
27
# File 'lib/postman/channel.rb', line 24

def publish(payload, options)
  options[:persistent] = true
  exchange.publish(payload, options)
end

#subscribe(consumer_tag, &block) ⇒ Object


18
19
20
21
# File 'lib/postman/channel.rb', line 18

def subscribe(consumer_tag, &block)
  channel.prefetch(10)
  queue.subscribe(manual_ack: true, block: false, consumer_tag: consumer_tag, durable: true, &block)
end