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



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

def index
  last_modified = Time.now.utc
  max_age = HealthCheck.max_age
  last_modified = Time.at((last_modified.to_f / max_age).floor * max_age).utc if max_age > 1
  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 true, nil, :ok, :ok
    else
      msg = HealthCheck.include_error_in_response_body ? "health_check failed: #{errors}" : nil
      send_response false, 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
      logger&.info msg
    end
  end
end