Class: Isimud::BunnyClient
Constant Summary
- DEFAULT_URL =
'amqp://guest:guest@localhost'
- CHANNEL_KEY =
isimud.bunny_client.channel
Instance Attribute Summary (collapse)
-
- (Object) url
readonly
Returns the value of attribute url.
Instance Method Summary (collapse)
- - (Object) bind(queue_name, exchange_name, *routing_keys, &block)
- - (Object) channel
- - (Object) close
- - (Boolean) connected?
- - (Object) connection (also: #connect)
- - (Object) create_queue(queue_name, exchange_name, options = {}, &block)
- - (Object) delete_queue(queue_name)
- - (Object) exception_handler(&block)
-
- (BunnyClient) initialize(_url = nil, _bunny_options = {})
constructor
A new instance of BunnyClient.
- - (Object) publish(exchange, routing_key, payload)
- - (Object) reconnect
- - (Object) reset
Methods included from Logging
Constructor Details
- (BunnyClient) initialize(_url = nil, _bunny_options = {})
Returns a new instance of BunnyClient
10 11 12 13 14 |
# File 'lib/isimud/bunny_client.rb', line 10 def initialize(_url = nil, = {}) log "Isimud::BunnyClient.initialize: options = #{.inspect}" @url = _url || DEFAULT_URL @bunny_options = end |
Instance Attribute Details
- (Object) url (readonly)
Returns the value of attribute url
8 9 10 |
# File 'lib/isimud/bunny_client.rb', line 8 def url @url end |
Instance Method Details
- (Object) bind(queue_name, exchange_name, *routing_keys, &block)
16 17 18 19 20 21 |
# File 'lib/isimud/bunny_client.rb', line 16 def bind(queue_name, exchange_name, *routing_keys, &block) create_queue(queue_name, exchange_name, queue_options: {durable: true}, routing_keys: routing_keys, subscribe_options: {manual_ack: true}, &block) end |
- (Object) channel
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/isimud/bunny_client.rb', line 61 def channel if (channel = Thread.current[CHANNEL_KEY]).try(:open?) channel else new_channel = connection.channel new_channel.confirm_select new_channel.prefetch(Isimud.prefetch_count) if Isimud.prefetch_count Thread.current[CHANNEL_KEY] = new_channel end end |
- (Object) close
86 87 88 89 90 |
# File 'lib/isimud/bunny_client.rb', line 86 def close connection.close ensure @connection = nil end |
- (Boolean) connected?
82 83 84 |
# File 'lib/isimud/bunny_client.rb', line 82 def connected? @connection && @connection.open? end |
- (Object) connection Also known as: connect
53 54 55 |
# File 'lib/isimud/bunny_client.rb', line 53 def connection @connection ||= ::Bunny.new(url, @bunny_options).tap(&:start) end |
- (Object) create_queue(queue_name, exchange_name, options = {}, &block)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/isimud/bunny_client.rb', line 23 def create_queue(queue_name, exchange_name, = {}, &block) = [:queue_options] || {} routing_keys = [:routing_keys] || [] = [:subscribe_options] || {} log "Isimud: create_queue #{queue_name}: queue_options=#{.inspect} routing_keys=#{routing_keys.join(',')} subscribe_options=#{.inspect}" current_channel = channel queue = current_channel.queue(queue_name, ) routing_keys.each { |key| queue.bind(exchange_name, routing_key: key, nowait: false) } queue.subscribe() do |delivery_info, properties, payload| begin log "Isimud: queue #{queue_name} received #{delivery_info.delivery_tag} routing_key: #{delivery_info.routing_key}" Thread.current['isimud_queue_name'] = queue_name Thread.current['isimud_delivery_info'] = delivery_info Thread.current['isimud_properties'] = properties block.call(payload) log "Isimud: queue #{queue_name} finished with #{delivery_info.delivery_tag}, acknowledging" current_channel.ack(delivery_info.delivery_tag) rescue => e log("Isimud: queue #{queue_name} error processing #{delivery_info.delivery_tag} payload #{payload.inspect}: #{e.class.name} #{e.}\n #{e.backtrace.join("\n ")}", :warn) current_channel.reject(delivery_info.delivery_tag, Isimud.retry_failures) raise end end queue end |
- (Object) delete_queue(queue_name)
49 50 51 |
# File 'lib/isimud/bunny_client.rb', line 49 def delete_queue(queue_name) channel.queue(queue_name).delete end |
- (Object) exception_handler(&block)
76 77 78 79 80 |
# File 'lib/isimud/bunny_client.rb', line 76 def exception_handler(&block) channel.on_uncaught_exception do yield end end |
- (Object) publish(exchange, routing_key, payload)
92 93 94 |
# File 'lib/isimud/bunny_client.rb', line 92 def publish(exchange, routing_key, payload) channel.topic(exchange, durable: true).publish(payload, routing_key: routing_key, persistent: true) end |
- (Object) reconnect
96 97 98 99 |
# File 'lib/isimud/bunny_client.rb', line 96 def reconnect close connect end |
- (Object) reset
72 73 74 |
# File 'lib/isimud/bunny_client.rb', line 72 def reset connection.close_all_channels end |