class HealthReporter

Constants

VERSION

Public Class Methods

clear_cache() click to toggle source
# File lib/health_reporter/reporter.rb, line 40
def self.clear_cache
  @@last_check_time     = nil
  @@healthy             = nil
end
clear_dependencies() click to toggle source
# File lib/health_reporter/reporter.rb, line 45
def self.clear_dependencies
  @@dependencies = {}
end
dependencies() click to toggle source
# File lib/health_reporter/reporter.rb, line 59
def self.dependencies
  @@dependencies
end
healthy?() click to toggle source
# File lib/health_reporter/reporter.rb, line 69
def self.healthy?
  @@semaphore.synchronize {
    perform_health_check if @@healthy.nil? or cache_ttl_expired
    @@healthy
  }
end
healthy_cache_ttl() click to toggle source
# File lib/health_reporter/reporter.rb, line 24
def self.healthy_cache_ttl
  @@healthy_cache_ttl
end
healthy_cache_ttl=(healthy_cache_ttl) click to toggle source
# File lib/health_reporter/reporter.rb, line 28
def self.healthy_cache_ttl=(healthy_cache_ttl)
  @@healthy_cache_ttl = healthy_cache_ttl
end
register_dependencies(provided_dependencies = []) click to toggle source
# File lib/health_reporter/reporter.rb, line 49
def self.register_dependencies(provided_dependencies = [])
  provided_dependencies.map{ |dependency|
    symbolized_dependency = Hash[dependency.map{|(k,v)| [k.to_sym,v]}]
    raise 'url not defined for dependency' unless symbolized_dependency[:url]
    add_defaults(symbolized_dependency)
    dependencies[symbolized_dependency[:url]] = symbolized_dependency
    dependencies[symbolized_dependency[:url]].delete(:url)
  }
end
register_dependency(url:, code: 200, timeout: 2) click to toggle source
# File lib/health_reporter/reporter.rb, line 63
def self.register_dependency(url:, code: 200, timeout: 2)
  $stderr.puts "The HealthReporter method register_dependency is depreciated. Use fully featured register_dependencies method instead"
  raise "Configured URL #{url} is invalid" unless url =~ URI::regexp
  dependencies[url] = { :code => code, :timeout => timeout }
end
self_test() click to toggle source
# File lib/health_reporter/reporter.rb, line 8
def self.self_test
  @@self_test
end
self_test=(self_test) click to toggle source
# File lib/health_reporter/reporter.rb, line 12
def self.self_test=(self_test)
  @@self_test = self_test
end
unhealthy_cache_ttl() click to toggle source
# File lib/health_reporter/reporter.rb, line 16
def self.unhealthy_cache_ttl
  @@unhealthy_cache_ttl
end
unhealthy_cache_ttl=(unhealthy_cache_ttl) click to toggle source
# File lib/health_reporter/reporter.rb, line 20
def self.unhealthy_cache_ttl=(unhealthy_cache_ttl)
  @@unhealthy_cache_ttl = unhealthy_cache_ttl
end

Private Class Methods

add_defaults(dependency) click to toggle source
# File lib/health_reporter/reporter.rb, line 78
def self.add_defaults(dependency)
  dependency[:code] |= 200
  dependency[:timeout] |= 2
end
cache_ttl_expired() click to toggle source
# File lib/health_reporter/reporter.rb, line 125
def self.cache_ttl_expired
  return true if @@last_check_time.nil?
  Time.now > (@@last_check_time + ttl)
end
check_dependencies() click to toggle source
# File lib/health_reporter/reporter.rb, line 92
def self.check_dependencies
  @@dependencies.each { |url, configuration|
    check_dependency(url: url, configuration: configuration)
  }
  @@healthy = true
end
check_dependency(url:, configuration:) click to toggle source
# File lib/health_reporter/reporter.rb, line 99
def self.check_dependency(url:, configuration:)
  connection ||= Faraday.new(:url => url) do |faraday|
    faraday.options[:open_timeout] = configuration[:timeout]
    faraday.options[:timeout] = configuration[:timeout]
    faraday.request(:url_encoded)
    faraday.adapter Faraday.default_adapter
    faraday.basic_auth(configuration[:username], configuration[:password]) if configuration[:username]
  end

  response = connection.get

  unless response.status == configuration[:code]
    raise "Response expected to be #{configuration[:code]} but is #{response.status}"
  end
rescue Exception => exception
  @@healthy = false
  raise "Dependency <#{url}> failed check due to #{exception.class}: #{exception.message}"
end
perform_health_check() click to toggle source
# File lib/health_reporter/reporter.rb, line 83
def self.perform_health_check
  @@last_check_time = Time.now
  @@healthy = sanitize(@@self_test.call)
  check_dependencies if @@healthy
rescue Exception => exception
  @@healthy = false
  raise
end
sanitize(result) click to toggle source
# File lib/health_reporter/reporter.rb, line 118
def self.sanitize(result)
  unless [true, false].include?(result)
    raise "Invalid non-boolean response from registered self-check lambda: #{result.to_s}"
  end
  result
end
ttl() click to toggle source
# File lib/health_reporter/reporter.rb, line 130
def self.ttl
  return @@healthy_cache_ttl if @@healthy
  @@unhealthy_cache_ttl
end