Class: PikaQue::Connection
- Inherits:
-
Object
- Object
- PikaQue::Connection
show all
- Extended by:
- Forwardable
- Includes:
- Logging
- Defined in:
- lib/pika_que/connection.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Logging
init_logger, logger, #logger, logger=
Constructor Details
#initialize(opts = {}) ⇒ Connection
Returns a new instance of Connection.
11
12
13
14
15
|
# File 'lib/pika_que/connection.rb', line 11
def initialize(opts = {})
@opts = PikaQue.config.merge(opts)
@opts[:amqp] = ENV.fetch('RABBITMQ_URL', 'amqp://guest:guest@localhost:5672')
@opts[:vhost] = AMQ::Settings.parse_amqp_url(@opts[:amqp]).fetch(:vhost, '/')
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
9
10
11
|
# File 'lib/pika_que/connection.rb', line 9
def connection
@connection
end
|
Class Method Details
.create(opts = {}) ⇒ Object
17
18
19
|
# File 'lib/pika_que/connection.rb', line 17
def self.create(opts = {})
new(opts).tap{ |conn| conn.connect! }
end
|
Instance Method Details
#connect! ⇒ Object
21
22
23
24
25
26
27
28
|
# File 'lib/pika_que/connection.rb', line 21
def connect!
@connection ||= Bunny.new(@opts[:amqp], :vhost => @opts[:vhost],
:heartbeat => @opts[:heartbeat],
:properties => @opts.fetch(:properties, {}),
:logger => PikaQue::logger).tap do |conn|
conn.start
end
end
|
#connected? ⇒ Boolean
30
31
32
|
# File 'lib/pika_que/connection.rb', line 30
def connected?
@connection && @connection.connected?
end
|
#disconnect! ⇒ Object
34
35
36
37
|
# File 'lib/pika_que/connection.rb', line 34
def disconnect!
@connection.close if @connection
@connection = nil
end
|
#ensure_connection ⇒ Object
39
40
41
42
43
44
|
# File 'lib/pika_que/connection.rb', line 39
def ensure_connection
unless connected?
@connection = nil
connect!
end
end
|