2016-07-21 39 views
-1

我正在构建一个Sinatra API调用,它将触发子进程中的长时间运行操作。我使用的是exception_handler宝石,但不明白我如何在分叉过程中使用它。处理分叉进程中的异常

西纳特拉应用:

require 'sinatra' 
require 'rubygems' 
require 'bundler/setup' 
require 'exception_notification' 

use ExceptionNotification::Rack, 
    :email => { 
    :email_prefix => "[Example] ", 
    :sender_address => %{"notifier" <[email protected]>}, 
    :exception_recipients => %w{[email protected]}, 
    :delivery_method => :sendmail 
    } 

get '/error' do 
    raise 'Bad!' # Notification gets sent 
end 

get '/error_async' do 
    p1 = fork do 
    sleep 10 
    raise 'Bad! (async)' # Notification never gets sent 
    end 
    Process.detach(p1) 
end 

回答

1

得到它的工作,每docs

get '/error_async' do 

    p1 = fork do 

    begin 
     sleep 10 
     raise 'Bad! (async)' 
    rescue Exception => e 
     ExceptionNotifier.notify_exception(e) 
    end 

    end 
    Process.detach(p1) 

end 
+0

哇,如果你需要明确写入'ExceptionNotifier.notify_exception'发送通知,然后是创业板基本是没用的。像这样的宝石的真正好处是,即使你不知道你的产品Sinatra/Rails应用程序在哪里抛出异常,你会得到通知,因为你没有预料到它(如果你确实这样做了,开始/ rescue')。 –

+0

@Mike你只是将正在被淘汰的代码包装到后台进程中 - 我认为这实际上是有道理的。 – Yarin

+0

确实,您可以为新线程中的所有代码做一个搜索,看起来有点便宜,但它可以完成工作。 –