Module: Philiprehberger::CircuitBoard

Defined in:
lib/philiprehberger/circuit_board.rb,
lib/philiprehberger/circuit_board/check.rb,
lib/philiprehberger/circuit_board/status.rb,
lib/philiprehberger/circuit_board/version.rb,
lib/philiprehberger/circuit_board/middleware.rb,
lib/philiprehberger/circuit_board/configuration.rb

Defined Under Namespace

Modules: Rack Classes: Check, Configuration, Error, Status

Constant Summary collapse

VERSION =
'0.6.0'

Class Method Summary collapse

Class Method Details

.check(parallel: false) ⇒ Status

Run all configured health checks and return an aggregated status.

Parameters:

  • parallel (Boolean) (defaults to: false)

    run checks concurrently in threads (default: false)

Returns:

  • (Status)

    the aggregated health status



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/philiprehberger/circuit_board.rb', line 29

def self.check(parallel: false)
  results = if parallel
              run_checks_parallel
            else
              @configuration.checks.map(&:call)
            end
  status = Status.new(results)

  new_status = status.to_h[:status]
  new_status_sym = new_status.to_sym
  if @configuration.on_change_callback && @previous_status && @previous_status != new_status_sym
    @configuration.on_change_callback.call(@previous_status, new_status_sym)
  end
  @previous_status = new_status_sym

  status
end

.check_one(name) ⇒ Hash

Run a single named health check and return its result hash.

Parameters:

  • name (Symbol, String)

    the name of the check to run

Returns:

  • (Hash)

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

Raises:

  • (Error)

    if no check with the given name exists



52
53
54
55
56
57
# File 'lib/philiprehberger/circuit_board.rb', line 52

def self.check_one(name)
  check_obj = @configuration.checks.find { |c| c.name.to_s == name.to_s }
  raise Error, "unknown check: #{name}" unless check_obj

  check_obj.call
end

.configure {|Configuration| ... } ⇒ void

This method returns an undefined value.

Configure health checks using a DSL block.

Yields:



20
21
22
23
# File 'lib/philiprehberger/circuit_board.rb', line 20

def self.configure(&)
  @configuration = Configuration.new
  @configuration.instance_eval(&)
end

.reset!void

This method returns an undefined value.

Reset all configured checks.



70
71
72
73
# File 'lib/philiprehberger/circuit_board.rb', line 70

def self.reset!
  @configuration = Configuration.new
  @previous_status = nil
end