Class: StandardHealth::Check
- Inherits:
-
Object
- Object
- StandardHealth::Check
- Defined in:
- lib/standard_health/check.rb
Overview
Base class for health checks.
Subclasses implement ‘#run`, returning a hash like:
{ status: :ok, latency_ms: 3 }
or
{ status: :fail, error: "connection refused" }
The ‘with_timing` helper wraps a block, captures latency, and converts any unhandled `StandardError` into a `:fail` row so subclasses don’t have to repeat the pattern.
Direct Known Subclasses
StandardHealth::Checks::ActiveRecord, StandardHealth::Checks::SolidCache, StandardHealth::Checks::SolidQueue
Instance Attribute Summary collapse
-
#critical ⇒ Object
readonly
Returns the value of attribute critical.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #critical? ⇒ Boolean
-
#initialize(name:, critical: false) ⇒ Check
constructor
A new instance of Check.
-
#run ⇒ Object
Subclasses override this.
-
#with_timing ⇒ Object
Wrap a block: time it, return :ok with latency_ms on success, or :fail with the error message on any StandardError.
Constructor Details
#initialize(name:, critical: false) ⇒ Check
Returns a new instance of Check.
20 21 22 23 |
# File 'lib/standard_health/check.rb', line 20 def initialize(name:, critical: false) @name = name @critical = critical end |
Instance Attribute Details
#critical ⇒ Object (readonly)
Returns the value of attribute critical.
18 19 20 |
# File 'lib/standard_health/check.rb', line 18 def critical @critical end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/standard_health/check.rb', line 18 def name @name end |
Instance Method Details
#critical? ⇒ Boolean
25 26 27 |
# File 'lib/standard_health/check.rb', line 25 def critical? !!@critical end |
#run ⇒ Object
Subclasses override this. Default implementation reports an unimplemented check rather than raising, so a misconfigured custom check degrades gracefully instead of taking down /ready.
32 33 34 |
# File 'lib/standard_health/check.rb', line 32 def run { status: :fail, error: "not implemented" } end |
#with_timing ⇒ Object
Wrap a block: time it, return :ok with latency_ms on success, or :fail with the error message on any StandardError. Useful for check implementations that boil down to “run this query, swallow exceptions, report status”.
40 41 42 43 44 45 46 47 |
# File 'lib/standard_health/check.rb', line 40 def with_timing start = Process.clock_gettime(Process::CLOCK_MONOTONIC) yield latency_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start) * 1000).round { status: :ok, latency_ms: latency_ms } rescue StandardError => e { status: :fail, error: e. } end |