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