lib/waffle/transports/base.rb in waffle-0.4.0 vs lib/waffle/transports/base.rb in waffle-0.5.0

- old
+ new

@@ -1,20 +1,41 @@ module Waffle module Transports class Base - def initialize + attr_reader :config + def initialize config + @config = config connect! end + def publish flow = 'events', message = '' + publish_impl(flow, message) + rescue *connection_exceptions => e + reconnect && retry if ready_to_connect? + end + + def subscribe flow = '', &block + subscribe_impl(flow, &block) + rescue *connection_exceptions => e + until reconnect do + sleep(config.connection_attempt_timeout) + end + retry + end + + protected def ready_to_connect? - (Time.now - @last_connection_attempt) > Waffle.config.connection_attempt_timeout + (Time.now - @last_connection_attempt) > config.connection_attempt_timeout end def reconnect connect! end - protected + def encoder + @encoder ||= "Waffle::Encoders::#{config.encoder.camelize}".constantize + end + def connect! @last_connection_attempt = Time.now do_connect rescue false