Twitter Gem - 需要考虑救援吗?

发布于 2024-11-16 13:53:09 字数 588 浏览 5 评论 0原文

我正在使用 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 

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

I'm working with the Twitter Gem and I've created a long running ruby task. I would like it to be able to handle common errors so I'm looking to build a list of those I should consider to protect against (for example the fail whale 500)

Here is the begin/end loop my code functions in:

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 

Can you think of any other Errors to protect and retry against? Is this a well structured way to handle errors? What are some design implementations I should consider?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦爱 2024-11-23 13:53:09

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

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

Consider having a catch-all exception handler at the end that logs what kind of exception was encountered and re-raises it. Your script may fail the first time, but at least you'll find out why.

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
乱了心跳 2024-11-23 13:53:09

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

I also catch the Twitter::Forbidden error in my code that uses the Twitter gem.

只是我以为 2024-11-23 13:53:09

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

Alternatively, you could try rescue SystemCallError, since all Errno errors are subclasses of that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文