Use retry instead of while loop for rescuing connection failure (quasor)
This commit is contained in:
parent
b77b3fe1e6
commit
01c38eabdd
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in New Issue