Class: HealthCheck::MiddlewareHealthcheck

Inherits:
Object
  • Object
show all
Defined in:
lib/health_check/middleware_health_check.rb

Defined Under Namespace

Classes: Request

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ MiddlewareHealthcheck

Returns a new instance of MiddlewareHealthcheck.



8
9
10
# File 'lib/health_check/middleware_health_check.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/health_check/middleware_health_check.rb', line 12

def call(env)
  (response_type, middleware_checks, full_stack_checks) = parse_env(env)
  if response_type
    if error_response = (ip_blocked(env) || not_authenticated(env))
      return error_response
    end
    HealthCheck.installed_as_middleware = true
    errors = ''
    begin
      # Process the checks to be run from middleware
      errors = HealthCheck::Utils.process_checks(middleware_checks, true)
      # Process remaining checks through the full stack if there are any
      unless full_stack_checks.empty?
        return @app.call(env)
      end
    rescue => e
      errors = e.message.blank? ? e.class.to_s : e.message.to_s
    end
    healthy = errors.blank?
    msg = healthy ? HealthCheck.success : "health_check failed: #{errors}"
    if response_type == 'xml'
      content_type = 'text/xml'
      msg = { healthy: healthy, message: msg }.to_xml
      error_code = HealthCheck.http_status_for_error_object
    elsif response_type == 'json'
      content_type = 'application/json'
      msg = { healthy: healthy, message: msg }.to_json
      error_code = HealthCheck.http_status_for_error_object
    else
      content_type = 'text/plain'
      error_code = HealthCheck.http_status_for_error_text
    end
    [ (healthy ? 200 : error_code), { 'Content-Type' => content_type }, [msg] ]
  else
    @app.call(env)
  end
end