lib/kanina/subscription.rb in kanina-0.6.1 vs lib/kanina/subscription.rb in kanina-0.6.2

- old
+ new

@@ -28,16 +28,16 @@ # Begins subscribing to the specified queue (or binds to an exchange and # sets up an anonymous queue). The block is called for every message # received, and data is passed in as a plain Ruby hash. # @param queue [String] Optional, the queue to watch. # @param bind [String] Optional, the exchange to bind to. - # @param durable [Boolean] Optional, whether to make the queue durable. + # @param durable [Boolean] Optional, whether to make the queue and exchange durable. # @yieldparam data [HashWithIndifferentAccess] The payload of the message, # automatically turned into a hash. def subscribe(queue:'', bind:nil, durable:false, &blk) create_queue(queue, durable: durable) - create_binding(bind) + create_binding(bind, durable: durable) @queue.subscribe do |delivery_info, properties, body| yield format_data(body) end end @@ -51,21 +51,22 @@ @queue = channel.queue(name, durable: durable) end # Ensures the named exchange exists, and binds the queue to it. # @param name [String] The name of the exchange to bind the queue to. - def create_binding(name) + # @param durable [Boolean] Optional, whether to make the exchange durable. + def create_binding(name, durable: false) if name.present? - ensure_exchange_exists(name) + ensure_exchange_exists(name, durable: durable) @queue.bind(name) end end private - def ensure_exchange_exists(bind) + def ensure_exchange_exists(bind, durable: false) unless Kanina::Server.connection.exchange_exists?(bind) - channel.exchange(bind, type: :direct) + channel.exchange(bind, type: :direct, durable: durable) end end def format_data(body) obj = JSON.parse(body)