Class: Basquiat::Adapters::RabbitMq::Connection

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/basquiat/adapters/rabbitmq/connection.rb

Overview

Control the connection to the RabitMQ server. Delegates calls to Bunny::Connection

Instance Method Summary collapse

Constructor Details

#initialize(hosts:, port: 5672, failover: {}, auth: {}) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • hosts: (Array<String>)

    IPs or FQDN of the RabbitMQ instances

  • port (Fixnum) (defaults to: 5672)

    Port that the RabbitMQ instances run

  • failover: (Hash) (defaults to: {})

    a customizable set of options

  • auth: (Hash) (defaults to: {})

    a customizable set of options



18
19
20
21
22
23
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 18

def initialize(hosts:, port: 5672, failover: {}, auth: {})
  @hosts    = hosts
  @port     = port
  @failover = failover
  @auth     = auth
end

Instance Method Details

#connected?Boolean

checks if the connection is started

Returns:

  • (Boolean)


40
41
42
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 40

def connected?
  connection.status == :started
end

#create_channelBunny::Channel

Creates a channel

Returns:

  • (Bunny::Channel)


27
28
29
30
31
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 27

def create_channel
  connection.start unless connected?
  Basquiat.logger.debug 'Creating a new channel'
  connection.create_channel
end

#disconnectObject

Closes all channels and then the connection.



45
46
47
48
49
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 45

def disconnect
  connection.close_all_channels
  connection.close
  reset
end

#startObject

Starts the connection if needed



34
35
36
37
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 34

def start
  Basquiat.logger.debug 'Connecting to RabbitMQ'
  connection.start unless connection.connected?
end