Class: Philiprehberger::CircuitBoard::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/circuit_board/check.rb

Overview

Represents a single health check definition.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, timeout: 5, critical: true, cache: nil, &block) ⇒ Check

Returns a new instance of Check.

Parameters:

  • name (Symbol)

    the check name

  • timeout (Numeric) (defaults to: 5)

    timeout in seconds

  • critical (Boolean) (defaults to: true)

    whether this check is critical (default: true)

  • cache (Numeric, nil) (defaults to: nil)

    cache successful results for this many seconds (default: nil — no caching)

  • block (Proc)

    block that returns truthy if healthy



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

#cacheObject (readonly)

Returns the value of attribute cache.



7
8
9
# File 'lib/philiprehberger/circuit_board/check.rb', line 7

def cache
  @cache
end

#criticalObject (readonly)

Returns the value of attribute critical.



7
8
9
# File 'lib/philiprehberger/circuit_board/check.rb', line 7

def critical
  @critical
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/philiprehberger/circuit_board/check.rb', line 7

def name
  @name
end

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

#callHash

Execute the health check, returning a cached result if available.

Returns:

  • (Hash)

    result with :name, :healthy, :duration, and optionally :error



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