Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Includes:
SeamlessDatabasePoolBehavior
Defined in:
lib/active_record/connection_adapters/seamless_database_pool_adapter.rb

Defined Under Namespace

Modules: SeamlessDatabasePoolBehavior

Class Method Summary collapse

Methods included from SeamlessDatabasePoolBehavior

included, #reload_with_seamless_database_pool

Class Method Details

.establish_adapter(adapter) ⇒ Object

Raises:

  • (AdapterNotSpecified)

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_record/connection_adapters/seamless_database_pool_adapter.rb', line 41

def establish_adapter(adapter)
  raise AdapterNotSpecified.new("database configuration does not specify adapter") unless adapter
  raise AdapterNotFound.new("database pool must specify adapters") if adapter == 'seamless_database_pool'

  begin
    require 'rubygems'
    gem "activerecord-#{adapter}-adapter"
    require "active_record/connection_adapters/#{adapter}_adapter"
  rescue LoadError
    begin
      require "active_record/connection_adapters/#{adapter}_adapter"
    rescue LoadError
      raise "Please install the #{adapter} adapter: `gem install activerecord-#{adapter}-adapter` (#{$!})"
    end
  end

  adapter_method = "#{adapter}_connection"
  if !respond_to?(adapter_method)
    raise AdapterNotFound, "database configuration specifies nonexistent #{adapter} adapter"
  end
end

.seamless_database_pool_connection(config) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/connection_adapters/seamless_database_pool_adapter.rb', line 4

def seamless_database_pool_connection(config)
  pool_weights = {}

  config = config.with_indifferent_access
  default_config = {:pool_weight => 1}.merge(config.merge(:adapter => config[:pool_adapter])).with_indifferent_access
  default_config.delete(:master)
  default_config.delete(:read_pool)
  default_config.delete(:pool_adapter)

  master_config = default_config.merge(config[:master]).with_indifferent_access
  establish_adapter(master_config[:adapter])
  master_connection = send("#{master_config[:adapter]}_connection".to_sym, master_config)
  pool_weights[master_connection] = master_config[:pool_weight].to_i if master_config[:pool_weight].to_i > 0
  
  read_connections = []
  config[:read_pool].each do |read_config|
    read_config = default_config.merge(read_config).with_indifferent_access
    read_config[:pool_weight] = read_config[:pool_weight].to_i
    if read_config[:pool_weight] > 0
      begin
        establish_adapter(read_config[:adapter])
        conn = send("#{read_config[:adapter]}_connection".to_sym, read_config)
        read_connections << conn
        pool_weights[conn] = read_config[:pool_weight]
      rescue Exception => e
        if logger
          logger.error("Error connecting to read connection #{read_config.inspect}")
          logger.error(e)
        end
      end
    end
  end if config[:read_pool]

  klass = ::ActiveRecord::ConnectionAdapters::SeamlessDatabasePoolAdapter.adapter_class(master_connection)
  klass.new(nil, logger, master_connection, read_connections, pool_weights)
end