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
# File 'lib/liebre/connection_manager.rb', line 10

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

Instance Method Details

#channel_for(connection_name, consumer_pool_size = 1) ⇒ Object



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

def channel_for connection_name, consumer_pool_size = 1
  Common::Utils.mutex_sync do
    channels[connection_name] ||= begin
      get(connection_name).create_channel nil, consumer_pool_size
    end
  end
end

#ensure_startedObject



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

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

#get(connection_name) ⇒ Object



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

def get connection_name
  connections[connection_name.to_sym]
end

#restartObject



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

def restart
  stop
  start
end

#startObject



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

def start
  Common::Utils.mutex_sync 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



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

def stop
  Common::Utils.mutex_sync do
    connections.each do |_, bunny|
      if bunny and bunny.open?
        bunny.close
      end
    end
    connections.clear
  end
end