Class: BugBunny::Rabbit

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_bunny/rabbit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#blockObject

Returns the value of attribute block.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def block
  @block
end

#confirm_selectObject

Returns the value of attribute confirm_select.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def confirm_select
  @confirm_select
end

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def connection
  @connection
end

#exchangeObject

Returns the value of attribute exchange.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def exchange
  @exchange
end

#identifierObject

Returns the value of attribute identifier.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def identifier
  @identifier
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def logger
  @logger
end

#no_ackObject

Returns the value of attribute no_ack.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def no_ack
  @no_ack
end

#persistentObject

Returns the value of attribute persistent.



5
6
7
# File 'lib/bug_bunny/rabbit.rb', line 5

def persistent
  @persistent
end

#rabbit_channelObject

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

#channelObject



36
37
38
# File 'lib/bug_bunny/rabbit.rb', line 36

def channel
  open? ? @rabbit_channel : set_channel
end

#closeObject



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_channelObject



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

Returns:

  • (Boolean)


64
65
66
# File 'lib/bug_bunny/rabbit.rb', line 64

def connection_openned?
  [:open, :connecting, :connected].include?(connection.status)
end

#create_connectionObject

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
  options = {}

  # 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
  options.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']}",
                          options)
  rabbit_conn.start
  logger&.debug("New status connection: #{rabbit_conn.status}")

  self.connection = rabbit_conn
end

#open?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/bug_bunny/rabbit.rb', line 59

def open?
  (connection.status == :open) &&
    (@rabbit_channel.status == :open)
end

#set_channelObject



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

#statusObject



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