Class: Easymon::SplitActiveRecordCheck
- Inherits:
-
Object
- Object
- Easymon::SplitActiveRecordCheck
- Defined in:
- lib/easymon/checks/split_active_record_check.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#results ⇒ Object
Returns the value of attribute results.
Instance Method Summary collapse
-
#check ⇒ Object
Assumes only 2 connections.
-
#initialize(&block) ⇒ SplitActiveRecordCheck
constructor
Here we pass a block so we get a fresh instance of ActiveRecord::Base or whatever other class we might be using to make database connections.
Constructor Details
#initialize(&block) ⇒ SplitActiveRecordCheck
Here we pass a block so we get a fresh instance of ActiveRecord::Base or whatever other class we might be using to make database connections
For example, given the following other class: module Easymon
class Base < ActiveRecord::Base
def establish_connection(spec = nil)
if spec
super
elsif config = Easymon.database_configuration
super config
end
end
def database_configuration
env = "#{Rails.env}_replica"
config = YAML.load_file(Rails.root.join('config/database.yml'))[env]
end
end
end
We would check both it and ActiveRecord::Base like so: check = Easymon::SplitActiveRecordCheck.new
[ActiveRecord::Base.connection, Easymon::Base.connection]
Easymon::Repository.add(“split-database”, check)
32 33 34 |
# File 'lib/easymon/checks/split_active_record_check.rb', line 32 def initialize(&block) self.block = block end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
3 4 5 |
# File 'lib/easymon/checks/split_active_record_check.rb', line 3 def block @block end |
#results ⇒ Object
Returns the value of attribute results.
4 5 6 |
# File 'lib/easymon/checks/split_active_record_check.rb', line 4 def results @results end |
Instance Method Details
#check ⇒ Object
Assumes only 2 connections
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/easymon/checks/split_active_record_check.rb', line 37 def check connections = Array(@block.call) results = connections.map{|connection| database_up?(connection) } primary_status = results.first ? "Primary: Up" : "Primary: Down" replica_status = results.last ? "Replica: Up" : "Replica: Down" [(results.all? && results.count > 0), "#{primary_status} - #{replica_status}"] end |