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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/health_check/health_check_controller.rb', line 12

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
  is_public = (max_age > 1) && ! HealthCheck.basic_auth_username
  if stale?(last_modified: last_modified, public: is_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'] = "must-revalidate, max-age=#{max_age}"
    if errors.blank?
      send_response true, nil, :ok, :ok
      if HealthCheck.success_callbacks
        HealthCheck.success_callbacks.each do |callback|
          callback.call(checks)
        end 
      end
    else
      msg = HealthCheck.include_error_in_response_body ? "#{HealthCheck.failure}: #{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
      msg = "#{HealthCheck.failure}: #{errors}"
      logger.send(HealthCheck.log_level, msg) if logger && HealthCheck.log_level
      if HealthCheck.failure_callbacks
        HealthCheck.failure_callbacks.each do |callback|
          callback.call(checks, msg)
        end
      end
    end
  end
end