How do i integrate Hoptoad with DelayedJob and DaemonSpawn?

后端 未结 4 1797
渐次进展
渐次进展 2021-02-06 13:13

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.

相关标签:
4条回答
  • 2021-02-06 13:21

    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.

    0 讨论(0)
  • 2021-02-06 13:30

    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
    
    0 讨论(0)
  • 2021-02-06 13:34

    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)

    0 讨论(0)
  • 2021-02-06 13:44

    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.

    0 讨论(0)
提交回复
热议问题