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



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

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) && ! basic_auth_username
  if stale?(:last_modified => last_modified, :public => public)
    # Rails 4.0 doesn't have :plain, but it is deprecated later on
    plain_key = Rails.version < '4.1' ? :text : :plain
    checks = params[:checks] || 'standard'
    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?
      obj = { :healthy => true, :message => HealthCheck.success }
      respond_to do |format|
        format.html { render plain_key => HealthCheck.success, :content_type => 'text/plain' }
        format.json { render :json => obj }
        format.xml { render :xml => obj }
        format.any { render plain_key => HealthCheck.success, :content_type => 'text/plain' }
      end
    else
      msg = "health_check failed: #{errors}"
      obj = { :healthy => false, :message => msg }
      respond_to do |format|
        format.html { render plain_key => msg, :status => HealthCheck.http_status_for_error_text, :content_type => 'text/plain'  }
        format.json { render :json => obj, :status => HealthCheck.http_status_for_error_object}
        format.xml { render :xml => obj, :status => HealthCheck.http_status_for_error_object }
        format.any { render plain_key => msg, :status => HealthCheck.http_status_for_error_text, :content_type => 'text/plain'  }
      end
      # 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