bolshakov / stoplight

:traffic_light: Traffic control for code.
http://bolshakov.github.io/stoplight/
MIT License
384 stars 40 forks source link

Notifier never being called after color change #105

Closed luizkowalski closed 8 years ago

luizkowalski commented 8 years ago

I have a circuit like this

  def stoplight(name, exception_message = DEFAULT_MESSSAGE, &block)
    raise if name.nil?
    raise unless block_given?
    stoplight = Stoplight(name, &block)
                .with_error_handler do |error, handle|
                  raise error if WHITELIST_EXCEPTIONS.include?(error.class)
                  handle.call(error)
                end
                .with_cool_off_time(15)
                .with_fallback { raise Errors::ExternalServiceError, exception_message }
    stoplight.run

that I use for different purpose (e.g. call facebook api, google api). I also want to expose the status to our health monitor. So that I decide to create a new notifier that will save the color to redis everytime a status is changed

class LightObserver < Stoplight::Notifier::Base
  def notify(light, _from_color, to_color, _error)
    REDIS.set("#{light.name}_color", to_color)
  end
end

...

Stoplight::Light.default_notifiers += [LightObserver.new]

when the status changed to red, LightObserver is invoked, but when change to yellow, nothing happen. I also didn't noticed the status going back to green ever.

tfausak commented 8 years ago

Notifiers are not called when the light transitions from red to yellow. Notifiers are only called from green to red and red to green. See the Runnable module for the code.

You can query a stoplight directly for its current color. That means that instead of setting up a LightObserver, you could do this:

def stoplight_color(name)
  Stoplight(name) { nil }
    .with_cool_off_time(15) # Must be the same as in the `stoplight` method.
    .color
end
luizkowalski commented 8 years ago

you are right, I got it Thanks