Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add --retry-unless-2xx.

  • Loading branch information...
commit 9076398b6221b64a71d63b62c9d7c92afaa311ba 1 parent d395780
@neglectedvalue authored
View
5 bin/yupd
@@ -22,6 +22,7 @@ Options:
Available log levels: fatal, error, warn, info, debug.
--persistent <path> Use persistent queue.
--timeout <seconds> Timeout for answer from FORWARD_TO_HOST (default 60.0)
+ --retry-unless-2xx Retry even if status code not 2xx.
Examples:
yupd --listen 0.0.0.0:8081 --status-code 201 errbit.host.somewhere
@@ -45,6 +46,7 @@ opts = GetoptLong.new(*[['--help', '-h', GetoptLong::NO_ARGUMENT],
['--loglevel', GetoptLong::REQUIRED_ARGUMENT],
['--persistent', GetoptLong::REQUIRED_ARGUMENT],
['--timeout', GetoptLong::REQUIRED_ARGUMENT],
+ ['--retry-unless-2xx', GetoptLong::NO_ARGUMENT],
])
config = {:loglevel => LEVELS["fatal"]}
@@ -73,6 +75,8 @@ opts.each do |opt, arg|
config[:persistent] = arg || "/tmp/queue"
when '--timeout'
config[:timeout] = arg.to_f
+ when '--retry-unless-2xx'
+ config[:retry_unless_2xx] = true
end
end
@@ -87,6 +91,7 @@ config[:forward_to] = ARGV.shift
Yup.logger.level = config[:loglevel]
Yup.watermark = config[:watermark] if config.has_key?(:watermark)
Yup.resend_delay = config[:resend_delay] if config.has_key?(:resend_delay)
+Yup.retry_unless_2xx = config[:retry_unless_2xx] if config.has_key?(:retry_unless_2xx)
if config.has_key?(:persistent)
Yup.run_with_state(config)
View
4 lib/yup.rb
@@ -21,6 +21,10 @@ def self.watermark=(seconds); @@watermark = seconds end
def self.logger; @@logger end
def self.logger=(logger); @@logger = logger end
+ @@retry_unless_2xx = false
+ def self.retry_unless_2xx; @@retry_unless_2xx end
+ def self.retry_unless_2xx=(bool); @@retry_unless_2xx = bool end
+
def self.run(config)
host = config[:listen_host] || 'localhost'
port = config[:listen_port] || 8080
View
18 lib/yup/request_forwarder.rb
@@ -26,7 +26,7 @@ def perform
:head => @headers,
:body => @body)
- @logger.progname = "Yup::RequestForwarder (##{http.__id__.to_s(36)} received at #{Time.now.to_s})"
+ @logger.progname = "Yup::RequestForwarder (##{self.__id__.to_s(36)} received at #{Time.now.to_s})"
http.callback do
Yup.watermark += 1
@@ -36,7 +36,12 @@ def perform
@logger.info "Success"
else
log_response(http)
- @logger.info "Fail; will not retry"
+ if Yup.retry_unless_2xx
+ @logger.info "Fail: got status code #{http.response_header.status}; will retry after #{Yup.resend_delay} seconds"
+ EventMachine.add_timer(Yup.resend_delay, &self.method(:retry))
+ else
+ @logger.info "Fail; will not retry"
+ end
end
end
@@ -114,7 +119,14 @@ def make_request(req)
@logger.info "Success"
else
log_response(raw_response, response_body, http)
- @logger.info "Fail; will not retry"
+ if Yup.retry_unless_2xx
+ @logger.info "Fail: got status code #{http.status_code}; will retry after #{Yup.resend_delay} seconds"
+ @state.to_feedback(Yajl::Encoder.encode([@http_method.downcase, @request_url, headers, body]))
+
+ sleep Yup.resend_delay
+ else
+ @logger.info "Fail; will not retry"
+ end
end
rescue Exception, Timeout::Error => e
View
10 test/test_persistence_yup.rb
@@ -31,13 +31,16 @@ def on_message_complete
case $attempts
when 0
when 1
+ send_data "HTTP/1.1 400 OK\r\nServer: test\r\n\r\n"
+ close_connection_after_writing
+ when 2
send_data "HTTP/1.1 200 OK\r\nServer: test\r\n\r\n"
close_connection_after_writing
end
end
def unbind
- if $attempts > 0
+ if $attempts >= 2
EM.add_timer(1) do
Process.kill("KILL", $pid)
EM.stop_event_loop()
@@ -75,7 +78,8 @@ def test_request_handler
state = Yup::State.new(dbpath, forward_to, feedback_channel)
timeout = 1
- Yup.resend_delay = 1
+ Yup.resend_delay = 1
+ Yup.retry_unless_2xx = true
$pid = Process.fork do
Yup::State::RequestForwarder.new(state, forward_to, timeout).run_loop
@@ -89,7 +93,7 @@ def test_request_handler
}
assert $client_parser
- assert_equal 200, $client_parser.status_code
+ assert_equal 200, $client_parser.status_code
assert_equal "yupd", $client_parser.headers["Server"]
assert $service_parser
assert_equal "/foo", $service_parser.request_url
View
8 test/test_yup.rb
@@ -27,12 +27,15 @@ def on_message_complete
case $attempts
when 0
when 1
+ send_data "HTTP/1.1 400 OK\r\nServer: test\r\n\r\n"
+ when 2
send_data "HTTP/1.1 200 OK\r\nServer: test\r\n\r\n"
+ close_connection_after_writing
end
end
def unbind
- if $attempts > 0
+ if $attempts >= 2
EM.next_tick { EM.stop_event_loop }
end
end
@@ -64,7 +67,8 @@ def test_request_handler
state = nil
timeout = 1
- Yup.resend_delay = 1
+ Yup.resend_delay = 1
+ Yup.retry_unless_2xx = true
EM.run {
EM.start_server("127.0.0.1", 16785, Service)
Please sign in to comment.
Something went wrong with that request. Please try again.