如何将 Hoptoad 与 DelayedJob 和 DaemonSpawn 集成?

发布于 2024-08-02 01:47:21 字数 575 浏览 8 评论 0原文

我一直很高兴地使用 DelayedJob 习惯用法:

foo.send_later(:bar)

这在 DelayedJob 进程中调用对象 foo 上的方法 bar。

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

但是...如果 foo 抛出异常 Hoptoad 不会捕获它。

这是这些软件包中的一个错误吗...或者我是否需要更改某些配置...或者我是否需要在 DS 或 DJ 中插入一些异常处理来调用 Hoptoad 通知程序?


回应下面的第一条评论。

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

I have been happily using the DelayedJob idiom:

foo.send_later(:bar)

This calls the method bar on the object foo in the DelayedJob process.

And I've been using DaemonSpawn to kick off the DelayedJob process on my server.

But... if foo throws an exception Hoptoad doesn't catch it.

Is this a bug in any of these packages... or do I need to change some configuration... or do I need to insert some exception handling in DS or DJ that will call the Hoptoad notifier?


In response to the first comment below.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

完美的未来在梦里 2024-08-09 01:47:21

尝试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应用程序中)

Try 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

This worked for me.

(in a Rails 3.0.10 app using delayed_job 2.1.4 and hoptoad_notifier 2.4.11)

撞了怀 2024-08-09 01:47:21

查看 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

Check out the source for Delayed::Job... there's a snippet like:

# 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

I haven't tried it, but I think you could do something like:

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
↘紸啶 2024-08-09 01:47:21

Hoptoad 使用 Rails rescue_action_in_public 挂钩方法来拦截异常并记录它们。 仅当 Rails 控制器分派请求时才会执行此方法。
因此,Hoptoad 完全不知道生成的任何异常,例如由 rake 任务或 Rails 脚本/运行程序生成的异常。

如果您想让 Hoptoad 跟踪您的异常,您应该手动集成它。
这应该是非常简单的。 以下代码片段演示了如何调用 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 可能会抱怨。

Hoptoad uses the Rails rescue_action_in_public hook method to intercept exceptions and log them. This method is only executed when the request is dispatched by a Rails controller.
For this reason, Hoptoad is completely unaware of any exception generated, for example, by rake tasks or the rails script/runner.

If you want to have Hoptoad tracking your exception, you should manually integrate it.
It should be quite straightforward. The following code fragment demonstrates how Hoptoad is invoked

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

Just include Hoptoad library in your environment and call notify_hoptoad(exception) should work. Make sure your environment provides the same API of a Rails controller or Hoptoad might complain.

﹏半生如梦愿梦如真 2024-08-09 01:47:21

只是把它扔在那里 - 你的守护进程应该需要你正在处理的 Rails 环境。 它看起来应该类似于:

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

通过这种方式,您可以指定调用守护程序的环境。

因为它运行延迟作业,所以守护进程已经这样做了(它需要 activerecord),但也许您只需要最少的 activerecord 即可在没有rails的情况下使delayed_job满意。

Just throwing it out there - your daemon should require the rails environment that you're working on. It should look something along the lines of:

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

This way you can specify environment in which daemon is called.

Since it runs delayed job chances are daemon already does that (it needs activerecord), but maybe you're only requiring minimal activerecord to make delayed_job happy without rails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文