如何解决超时问题(Ruby、Rails) [英] How to rescue timeout issues (Ruby, Rails)
问题描述
我的大多数应用都与网络服务有很大关系,而且经常由于第三方网站而导致超时问题.
most of my apps have a lot to do with web services and often due to the third party site, I get timeout issues.
这是我得到的错误:
execution expired
/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill'
如何在 Rails 应用中挽救这种错误?
How do I rescue this kind of error in a rails app?
推荐答案
根据您使用库的方式,有不同的方法可以挽救异常.
Depending on how you use the library, there are different ways to rescue the exception.
在图书馆
假设您创建了一个包装器来访问某种 Web 服务,您可以让包装器挽救异常并始终返回安全"数据.
Assuming you created a wrapper to access some kind of web service, you can have the wrapper rescue the exception and always return a "safe" data.
在行动
如果在action中调用了特定的方法,并且方法成功是action的要求,那么就可以在action中进行rescue.在下面的示例中,我修复了错误并显示了一个特定的模板来处理该问题.
If you call a specific method in the action and the method success is a requirement for the action, then you can rescue it in the action. In the following example I rescue the error and show a specific template to handle the problem.
def action
perform_external_call
rescue Timeout::Error => e
@error = e
render :action => "error"
end
在控制器中
如果方法调用可以发生在许多不同的操作中,您可能需要使用 rescue_from
.
If the method call can occur in many different actions, you might want to use rescue_from
.
class TheController < ApplicationController
rescue_from Timeout::Error, :with => :rescue_from_timeout
protected
def rescue_from_timeout(exception)
# code to handle the issue
end
end
这篇关于如何解决超时问题(Ruby、Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!