【问题标题】:Twitter Gem - rescue's to consider?Twitter Gem - 需要考虑救援吗?
【发布时间】:2011-09-18 22:22:24
【问题描述】:

我正在使用 Twitter Gem 并创建了一个长时间运行的 ruby​​ 任务。我希望它能够处理常见错误,因此我希望建立一个我应该考虑防止的错误列表(例如失败的鲸鱼 500)

这是我的代码在其中运行的开始/结束循环:

Begin

# My (omitted) very long ruby task
# filled with Twitter API requests

rescue Errno::ENOENT
  sleep(5)
  logger.info "ENOENT error - attempting to retry"
  retry
rescue Errno::ETIMEDOUT
  sleep(5)
  logger.info " Operation timed out - attempting to retry"
  retry
rescue Errno::ECONNRESET
  sleep(5)
  logger.info "Connection reset by peer - attempting to retry"
  retry
end 

你能想到任何其他需要保护和重试的错误吗?这是一种处理错误的结构良好的方法吗?我应该考虑哪些设计实现?

【问题讨论】:

    标签: ruby twitter rescue


    【解决方案1】:

    考虑在最后设置一个包罗万象的异常处理程序,记录遇到的异常类型并重新引发它。您的脚本第一次可能会失败,但至少您会找出原因。

    begin
    
    # My (omitted) very long ruby task
    # filled with Twitter API requests
    
    rescue Errno::ENOENT
      sleep(5)
      logger.info "ENOENT error - attempting to retry"
      retry
    rescue Errno::ETIMEDOUT
      sleep(5)
      logger.info " Operation timed out - attempting to retry"
      retry
    rescue Errno::ECONNRESET
      sleep(5)
      logger.info "Connection reset by peer - attempting to retry"
      retry
    rescue # This rescues StandardError and its children
      sleep(5)
      # The next line is somewhat pseudocode, because I don't use logger
      logger.this_is_somewhat_bad "Somewhat bad exception #{$!.class} #{$!} happened - I'm giving up"
      raise
    rescue Exception
      sleep(5)
      # The next line is somewhat pseudocode, because I don't use logger
      logger.omg_wtf_bbq "Really bad exception #{$!.class} #{$!} happened - I'm giving up"
      raise
    end
    

    【讨论】:

      【解决方案2】:

      我还在使用 Twitter gem 的代码中发现了 Twitter::Forbidden 错误。

      【讨论】:

      • 谢谢。在尝试重试之前你是否让你的进程休眠?如果是这样,对于 Twitter::Forbidden 来说需要多长时间?
      • 我有一个每 5 分钟运行一次的 cron 任务。如果我收到 Twitter::Forbidden,它将在 5 分钟后再次运行 cron 作业。
      【解决方案3】:

      或者,您可以尝试rescue SystemCallError,因为所有Errno 错误都是它的子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        相关资源
        最近更新 更多