Module: Pingable

Defined in:
lib/pingable/core.rb,
lib/pingable/handler.rb,
lib/pingable/version.rb,
lib/pingable/common_checks.rb

Defined Under Namespace

Classes: Handler

Constant Summary collapse

VERSION =
'0.0.9'

Class Method Summary collapse

Class Method Details

.active_record_checks!Object

Add checks for ActiveRecord.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pingable/common_checks.rb', line 21

def active_record_checks!
  require 'timeout'
  add_check lambda {
    begin
      timeout(10) do
        ActiveRecord::Base.verify_active_connections!
      end
      ActiveRecord::Base.connection.execute('select 1')
      nil
    rescue Timeout::Error
      "ActiveRecord: Timed out"
    rescue Exception => e
      if e.class.name == e.message
        "ActiveRecord: #{e}"
      else
        "ActiveRecord: #{e.class}: #{e.message}"
      end
    end
  }
end

.add_check(check) ⇒ Object



6
7
8
# File 'lib/pingable/core.rb', line 6

def add_check(check)
  @@checks << check
end

.common_checks!Object

Add checks for standard gems such as Active Record, based on what is currently available.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pingable/common_checks.rb', line 8

def common_checks!
  unless @@common_checks_added
    if defined?(ActiveRecord)
      active_record_checks!
    end
    if defined?(Rails) and Rails.respond_to?(:cache)
      rails_cache_checks!
    end
    @@common_checks_added = true
  end
end

.rails_cache_checks!Object

Add Rails cache checks.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pingable/common_checks.rb', line 43

def rails_cache_checks!
  add_check lambda {
    begin
      Rails.cache.read('ping_check')
      Rails.cache.write('ping_check', 'ok')
      nil
    rescue Exception => e
      "Rails cache: #{e.class}: #{e.message}"
    end
  }
end

.run_checks!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pingable/core.rb', line 10

def run_checks!
  failures = []
  @@checks.each do |check|
    begin
      result = check.call
    rescue Exception => e
      failures.push(:message => "Pinger check failed: #{e}")
    else
      if result
        case result
          when Array
            failures.concat result
          when String
            failures.push(:message => result)
          else
            failures.push(result)
        end
      end
    end
  end
  failures
end