Class: Liebre::ConnectionManager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/liebre/connection_manager.rb

Instance Method Summary collapse

Constructor Details

#initializeConnectionManager

Returns a new instance of ConnectionManager.



10
11
12
13
14
15
# File 'lib/liebre/connection_manager.rb', line 10

def initialize
  @path = Liebre::Config.connection_path
  @connections = {}
  @channels = {}
  @lock = Mutex.new
end

Instance Method Details

#channel_for(connection_name, consumer_pool_size = 1) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/liebre/connection_manager.rb', line 47

def channel_for connection_name, consumer_pool_size = 1
  @lock.synchronize do
    channels[connection_name] ||= begin
      get(connection_name).create_channel nil, consumer_pool_size
    end
  end
end

#ensure_startedObject



31
32
33
34
35
36
# File 'lib/liebre/connection_manager.rb', line 31

def ensure_started
  all_open = !@connections.empty? and @connections.all? do |_, bunny|
    bunny.open?
  end
  restart unless all_open
end

#get(connection_name) ⇒ Object



43
44
45
# File 'lib/liebre/connection_manager.rb', line 43

def get connection_name
  connections[connection_name.to_sym]
end

#restartObject



38
39
40
41
# File 'lib/liebre/connection_manager.rb', line 38

def restart
  stop
  start
end

#startObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/liebre/connection_manager.rb', line 17

def start
  @lock.synchronize do
    initialize_connections
    connections.each do |connection_name, bunny|
      begin
        bunny.start
      rescue => e
        $logger.error("#{self.class.name}: Can't connect to #{connection_name} instance")
        $logger.error(e.message + "\n" + e.backtrace.join("\n"))
      end
    end
  end
end

#stopObject



55
56
57
58
59
60
61
62
63
64
# File 'lib/liebre/connection_manager.rb', line 55

def stop
  @lock.synchronize do
    connections.each do |_, bunny|
      if bunny and bunny.open?
        bunny.close
      end
    end
    connections.clear
  end
end