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, vhost: '/', tls_options: {}, 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

  • vhost (String) (defaults to: '/')

    Virtual host

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

    a customizable set of options

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

    a customizable set of options

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

    a customizable set of options

Options Hash (tls_options:):

  • :tls (Boolean)

    when set to true, will set SSL context up and switch to TLS port (5671)

  • :tls_cert (String)

    string path to the client certificate (public key) in PEM format

  • :tls_key (String)

    string path to the client key (private key) in PEM format

  • :tls_ca_certificates (Array<String>)

    array of string paths to CA certificates in PEM format

  • :verify_peer (Boolean)

    determines if TLS peer authentication (verification) is performed, true by default



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

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

Instance Method Details

#connected?Boolean

checks if the connection is started

Returns:

  • (Boolean)


48
49
50
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 48

def connected?
  connection.status == :started
end

#create_channelBunny::Channel

Creates a channel

Returns:

  • (Bunny::Channel)


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

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.



53
54
55
56
57
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 53

def disconnect
  connection.close_all_channels
  connection.close
  reset
end

#startObject

Starts the connection if needed



42
43
44
45
# File 'lib/basquiat/adapters/rabbitmq/connection.rb', line 42

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