Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: track exceptions in :async activejob adapter #503

Merged
merged 9 commits into from
Dec 4, 2023
29 changes: 29 additions & 0 deletions lib/honeybadger/plugins/active_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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|
begin
joshuap marked this conversation as resolved.
Show resolved Hide resolved
Honeybadger.flush {
joshuap marked this conversation as resolved.
Show resolved Hide resolved
block.call
}
rescue => e
Honeybadger.notify(e, parameters: { job_arguments: self.arguments }, sync: true)
raise
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'
23 changes: 23 additions & 0 deletions spec/fixtures/rails/config/queue_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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

class ErrorJobController < ApplicationController
def enqueue_error_job
ErrorJob.perform_later({some: "data"})
head 200
end
end

Rails.application.routes.append do
post "/enqueue_error_job", to: "error_job#enqueue_error_job"
end

18 changes: 18 additions & 0 deletions spec/integration/rails/async_queue_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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
post "/enqueue_error_job"
joshuap marked this conversation as resolved.
Show resolved Hide resolved
end
expect(response.status).to eq(500)
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