Module: BBK::AMQP::Utils
- Defined in:
- lib/bbk/amqp/utils.rb
Class Method Summary collapse
-
.commonname(cert_path) ⇒ String
Extract CN certificate attribute from certificate path.
-
.create_connection(options = {}) ⇒ Bunny::Session
Set default options and create non started connection to amqp.
-
.pop(queue, timeout = 10) ⇒ Array
Try get message from amqp queue.
Class Method Details
.commonname(cert_path) ⇒ String
Extract CN certificate attribute from certificate path
60 61 62 63 |
# File 'lib/bbk/amqp/utils.rb', line 60 def self.commonname(cert_path) cert = OpenSSL::X509::Certificate.new(File.read(cert_path)) cert.subject.to_a.find {|name, _, _| name == 'CN' }[1] end |
.create_connection(options = {}) ⇒ Bunny::Session
Set default options and create non started connection to amqp
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bbk/amqp/utils.rb', line 83 def self.create_connection( = {}) hosts = [[:hosts] || [:host] || [:hostname]].flatten.select(&:present?).uniq hosts = hosts.map{|h| h.split(/[;|]/) }.flatten.select(&:present?).uniq [:hosts] = if hosts.empty? [ENV.fetch('MQ_HOST', 'mq')].split(/[;|]/).flatten.select(&:present?).uniq else hosts end [:port] ||= ENV['MQ_PORT'] || 5671 [:vhost] ||= ENV['MQ_VHOST'] || '/' user = [:username] || [:user] || ENV['MQ_USER'] [:username] = [:user] = user # Передаем пустую строку чтобы bunny не использовал пароль по умолчанию guest pwd = [:password] || [:pass] || [:pwd] || ENV['MQ_PASS'] || '' [:password] = [:pass] = [:pwd] = pwd [:tls] = .fetch(:tls, true) [:tls_cert] ||= 'config/keys/cert.pem' [:tls_key] ||= 'config/keys/key.pem' [:tls_ca_certificates] ||= ['config/keys/cacert.pem'] [:verify] = .fetch(:verify, .fetch(:verify_peer, .fetch(:verify_ssl, nil))) [:verify] = true if [:verify] [:verify_peer] = [:verify] [:verify_ssl] = [:verify] [:auth_mechanism] ||= if [:tls] 'EXTERNAL' else 'PLAIN' end [:automatically_recover] ||= false [:automatic_recovery] ||= false [:recovery_attempts] ||= 0 [:recover_attempts] = [:recovery_attempts] [:recover_from_connection_close] ||= false Bunny.new() end |
.pop(queue, timeout = 10) ⇒ Array
Try get message from amqp queue
15 16 17 18 19 20 21 22 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 48 49 50 51 52 53 54 55 |
# File 'lib/bbk/amqp/utils.rb', line 15 def self.pop(queue, timeout = 10) unblocker = Queue.new consumed = false mx = Mutex.new # Если сообщений несколько то порядок может поменяться и это нужно иметь в виду. # Решить можно создав, отдельный канал с qos 1. consumer = queue.subscribe(block: false, manual_ack: true) do |delivery_info, , payload| mx.synchronize do if consumed queue.channel.nack(delivery_info.delivery_tag, false, true) sleep 0.1 break end consumed = true = [ delivery_info, .to_hash.with_indifferent_access, begin Oj.load(payload).with_indifferent_access rescue StandardError payload end ] unblocker << end end Thread.new do sleep timeout unblocker << :timeout end result = unblocker.pop if result == :timeout consumed = true consumer.cancel raise ::Timeout::Error end queue.channel.ack(result[0].delivery_tag) sleep 0.2 consumer.cancel result end |