class RubyRabbitmqJanus::Rabbit::Connect

@author VAILLANT Jeremy <jeremy.vaillant@dazzl.tv>

Class for manage connection with RabbitMQ

Public Class Methods

new() click to toggle source

Initialize connection to server RabbitMQ

# File lib/rrj/rabbit/connect.rb, line 12
def initialize
  @rabbit = Bunny.new(bunny_conf)
end

Public Instance Methods

channel() click to toggle source

Create an channel

# File lib/rrj/rabbit/connect.rb, line 58
def channel
  @rabbit.create_channel
end
close() click to toggle source

Close connection to server RabbitMQ

# File lib/rrj/rabbit/connect.rb, line 53
def close
  @rabbit.close
end
start() click to toggle source

Opening a connection with RabbitMQ

# File lib/rrj/rabbit/connect.rb, line 48
def start
  @rabbit.start
end
transaction_long() { || ... } click to toggle source

Create an transaction with rabbitmq and not close

# File lib/rrj/rabbit/connect.rb, line 34
def transaction_long
  raise RubyRabbitmqJanus::Errors::Connect::MissingAction \
    unless block_given?

  Timeout.timeout(60) do
    start
    yield
  end
rescue Timeout::Error
  raise RubyRabbitmqJanus::Errors::Connect::TransactionTimeout, \
        'The "Long transaction" have raised Timeout exception.'
end
transaction_short(&block) click to toggle source

Create an transaction with rabbitmq and close after response is received

# File lib/rrj/rabbit/connect.rb, line 17
def transaction_short(&block)
  raise RubyRabbitmqJanus::Errors::Connect::MissingAction unless block

  response = nil

  Timeout.timeout(5) do
    response = transaction_long(&block)
  end
rescue Timeout::Error
  raise RubyRabbitmqJanus::Errors::Connect::TransactionTimeout, \
        'The "Short transaction" have raised Timeout exception.'
ensure
  close
  response
end

Private Instance Methods

bunny_conf() click to toggle source
# File lib/rrj/rabbit/connect.rb, line 64
def bunny_conf
  Tools::Config.instance.server_settings.merge(bunny_conf_static)
end
bunny_conf_static() click to toggle source
# File lib/rrj/rabbit/connect.rb, line 68
def bunny_conf_static
  {
    connection_timeout: 5,
    connection_name: "[#{rand(999)}] backend",
    recover_from_connection_close: false
  }
end