Ruby Retry Retry is used primarily in the context of exception handling in Ruby. When your program encounters an exception inside a begin block, control moves to the rescue block where the exception is handled. This is conventionally followed by the execution of the code below the rescue block.
In Ruby, Redo statement is used to repeat the current iteration of the loop. redo always used inside the loop. The redo statement restarts the loop without evaluating the condition again. In above example, redo statement apply when x = 15.
Reraising Exceptions If you call raise with no arguments, while inside of a rescue block, Ruby will re-raise the original rescued exception.
The method definition itself does the work of begin , so you can omit it. You can also do this with blocks. Now, there is one more way to use the rescue keyword without begin . Let's see how that works.
I have the following code in my script...
begin
#Loop to create 1000 emails...
#Loop to send 1000 emails...
rescue Timeout::Error => e
retry_attempts += 1
if retry_attempts < 10
retry
else
puts "Timeout error, deleting emails...".red
logs.puts("Rescued a timeout error...#{e}")
email_ids_all.each do |email_delete|
#delete all email...
end
My question is what retry
is actually going to "retry". If the script has already generated 1000 emails in one loop and sent 999 of them in another loop, and then it times out on sending the 1000th email- Will it retry the specific line of code it encountered the error on, will it start the loop over with the 1000th email, will it start the entire loop over, or will it start at the beginning of the script running through both loops?
I am using ruby 1.9.3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With