class LitmusPaper::Health

Attributes

forced_reason[R]
summary[R]

Public Class Methods

new(forced = :none, forced_reason = "") click to toggle source
# File lib/litmus_paper/health.rb, line 6
def initialize(forced = :none, forced_reason = "")
  @value = 0
  @dependencies_available = true
  @summary = ""
  @forced = forced
  @forced_reason = forced_reason
end

Public Instance Methods

direction() click to toggle source
# File lib/litmus_paper/health.rb, line 22
def direction
  @forced
end
ensure(dependency) click to toggle source
# File lib/litmus_paper/health.rb, line 58
def ensure(dependency)
  available = dependency.available?

  @dependencies_available &&= available
  @summary << "#{dependency}: #{available ? 'OK' : 'FAIL'}\n"
end
forced?() click to toggle source
# File lib/litmus_paper/health.rb, line 18
def forced?
  @forced != :none
end
measured_health() click to toggle source
# File lib/litmus_paper/health.rb, line 46
def measured_health
  return 0 unless @dependencies_available
  @value
end
ok?() click to toggle source
# File lib/litmus_paper/health.rb, line 14
def ok?
  value > 0
end
perform(metric) click to toggle source
# File lib/litmus_paper/health.rb, line 51
def perform(metric)
  health = metric.current_health.ceil

  @value += health
  @summary << "#{metric}: #{health}\n"
end
value() click to toggle source
# File lib/litmus_paper/health.rb, line 26
def value
  if forced?
    return case @forced
    when :up
      100
    when :down
      0
    when :health
      forced_health = @forced_reason.split("\n").last.to_i

      # This could potentially be argued differently, but I feel like forcing
      # a health value != forcing up - if the measured health is less than the
      # forced health, we should return the measured health.
      measured_health < forced_health ? measured_health : forced_health
    end
  end

  measured_health
end