module Stoplight::Light::Runnable

Public Instance Methods

color() click to toggle source

@return [String]

# File lib/stoplight/light/runnable.rb, line 7
def color
  failures, state = failures_and_state
  failure = failures.first

  if state == State::LOCKED_GREEN then Color::GREEN
  elsif state == State::LOCKED_RED then Color::RED
  elsif failures.size < threshold then Color::GREEN
  elsif failure && Time.now - failure.time >= cool_off_time
    Color::YELLOW
  else Color::RED
  end
end
run() click to toggle source

@raise [Error::RedLight]

# File lib/stoplight/light/runnable.rb, line 21
def run
  case color
  when Color::GREEN then run_green
  when Color::YELLOW then run_yellow
  else run_red
  end
end

Private Instance Methods

clear_failures() click to toggle source
# File lib/stoplight/light/runnable.rb, line 67
def clear_failures
  safely([]) { data_store.clear_failures(self) }
end
failures_and_state() click to toggle source
# File lib/stoplight/light/runnable.rb, line 71
def failures_and_state
  safely([[], State::UNLOCKED]) { data_store.get_all(self) }
end
handle_error(error, on_failure) click to toggle source
# File lib/stoplight/light/runnable.rb, line 59
def handle_error(error, on_failure)
  error_handler.call(error, Error::HANDLER)
  size = record_failure(error)
  on_failure.call(size, error) if on_failure
  raise error unless fallback
  fallback.call(error)
end
notify(from_color, to_color, error = nil) click to toggle source
# File lib/stoplight/light/runnable.rb, line 75
def notify(from_color, to_color, error = nil)
  notifiers.each do |notifier|
    safely { notifier.notify(self, from_color, to_color, error) }
  end
end
record_failure(error) click to toggle source
# File lib/stoplight/light/runnable.rb, line 81
def record_failure(error)
  failure = Failure.from_error(error)
  safely(0) { data_store.record_failure(self, failure) }
end
run_code(on_success, on_failure) click to toggle source
# File lib/stoplight/light/runnable.rb, line 50
def run_code(on_success, on_failure)
  result = code.call
  failures = clear_failures
  on_success.call(failures) if on_success
  result
rescue Exception => error # rubocop:disable Lint/RescueException
  handle_error(error, on_failure)
end
run_green() click to toggle source
# File lib/stoplight/light/runnable.rb, line 31
def run_green
  on_failure = lambda do |size, error|
    notify(Color::GREEN, Color::RED, error) if size == threshold
  end
  run_code(nil, on_failure)
end
run_red() click to toggle source
# File lib/stoplight/light/runnable.rb, line 45
def run_red
  raise Error::RedLight, name unless fallback
  fallback.call(nil)
end
run_yellow() click to toggle source
# File lib/stoplight/light/runnable.rb, line 38
def run_yellow
  on_success = lambda do |failures|
    notify(Color::RED, Color::GREEN) unless failures.empty?
  end
  run_code(on_success, nil)
end
safely(default = nil) { || ... } click to toggle source
# File lib/stoplight/light/runnable.rb, line 86
def safely(default = nil, &code)
  return yield if data_store == Default::DATA_STORE

  self
    .class
    .new("#{name}-safely", &code)
    .with_data_store(Default::DATA_STORE)
    .with_fallback do |error|
      error_notifier.call(error) if error
      default
    end
    .run
end