class Easymon::SplitActiveRecordCheck

Attributes

block[RW]
results[RW]

Public Class Methods

new(&block) click to toggle source

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)

# File lib/easymon/checks/split_active_record_check.rb, line 32
def initialize(&block)
  self.block = block
end

Public Instance Methods

check() click to toggle source

Assumes only 2 connections

# 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

Private Instance Methods

database_up?(connection) click to toggle source
# File lib/easymon/checks/split_active_record_check.rb, line 49
def database_up?(connection)
  connection.active?
rescue
  false
end