Class: Rack::Healthz::Accumulator
- Inherits:
-
Object
- Object
- Rack::Healthz::Accumulator
- Defined in:
- lib/rack/healthz/accumulator.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#last_success_time ⇒ Object
readonly
Returns the value of attribute last_success_time.
-
#max_time_between_requests ⇒ Object
readonly
Returns the value of attribute max_time_between_requests.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#up_at ⇒ Object
readonly
Returns the value of attribute up_at.
Instance Method Summary collapse
- #call(env) ⇒ Object
- #healthy? ⇒ Boolean
-
#initialize(app, max_time_between_requests: DefaultTimeBetweenRequests, path: DefaultPath) ⇒ Accumulator
constructor
A new instance of Accumulator.
- #response ⇒ Object
- #status ⇒ Object
- #status_request?(env) ⇒ Boolean
- #time_since_last_success ⇒ Object
- #to_h ⇒ Object
- #uptime ⇒ Object
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
#app ⇒ Object (readonly)
Returns the value of attribute app.
4 5 6 |
# File 'lib/rack/healthz/accumulator.rb', line 4 def app @app end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
4 5 6 |
# File 'lib/rack/healthz/accumulator.rb', line 4 def count @count end |
#last_success_time ⇒ Object (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_requests ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
4 5 6 |
# File 'lib/rack/healthz/accumulator.rb', line 4 def path @path end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
4 5 6 |
# File 'lib/rack/healthz/accumulator.rb', line 4 def stats @stats end |
#up_at ⇒ Object (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
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 |
#response ⇒ Object
38 39 40 |
# File 'lib/rack/healthz/accumulator.rb', line 38 def response Response.new self end |
#status ⇒ Object
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
30 31 32 |
# File 'lib/rack/healthz/accumulator.rb', line 30 def status_request?(env) env.fetch('PATH_INFO') == path end |
#time_since_last_success ⇒ Object
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_h ⇒ Object
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 |
#uptime ⇒ Object
46 47 48 |
# File 'lib/rack/healthz/accumulator.rb', line 46 def uptime current_time - up_at end |