Class: BugBunny::Rabbit
- Inherits:
-
Object
- Object
- BugBunny::Rabbit
- Defined in:
- lib/bug_bunny/rabbit.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#confirm_select ⇒ Object
Returns the value of attribute confirm_select.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#exchange ⇒ Object
Returns the value of attribute exchange.
-
#identifier ⇒ Object
Returns the value of attribute identifier.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#no_ack ⇒ Object
Returns the value of attribute no_ack.
-
#persistent ⇒ Object
Returns the value of attribute persistent.
-
#rabbit_channel ⇒ Object
Returns the value of attribute rabbit_channel.
Instance Method Summary collapse
- #channel ⇒ Object
- #close ⇒ Object
- #close_channel ⇒ Object
- #connection_openned? ⇒ Boolean
-
#create_connection ⇒ Object
status = :open, :connected, :connecting, :closing, :disconnected, :not_connected, :closed.
-
#initialize(attrs = {}) ⇒ Rabbit
constructor
A new instance of Rabbit.
- #open? ⇒ Boolean
- #set_channel ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Rabbit
Returns a new instance of Rabbit.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/bug_bunny/rabbit.rb', line 15 def initialize(attrs = {}) @block = attrs[:block] || true @no_ack = attrs[:no_ack] || true @persistent = attrs[:persistent] || true @confirm_select = attrs[:confirm_select] || true @logger = attrs[:logger] || Logger.new('./log/bug_rabbit.log', 'monthly') @identifier = SecureRandom.uuid create_connection set_channel end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def block @block end |
#confirm_select ⇒ Object
Returns the value of attribute confirm_select.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def confirm_select @confirm_select end |
#connection ⇒ Object
Returns the value of attribute connection.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def connection @connection end |
#exchange ⇒ Object
Returns the value of attribute exchange.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def exchange @exchange end |
#identifier ⇒ Object
Returns the value of attribute identifier.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def identifier @identifier end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def logger @logger end |
#no_ack ⇒ Object
Returns the value of attribute no_ack.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def no_ack @no_ack end |
#persistent ⇒ Object
Returns the value of attribute persistent.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def persistent @persistent end |
#rabbit_channel ⇒ Object
Returns the value of attribute rabbit_channel.
5 6 7 |
# File 'lib/bug_bunny/rabbit.rb', line 5 def rabbit_channel @rabbit_channel end |
Instance Method Details
#channel ⇒ Object
36 37 38 |
# File 'lib/bug_bunny/rabbit.rb', line 36 def channel open? ? @rabbit_channel : set_channel end |
#close ⇒ Object
40 41 42 43 44 45 |
# File 'lib/bug_bunny/rabbit.rb', line 40 def close @rabbit_channel.close if defined?(@rabbit_channel) connection.close if connection.present? rescue Bunny::ChannelAlreadyClosed nil end |
#close_channel ⇒ Object
47 48 49 |
# File 'lib/bug_bunny/rabbit.rb', line 47 def close_channel @rabbit_channel.close if defined?(@rabbit_channel) end |
#connection_openned? ⇒ Boolean
64 65 66 |
# File 'lib/bug_bunny/rabbit.rb', line 64 def connection_openned? [:open, :connecting, :connected].include?(connection.status) end |
#create_connection ⇒ Object
status = :open, :connected, :connecting, :closing, :disconnected, :not_connected, :closed
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/bug_bunny/rabbit.rb', line 70 def create_connection = {} # if WisproUtils::Config.defaults.use_tls # path = (Rails.root.join('private', 'certs') rescue './private/certs') # options.merge!(tls: true, # port: ENV['RABBIT_SSL_PORT'] || 5671, # log_level: ENV['LOG_LEVEL'] || :debug, # verify_peer: true, # tls_cert: "#{path}/cert.pem", # tls_key: "#{path}/key.pem", # tls_ca_certificates: ["#{path}/ca.pem"]) # end logger&.debug('Stablish new connection to rabbit') logger&.debug("amqp://#{ENV['RABBIT_USER']}:" \ "#{ENV['RABBIT_PASS']}@#{ENV['RABBIT_HOST']}" \ "/#{ENV['RABBIT_VIRTUAL_HOST']}") bunny_logger = ::Logger.new('./log/bunny.log', 7, 10485760) bunny_logger.level = ::Logger::DEBUG .merge!( heartbeat_interval: 20, # 20.seconds per connection logger: bunny_logger, # Override bunny client_propierties client_properties: { product: identifier, platform: ''} ) rabbit_conn = Bunny.new("amqp://#{ENV['RABBIT_USER']}" \ ":#{ENV['RABBIT_PASS']}@"\ "#{ENV['RABBIT_HOST']}/"\ "#{ENV['RABBIT_VIRTUAL_HOST']}", ) rabbit_conn.start logger&.debug("New status connection: #{rabbit_conn.status}") self.connection = rabbit_conn end |
#open? ⇒ Boolean
59 60 61 62 |
# File 'lib/bug_bunny/rabbit.rb', line 59 def open? (connection.status == :open) && (@rabbit_channel.status == :open) end |
#set_channel ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/bug_bunny/rabbit.rb', line 27 def set_channel logger.debug("Set Channel: #{connection.status}") if logger try(:close_channel) @rabbit_channel = connection.create_channel @exchange = channel.default_exchange channel.confirm_select if confirm_select @rabbit_channel end |
#status ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/bug_bunny/rabbit.rb', line 51 def status { connection: connection.status, channel: @rabbit_channel.status, identifier: identifier } end |