Class: Leafy::Rack::Health

Inherits:
Object
  • Object
show all
Defined in:
lib/leafy/rack/health.rb

Constant Summary collapse

WRITER =
::Leafy::Json::HealthWriter.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, registry, path = '/health') ⇒ Health

Returns a new instance of Health.



21
22
23
24
25
# File 'lib/leafy/rack/health.rb', line 21

def initialize(app, registry, path = '/health')
  @app = app
  @path = path
  @registry = registry
end

Class Method Details

.response(health, env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/leafy/rack/health.rb', line 10

def self.response( health, env )
  data = health.run_health_checks
  is_healthy = data.values.all? { |r| r.healthy? }
  [
   is_healthy ? 200 : 500, 
   { 'Content-Type' => 'application/json',
     'Cache-Control' => 'must-revalidate,no-cache,no-store' }, 
   [ WRITER.to_json( data, env[ 'QUERY_STRING' ] == 'pretty' ) ]
  ]
end

Instance Method Details

#call(env) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/leafy/rack/health.rb', line 27

def call(env)
  if env['PATH_INFO'] == @path
    Health.response( @registry.health, env )
  else
    @app.call( env )
  end
end