如何将Hoptoad与DelayedJob和DaemonSpawn集成?

我一直很高兴使用DelayedJob成语:

foo.send_later(:bar) 

这将调用DelayedJob进程中对象foo上的方法栏。

我一直在使用DaemonSpawn在我的服务器上启动DelayedJob进程。

但是……如果foo抛出exception,Hoptoad就无法捕获它。

这是任何这些软件包中的错误……或者我是否需要更改某些配置…或者我是否需要在DS或DJ中插入一些将调用Hoptoad通知程序的exception处理?


回应下面的第一条评论。

 class DelayedJobWorker < DaemonSpawn::Base def start(args) ENV['RAILS_ENV'] ||= args.first || 'development' Dir.chdir RAILS_ROOT require File.join('config', 'environment') Delayed::Worker.new.start end 

尝试monkeypatching Delayed :: Worker#handle_failed_job:

 # lib/delayed_job_airbrake.rb module Delayed class Worker protected def handle_failed_job_with_airbrake(job, error) say "Delayed job failed -- logging to Airbrake" HoptoadNotifier.notify(error) handle_failed_job_without_airbrake(job, error) end alias_method_chain :handle_failed_job, :airbrake end end 

这对我有用。

(在使用delayed_job 2.1.4和hoptoad_notifier 2.4.11的Rails 3.0.10应用程序中)

查看Delayed :: Job的来源…有一个代码片段:

 # This is a good hook if you need to report job processing errors in additional or different ways def log_exception(error) logger.error "* [JOB] #{name} failed with #{error.class.name}: #{error.message} - #{attempts} failed attempts" logger.error(error) end 

我没试过,但我认为你可以这样做:

 class Delayed::Job def log_exception_with_hoptoad(error) log_exception_without_hoptoad(error) HoptoadNotifier.notify(error) end alias_method_chain :log_exception, :hoptoad end 

Hoptoad使用Rails rescue_action_in_public钩子方法拦截exception并记录它们。 只有在Rails控制器调度请求时才会执行此方法。 出于这个原因,Hoptoad完全没有意识到任何exception,例如,rake任务或rails script / runner。

如果您想让Hoptoad跟踪您的exception,您应该手动集成它。 它应该非常简单。 以下代码片段演示了如何调用Hoptoad

 def rescue_action_in_public_with_hoptoad exception notify_hoptoad(exception) unless ignore?(exception) || ignore_user_agent? rescue_action_in_public_without_hoptoad(exception) end 

只需在您的环境中包含Hoptoad库,并调用notify_hoptoad(exception) 。 确保您的环境提供与Rails控制器相同的API,或者Hoptoad可能会抱怨。

把它扔出去 – 你的守护进程应该需要你正在处理的rails环境。 它看起来应该是这样的:

 RAILS_ENV = ARGV.first || ENV['RAILS_ENV'] || 'production' require File.join('config', 'environment') 

这样,您可以指定调用守护程序的环境。

由于它运行延迟的工作机会是守护进程已经做到了(它需要activerecord),但也许你只需要最小的activerecord来使delayed_job快乐而没有rails。

Interesting Posts