Use retry instead of while loop for rescuing connection failure (quasor)

This commit is contained in:
Kyle Banker 2010-12-29 13:13:54 -05:00
parent b77b3fe1e6
commit 01c38eabdd
2 changed files with 17 additions and 23 deletions

View File

@ -38,18 +38,14 @@ every half second and time out after thirty seconds.
# Ensure retry upon failure
def rescue_connection_failure(max_retries=60)
success = false
retries = 0
while !success
begin
yield
success = true
rescue Mongo::ConnectionFailure => ex
retries += 1
raise ex if retries >= max_retries
sleep(0.5)
end
end
retries = 0
begin
yield
rescue Mongo::ConnectionFailure => ex
retries += 1
raise ex if retries > max_retries
sleep(0.5)
retry
end
end

View File

@ -12,17 +12,15 @@ class Test::Unit::TestCase
# Generic code for rescuing connection failures and retrying operations.
# This could be combined with some timeout functionality.
def rescue_connection_failure(max_retries=60)
success = false
tries = 0
while !success && tries < max_retries
begin
yield
success = true
rescue Mongo::ConnectionFailure
puts "Rescue attempt #{tries}\n"
tries += 1
sleep(1)
end
retries = 0
begin
yield
rescue Mongo::ConnectionFailure => ex
puts "Rescue attempt #{retries}"
retries += 1
raise ex if retries > max_retries
sleep(1)
retry
end
end