Class: StandardHealth::Check

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#criticalObject (readonly)

Returns the value of attribute critical.



18
19
20
# File 'lib/standard_health/check.rb', line 18

def critical
  @critical
end

#nameObject (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

Returns:

  • (Boolean)


25
26
27
# File 'lib/standard_health/check.rb', line 25

def critical?
  !!@critical
end

#runObject

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_timingObject

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.message }
end