Skip to content

Commit

Permalink
feat: track exceptions in :async activejob adapter (#503)
Browse files Browse the repository at this point in the history
* WIP: Allow for exception tracking on async adapter.

* Using perform_around callback to catch exceptions for ActiveJob

* Restructure test for older rails versions

* Make sure ActiveJob::TestHelper is only included when Rails is there

* Test with around filter

* Remove unnecessary test controller

* call Honeybadger.clear! before executing job
  • Loading branch information
halfbyte authored Dec 4, 2023
1 parent 418cbf7 commit 9a6e2ec
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/honeybadger/plugins/active_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Honeybadger
module Plugins
module ActiveJob

Plugin.register {
requirement { defined?(::Rails.application) && ::Rails.application }
requirement {
::Rails.application.config.active_job[:queue_adapter] == :async
}

execution {
::ActiveJob::Base.class_eval do |base|
base.set_callback :perform, :around do |param, block|
Honeybadger.clear!
begin
block.call
rescue => error
Honeybadger.notify(error, parameters: { job_arguments: self.arguments })
raise error
end
end
end
}
}
end
end

end
3 changes: 3 additions & 0 deletions spec/fixtures/rails/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class RailsApp < Rails::Application
config.serve_static_files = false
config.consider_all_requests_local = false

config.active_job.queue_adapter = :async

routes.append do
get '/runtime_error', :to => 'rails#runtime_error'
get '/record_not_found', :to => 'rails#record_not_found'
Expand Down Expand Up @@ -62,3 +64,4 @@ def index
Rails.logger = Logger.new(File::NULL)

require_relative './breadcrumbs'
require_relative './queue_adapter'
13 changes: 13 additions & 0 deletions spec/fixtures/rails/config/queue_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ErrorJob < ActiveJob::Base
around_perform :around_for_testing

def perform(opts={})
raise "exception raised in job"
end

def around_for_testing(*args)
yield
end
end


20 changes: 20 additions & 0 deletions spec/integration/rails/async_queue_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require_relative '../rails_helper'

describe "Rails Async Queue Adapter Test", if: RAILS_PRESENT, type: :request do
include ActiveJob::TestHelper if RAILS_PRESENT
load_rails_hooks(self)

it "reports exceptions" do
Honeybadger.flush do
perform_enqueued_jobs do
expect {
ErrorJob.perform_later({some: 'data'})
}.to raise_error(StandardError)
end
end

expect(Honeybadger::Backend::Test.notifications[:notices].size).to eq(1)
expect(Honeybadger::Backend::Test.notifications[:notices][0].params[:job_arguments][0]).to eq({some: 'data'})
end

end

0 comments on commit 9a6e2ec

Please sign in to comment.