Class: Tackle::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/tackle/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amqp_url, exception_handler, logger, connection = nil) ⇒ Connection

Returns a new instance of Connection.



5
6
7
8
9
10
11
12
# File 'lib/tackle/connection.rb', line 5

def initialize(amqp_url, exception_handler, logger, connection = nil)
  @amqp_url = amqp_url
  @exception_handler = exception_handler
  @logger = logger
  @connection = connection

  connect
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



3
4
5
# File 'lib/tackle/connection.rb', line 3

def channel
  @channel
end

Instance Method Details

#closeObject



37
38
39
40
41
42
43
# File 'lib/tackle/connection.rb', line 37

def close
  @channel.close
  @logger.info("Closed channel")

  @connection.close
  @logger.info("Closed connection to RabbitMQ")
end

#connectObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tackle/connection.rb', line 14

def connect
  @logger.info("Connecting to RabbitMQ")

  @connection ||= Bunny.new(@amqp_url)
  @connection.start

  @logger.info("Connected to RabbitMQ")

  @channel = @connection.create_channel
  @channel.prefetch(1)
  @channel.on_uncaught_exception(&@exception_handler)

  @logger.info("Connected to channel")
rescue Timeout::Error => ex
  @logger.error("Timeout while connecting to RabbitMQ server message='#{ex}'")

  raise ex
rescue StandardError => ex
  @logger.error("Error while connecting to RabbitMQ message='#{ex}'")

  raise ex
end