Class: SinatraHealthCheck::Checker
- Inherits:
-
Object
- Object
- SinatraHealthCheck::Checker
- Defined in:
- lib/sinatra-health-check/checker.rb
Overview
The application health check. Create an instance and use .health to repond to your health check requests.
Constant Summary collapse
- DEFAULT_OPTS =
{ :aggregator => SinatraHealthCheck::Status::StrictAggregator.new, :exit => true, :health => true, :logger => nil, :signals => %w[TERM INT], :systems => {}, :timeout => 10, :wait => 0, }
Instance Attribute Summary collapse
-
#health ⇒ Object
Returns the value of attribute health.
-
#systems ⇒ Object
readonly
Returns the value of attribute systems.
Instance Method Summary collapse
-
#graceful_stop ⇒ Object
Set application to unhealthy state and stop it after wating for ++@timeout++.
- #healthy? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Checker
constructor
Create a health checker.
-
#join ⇒ Object
Waits for the stopping thread to finish.
-
#status ⇒ Object
Returns a Status object.
Constructor Details
#initialize(opts = {}) ⇒ Checker
Create a health checker. Params: aggrgator
: an aggregator for substatuus, default: StrictAggregator exit
: call exit
at the end of graceful_stop
health
: initial health state logger
: a logger signals
: array of signals to register a graceful stop handler systems
: a hash of subsystems responding to .status timeout
: timeout for graceful stop in seconds wait
: wait before setting health to unhealthy
31 32 33 34 35 36 37 |
# File 'lib/sinatra-health-check/checker.rb', line 31 def initialize(opts = {}) @opts = DEFAULT_OPTS.merge(opts) @aggregator = SinatraHealthCheck::Status::OverwritingAggregator.new(@opts[:aggregator]) @health = @opts[:health] @systems = @opts[:systems] trap(@opts[:signals]) end |
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
18 19 20 |
# File 'lib/sinatra-health-check/checker.rb', line 18 def health @health end |
#systems ⇒ Object (readonly)
Returns the value of attribute systems.
19 20 21 |
# File 'lib/sinatra-health-check/checker.rb', line 19 def systems @systems end |
Instance Method Details
#graceful_stop ⇒ Object
Set application to unhealthy state and stop it after wating for ++@timeout++.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sinatra-health-check/checker.rb', line 40 def graceful_stop # set to unhealthy state unless @stopper # spawn a thread to stop application after a given time @stopper = Thread.new do if @opts[:wait] > 0 logger.info "asked to stop application, waiting for #{@opts[:wait]}s before doing so" if logger sleep @opts[:wait] end logger.info "stopping application, waiting for #{@opts[:timeout]}s" if logger @health = false sleep @opts[:timeout] logger.info "exit application" if logger exit if @opts[:exit] end end end |
#healthy? ⇒ Boolean
70 71 72 |
# File 'lib/sinatra-health-check/checker.rb', line 70 def healthy? status.level != :error end |
#join ⇒ Object
Waits for the stopping thread to finish
59 60 61 |
# File 'lib/sinatra-health-check/checker.rb', line 59 def join @stopper.join if @stopper end |
#status ⇒ Object
Returns a Status object
64 65 66 67 68 |
# File 'lib/sinatra-health-check/checker.rb', line 64 def status statuus = {} systems.each { |k,v| statuus[k] = v.status if v.respond_to?(:status) } @aggregator.aggregate(statuus, health ? nil : SinatraHealthCheck::Status.new(:error, 'app is unhealthy')) end |