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
43 44 45 46 |
# File 'lib/bbk/amqp/utils.rb', line 43 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
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 |
# File 'lib/bbk/amqp/utils.rb', line 66 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 |
# File 'lib/bbk/amqp/utils.rb', line 15 def self.pop(queue, timeout = 10) unblocker = Queue.new consumer = queue.subscribe(block: false) do |delivery_info, , payload| = [ delivery_info, .to_hash.with_indifferent_access, begin Oj.load(payload).with_indifferent_access rescue StandardError payload end ] unblocker << end Thread.new do sleep timeout unblocker << :timeout end result = unblocker.pop consumer.cancel raise ::Timeout::Error if result == :timeout result end |