Class: OkComputer::CacheCheck

Inherits:
Check
  • Object
show all
Defined in:
lib/ok_computer/built_in_checks/cache_check.rb

Overview

Verifies that the Rails cache is set up and can speak with Memcached running on the given host (defaults to local).

Constant Summary

Constants inherited from Check

OkComputer::Check::CheckNotDefined

Instance Attribute Summary collapse

Attributes inherited from Check

#failure_occurred, #message, #registrant_name, #time

Instance Method Summary collapse

Methods inherited from Check

#<=>, #clear, #mark_failure, #mark_message, #run, #success?, #to_json, #to_text, #with_benchmarking

Constructor Details

#initialize(host = Socket.gethostname) ⇒ CacheCheck

Returns a new instance of CacheCheck.



7
8
9
# File 'lib/ok_computer/built_in_checks/cache_check.rb', line 7

def initialize(host=Socket.gethostname)
  self.host = host
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



5
6
7
# File 'lib/ok_computer/built_in_checks/cache_check.rb', line 5

def host
  @host
end

Instance Method Details

#checkObject

Public: Check whether the cache is active



12
13
14
15
16
17
# File 'lib/ok_computer/built_in_checks/cache_check.rb', line 12

def check
  mark_message "Cache is available (#{stats})"
rescue ConnectionFailed => e
  mark_failure
  mark_message "Error: '#{e}'"
end

#statsObject

Public: Outputs stats string for cache



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ok_computer/built_in_checks/cache_check.rb', line 20

def stats
  return "" unless Rails.cache.respond_to? :stats

  stats    = Rails.cache.stats
  values     = stats.select{|k,v| k =~ Regexp.new(host) }.values[0]
  mem_used = to_megabytes values['bytes']
  mem_max  = to_megabytes values['limit_maxbytes']
  return "#{mem_used} / #{mem_max} MB, #{stats.count - 1} peers"
rescue => e
  raise ConnectionFailed, e
end