Ruby抢救并重试特定代码块 [英] Ruby rescue and retry specific code block

查看:80
本文介绍了Ruby抢救并重试特定代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中包含以下代码...

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

我的问题是retry实际上将要重试".如果脚本已经在一个循环中生成了1000封电子邮件,并在另一个循环中发送了999封电子邮件,然后在发送第1000封电子邮件时超时-它将重试遇到错误的特定代码行,是否将启动循环用第1000封电子邮件结束,它会重新开始整个循环,还是在两个循环中运行的脚本的开头开始?

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?

我正在使用ruby 1.9.3.

I am using ruby 1.9.3.

推荐答案

retry将执行 entire begin块,因此对于您来说 all 循环将再次运行.

retry will execute the entire begin block, so in your case all the email loops will run again.

这是一个简单的示例,它将连续打印1到7的整数(以CTRL-C终止,因为它将无限循环):

Here's a quick example, which will print integers 1 through 7 continuously (terminate with CTRL-C, as it will infinite loop):

begin
  (1..10).each do |x|
    puts x
    if x > 6
      STDIN.gets # press enter to do another iteration
      raise ArgumentException
    end
  end
rescue
  retry # loop will restart from 1
end

这篇关于Ruby抢救并重试特定代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆