class HealthCheck - HealthCheck has no descriptive comment
CUSTOM_ITEMS = %w(hostname url client_ip localtime utctime)
BOOLEAN_ITEMS = %w(database_connection database_persistence database_integrity)
TEST_SETTINGS_KEY = 'healthcheck_test_key'
def self.checkup(env)
new(env).checkup
end
def initialize(env)
@env = env
end
def checkup - HealthCheck#checkup has approx 6 statements
diagnosis = {}
CUSTOM_ITEMS.each do |symptom|
diagnosis[symptom] = self.send(symptom) -
end
BOOLEAN_ITEMS.each do |symptom|
diagnosis[symptom] = stringify(self.send(symptom)) -
end
diagnosis
end
private
def hostname - HealthCheck#hostname doesn't depend on instance state (maybe move it to another class?)
Socket.gethostname
end
def url
@env.fetch('REQUEST_URI', 'FAILED: no REQUEST_URI present in env')
end
def client_ip
@env.fetch('REMOTE_ADDR', 'FAILED: no REMOTE_ADDR present in env')
end
def localtime - HealthCheck#localtime doesn't depend on instance state (maybe move it to another class?)
Time.current.rfc2822
end
def utctime - HealthCheck#utctime doesn't depend on instance state (maybe move it to another class?)
Time.current.getutc.rfc2822
end
def stringify(b) - HealthCheck#stringify has the parameter name 'b'
b ? 'OK' : 'FAILED' - HealthCheck#stringify is controlled by argument 'b'
end
def database_connection - HealthCheck#database_connection doesn't depend on instance state (maybe move it to another class?)
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection
true
rescue
false
end
def database_persistence
return false unless database_connection
return false unless Site.first_or_create
return false unless Site.last_checked_at!
true
rescue
false
end
def database_integrity
return false unless database_connection
!ActiveRecord::Migrator.needs_migration?
end
end