Class: Rack::Healthz::Accumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/healthz/accumulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, max_time_between_requests: DefaultTimeBetweenRequests, path: DefaultPath) ⇒ Accumulator

Returns a new instance of Accumulator.



6
7
8
9
10
11
12
13
14
# File 'lib/rack/healthz/accumulator.rb', line 6

def initialize(app, max_time_between_requests: DefaultTimeBetweenRequests, path: DefaultPath)
  @app = app
  @count = 0
  @stats = {}
  @up_at = Healthz.current_time
  @last_success_time = nil
  @max_time_between_requests = max_time_between_requests
  @path = path
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def app
  @app
end

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def count
  @count
end

#last_success_timeObject (readonly)

Returns the value of attribute last_success_time.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def last_success_time
  @last_success_time
end

#max_time_between_requestsObject (readonly)

Returns the value of attribute max_time_between_requests.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def max_time_between_requests
  @max_time_between_requests
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def path
  @path
end

#statsObject (readonly)

Returns the value of attribute stats.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def stats
  @stats
end

#up_atObject (readonly)

Returns the value of attribute up_at.



4
5
6
# File 'lib/rack/healthz/accumulator.rb', line 4

def up_at
  @up_at
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack/healthz/accumulator.rb', line 16

def call(env)
  count!

  response = nil

  elapsed_time = Healthz.measure do
    response = app.call(env)
  end

  handle_response! elapsed_time, Rack::Response.new(response)

  response
end

#healthy?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/rack/healthz/accumulator.rb', line 34

def healthy?
  !time_since_last_success.nil? and (time_since_last_success < max_time_between_requests)
end

#responseObject



38
39
40
# File 'lib/rack/healthz/accumulator.rb', line 38

def response
  Response.new self
end

#statusObject



50
51
52
53
54
55
56
# File 'lib/rack/healthz/accumulator.rb', line 50

def status
  if healthy?
    "healthy"
  else
    "unhealthy"
  end
end

#status_request?(env) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rack/healthz/accumulator.rb', line 30

def status_request?(env)
  env.fetch('PATH_INFO') == path
end

#time_since_last_successObject



42
43
44
# File 'lib/rack/healthz/accumulator.rb', line 42

def time_since_last_success
  last_success_time ? current_time - last_success_time : nil
end

#to_hObject



58
59
60
61
62
63
64
65
66
# File 'lib/rack/healthz/accumulator.rb', line 58

def to_h
  {}.tap do |result|
    result[:count] = count
    result[:uptime] = uptime
    result[:time_since_last_success] = time_since_last_success
    result[:status] = status
    result[:stats] = stats_hash
  end
end

#uptimeObject



46
47
48
# File 'lib/rack/healthz/accumulator.rb', line 46

def uptime
  current_time - up_at
end