Module: RailsFailover::ActiveRecord

Defined in:
lib/rails_failover/active_record.rb,
lib/rails_failover/active_record/handler.rb,
lib/rails_failover/active_record/railtie.rb,
lib/rails_failover/active_record/middleware.rb

Defined Under Namespace

Classes: Handler, Interceptor, Middleware, Railtie

Class Method Summary collapse

Class Method Details

.configObject



13
14
15
# File 'lib/rails_failover/active_record.rb', line 13

def config
  ::ActiveRecord::Base.connection_db_config.configuration_hash
end

.establish_reading_connection(handler, config, role: reading_role) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rails_failover/active_record.rb', line 33

def establish_reading_connection(handler, config, role: reading_role)
  return unless config[:replica_host] && config[:replica_port]
  replica_config = config.dup
  replica_config[:host] = replica_config.delete(:replica_host)
  replica_config[:port] = replica_config.delete(:replica_port)
  replica_config[:replica] = true
  handler.establish_connection(replica_config, role: role)
end

.loggerObject



21
22
23
# File 'lib/rails_failover/active_record.rb', line 21

def logger
  @logger || Rails.logger
end

.logger=(logger) ⇒ Object



17
18
19
# File 'lib/rails_failover/active_record.rb', line 17

def logger=(logger)
  @logger = logger
end

.on_failover(&block) ⇒ Object



46
47
48
# File 'lib/rails_failover/active_record.rb', line 46

def on_failover(&block)
  @on_failover_callback = block
end

.on_failover_callback!(key) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/rails_failover/active_record.rb', line 50

def on_failover_callback!(key)
  @on_failover_callback&.call(key)
rescue => e
  logger.warn(
    "RailsFailover::ActiveRecord.on_failover failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}",
  )
end

.on_fallback(&block) ⇒ Object



58
59
60
# File 'lib/rails_failover/active_record.rb', line 58

def on_fallback(&block)
  @on_fallback_callback = block
end

.on_fallback_callback!(key) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rails_failover/active_record.rb', line 62

def on_fallback_callback!(key)
  @on_fallback_callback&.call(key)
rescue => e
  logger.warn(
    "RailsFailover::ActiveRecord.on_fallback failed: #{e.class} #{e.message}\n#{e.backtrace.join("\n")}",
  )
end

.reading_roleObject



70
71
72
# File 'lib/rails_failover/active_record.rb', line 70

def reading_role
  ::ActiveRecord.try(:reading_role) || ::ActiveRecord::Base.reading_role
end

.register_force_reading_role_callback(&block) ⇒ Object



42
43
44
# File 'lib/rails_failover/active_record.rb', line 42

def register_force_reading_role_callback(&block)
  Middleware.force_reading_role_callback = block
end

.verify_primary_frequency_secondsObject



29
30
31
# File 'lib/rails_failover/active_record.rb', line 29

def verify_primary_frequency_seconds
  @verify_primary_frequency_seconds || 5
end

.verify_primary_frequency_seconds=(seconds) ⇒ Object



25
26
27
# File 'lib/rails_failover/active_record.rb', line 25

def verify_primary_frequency_seconds=(seconds)
  @verify_primary_frequency_seconds = seconds
end

.writing_roleObject



74
75
76
# File 'lib/rails_failover/active_record.rb', line 74

def writing_role
  ::ActiveRecord.try(:writing_role) || ::ActiveRecord::Base.writing_role
end