ruby-on-rails - 没有在 Rails 中捕获独角兽超时异常
问题描述
这是我试图处理来自独角兽的异常的示例代码。
unicron.rb
worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)
timeout 15
preload_app true
超时.rb
Rack::Timeout.timeout = 12
示例代码
def create_store
ActiveRecord::Base.transaction do
@store = Store.new(params[:store])
if @store.save!
sleep 12.3
InformUserWorker.perform_async(store_id)
end
end
rescue => exception
exception.backtrace.each { |trace| puts trace }
rescue Timeout::Error
puts 'That took too long, exiting...1'
rescue Rack::Timeout::RequestTimeoutException
puts 'That took too long, exiting...2'
rescue Rack::Timeout::RequestTimeoutError
puts 'That took too long, exiting...3'
rescue Rack::Timeout::RequestExpiryError
puts 'That took too long, exiting...4'
end
我code=H13 desc="Connection closed without response"
睡了 12.3 秒,事务回滚发生了,但是这些异常都没有执行。我在这里添加了几个例外。这里有什么问题吗?
解决方案
救援按顺序执行。因此,您的第一次救援捕获了正在引发的任何内容,并且代码永远不会超过那个点。
试着把它放在最后:
# other rescues in here.
rescue => exception
exception.backtrace.each { |trace| puts trace }
end # this one is the last, only executed if none of the others are
如果问题是您不确定正在引发哪个类,只需使用 pry 之类的东西来调试exception.class
推荐阅读
- objective-c - 点击时UITableViewCell高度问题
- shell - 如果前一行模式匹配,则附加到当前行
- java - Java Generic 不推断类型
- .net-core - 在 OS X 上启动 .NET Core 应用程序时出现 OnConnectionAsync 方法问题
- linux - 从 CSV 文件中获取条目并将其写入配置文件 freeRADIUS 的 Linux 脚本
- python - 计数值 Pandas 并与其他数据框结合
- c# - 始终获取主显示器 DPI
- ibm-cloud-private - 启用 \"va\" 服务时必须设置 \"va\" 主机组 - 安装错误
- php - 动态列表框 - 我的知识有很多空白
- drools - 在一个时间范围内检测一定数量的事件,但只触发一次