Class: Leafy::Rack::Health

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Health.



37
38
39
40
41
# File 'lib/leafy/rack/health.rb', line 37

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

Class Method Details

.hostinfoObject



9
10
11
# File 'lib/leafy/rack/health.rb', line 9

def self.hostinfo
  @info ||= {}
end

.response(health, env) ⇒ Object



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/leafy/rack/health.rb', line 13

def self.response( health, env )
  checks = health.run_health_checks
  if @info
    data = { 'host' => @info, 'checks' => checks.to_hash }
  else
    data = checks
  end
  is_healthy = checks.values.all? { |r| r.healthy? }
  json = if env[ 'QUERY_STRING' ] == 'pretty'
           JSON.pretty_generate( data.to_hash )
         else
           # to use data.to_hash.to_json did produce
           # a different json structure on some
           # rack setup
           JSON.generate( data.to_hash )
         end
  [
   is_healthy ? 200 : 503, 
   { 'Content-Type' => 'application/json',
     'Cache-Control' => 'must-revalidate,no-cache,no-store' }, 
   [ json ]
  ]
end

Instance Method Details

#call(env) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/leafy/rack/health.rb', line 43

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