Class: Philiprehberger::CircuitBoard::Check
- Inherits:
-
Object
- Object
- Philiprehberger::CircuitBoard::Check
- Defined in:
- lib/philiprehberger/circuit_board/check.rb
Overview
Represents a single health check definition.
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#critical ⇒ Object
readonly
Returns the value of attribute critical.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#call ⇒ Hash
Execute the health check, returning a cached result if available.
-
#initialize(name, timeout: 5, critical: true, cache: nil, &block) ⇒ Check
constructor
A new instance of Check.
Constructor Details
#initialize(name, timeout: 5, critical: true, cache: nil, &block) ⇒ Check
Returns a new instance of Check.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 14 def initialize(name, timeout: 5, critical: true, cache: nil, &block) @name = name @timeout = timeout @critical = critical @cache = cache @block = block @cache_mutex = Mutex.new @cached_result = nil @cached_at = nil end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
7 8 9 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 7 def cache @cache end |
#critical ⇒ Object (readonly)
Returns the value of attribute critical.
7 8 9 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 7 def critical @critical end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 7 def name @name end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 7 def timeout @timeout end |
Instance Method Details
#call ⇒ Hash
Execute the health check, returning a cached result if available.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/philiprehberger/circuit_board/check.rb', line 28 def call cached = read_cache return cached if cached start = Process.clock_gettime(Process::CLOCK_MONOTONIC) healthy = execute_with_timeout duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start result = { name: @name, healthy: healthy, critical: @critical, duration: duration.round(4) } write_cache(result) if healthy result rescue StandardError => e duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start { name: @name, healthy: false, critical: @critical, duration: duration.round(4), error: e. } end |