Class: PgFailover::ActiveRecordAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_failover/active_record_adapter.rb

Class Method Summary collapse

Class Method Details

.enableObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pg_failover/active_record_adapter.rb', line 5

def self.enable
  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.set_callback(:checkout, :after) do
    # this seems to be the connection pool that internaly has the connection loaded for the current thread
    # methods like #execute and #reconnect! are against that connection
    connection = self

    PgFailover.connection_validator.call(
      throttle_by: connection.raw_connection,
      in_recovery: proc {
        result = connection.execute('select pg_is_in_recovery()').first

        %w[1 t true].include?(result['pg_is_in_recovery'].to_s)
      },
      reconnect: proc { connection.reconnect! }
    )
  end
end