Class: HealthCheck::HealthCheckController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
lib/health_check/health_check_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/health_check/health_check_controller.rb', line 11

def index
  last_modified = Time.now.utc
  max_age = HealthCheck.max_age
  if max_age > 1
    last_modified = Time.at((last_modified.to_f / max_age).floor * max_age).utc
  end
  public = (max_age > 1) && ! HealthCheck.basic_auth_username
  if stale?(:last_modified => last_modified, :public => public)
    checks = params[:checks] ? params[:checks].split('_') : ['standard']
    checks -= HealthCheck.middleware_checks if HealthCheck.installed_as_middleware
    begin
      errors = HealthCheck::Utils.process_checks(checks)
    rescue Exception => e
      errors = e.message.blank? ? e.class.to_s : e.message.to_s
    end
    response.headers['Cache-control'] = (public ? 'public' : 'private') + ', no-cache, must-revalidate' + (max_age > 0 ? ", max-age=#{max_age}" : '')
    if errors.blank?
      send_response nil, :ok, :ok
    else
      msg = "health_check failed: #{errors}"
      send_response msg, HealthCheck.http_status_for_error_text, HealthCheck.http_status_for_error_object
      # Log a single line as some uptime checkers only record that it failed, not the text returned
      if logger
        logger.info msg
      end
    end
  end
end