Class: Scheduler::Connection

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

Class Method Summary collapse

Class Method Details

.current_dbObject



4
5
6
# File 'lib/scheduler/connection.rb', line 4

def current_db
  ActiveRecord::Base.connection_pool.spec.config[:db_key] || "default"
end

.current_hostnameObject



8
9
10
11
# File 'lib/scheduler/connection.rb', line 8

def current_hostname
  config = ActiveRecord::Base.connection_pool.spec.config
  config[:host_names].nil? ? config[:host] : config[:host_names].first
end

.establish_connection(db) ⇒ Object



13
14
15
16
# File 'lib/scheduler/connection.rb', line 13

def establish_connection(db)
  config = ActiveRecord::Base.configurations[db]
  ActiveRecord::Base.establish_connection(config)
end

.with_connection(db) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/scheduler/connection.rb', line 18

def with_connection(db)
  old = current_db
  connected = ActiveRecord::Base.connection_pool.connected?

  establish_connection(:db => db) unless connected && db == old
  rval = yield db

  unless connected && db == old
    ActiveRecord::Base.connection_handler.clear_active_connections!

    establish_connection(:db => old)
    ActiveRecord::Base.connection_handler.clear_active_connections! unless connected
  end

  rval
end