无法在Rails中捕获独角兽超时异常

时间:2020-07-10 15:21:53

标签: ruby-on-rails ruby ruby-on-rails-3 heroku

这是示例代码,我正尝试处理来自独角兽的异常。

unicron.rb

worker_processes Integer(ENV['WEB_CONCURRENCY'] || 3)
timeout 15
preload_app true

timeout.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

我的睡眠时间为12.3秒,code=H13 desc="Connection closed without response"发生了,事务回滚发生了,但是这些异常都没有执行。我在这里添加了几个例外。这里有什么问题吗?。

1 个答案:

答案 0 :(得分:0)

救援按顺序执行。因此,您的第一个急救程序将捕获正在提出的所有内容,并且代码永远都不会超过该点。

尝试放在最后:

 # 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

相关问题