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

Issue #487: Automatically add Rollbar::ActiveJob to ActionMailer::DeliveryJob #819

Merged
merged 5 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/rollbar/plugins/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def self.included(base)
end
end
end

# Automatically add to ActionMailer::DeliveryJob
ActionMailer::DeliveryJob.send(:include, Rollbar::ActiveJob) if defined?(ActionMailer::DeliveryJob)
7 changes: 6 additions & 1 deletion spec/rollbar/delay/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
require 'rollbar/delay/active_job'

describe Rollbar::Delay::ActiveJob do
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper) # rubocop:disable Style/MixinUsage

describe '.call' do
let(:payload) { {} }
it 'calls Rollbar' do
expect(Rollbar).to receive(:process_from_async_handler).with(payload)
Rollbar::Delay::ActiveJob.call(payload)

perform_enqueued_jobs do
Rollbar::Delay::ActiveJob.call(payload)
end
end
end
end
Expand Down
108 changes: 66 additions & 42 deletions spec/rollbar/plugins/active_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,75 @@

require 'active_support/rescuable'

if Gem::Version.new(Rails.version) >= Gem::Version.new('4.2.0')
context 'using rails4.2 and up' do
describe Rollbar::ActiveJob do
class TestJob
# To mix in rescue_from
include ActiveSupport::Rescuable
include Rollbar::ActiveJob

attr_reader :job_id
attr_accessor :arguments

def initialize(*arguments)
@arguments = arguments
end

def perform(exception, job_id)
@job_id = job_id
# ActiveJob calls rescue_with_handler when a job raises an exception
rescue_with_handler(exception) || raise(exception)
end
end
describe Rollbar::ActiveJob do
class TestJob
# To mix in rescue_from
include ActiveSupport::Rescuable
include Rollbar::ActiveJob

attr_reader :job_id
attr_accessor :arguments

def initialize(*arguments)
@arguments = arguments
end

def perform(exception, job_id)
@job_id = job_id
# ActiveJob calls rescue_with_handler when a job raises an exception
rescue_with_handler(exception) || raise(exception)
end
end

before { reconfigure_notifier }

let(:exception) { StandardError.new('oh no') }
let(:job_id) { '123' }
let(:argument) { 12 }

before { reconfigure_notifier }

let(:exception) { StandardError.new('oh no') }
let(:job_id) { '123' }
let(:argument) { 12 }

it 'reports the error to Rollbar' do
expected_params = {
:job => 'TestJob',
:job_id => job_id,
:use_exception_level_filters => true,
:arguments => [argument]
}
expect(Rollbar).to receive(:error).with(exception, expected_params)
begin
TestJob.new(argument).perform(exception, job_id)
rescue StandardError
nil
end
it 'reports the error to Rollbar' do
expected_params = {
:job => 'TestJob',
:job_id => job_id,
:use_exception_level_filters => true,
:arguments => [argument]
}
expect(Rollbar).to receive(:error).with(exception, expected_params)
TestJob.new(argument).perform(exception, job_id) rescue nil # rubocop:disable Style/RescueModifier
end

it 'reraises the error so the job backend can handle the failure and retry' do
expect { TestJob.new(argument).perform(exception, job_id) }.to raise_error exception
end

context 'using ActionMailer::DeliveryJob', :if => defined?(ActionMailer::DeliveryJob) do
include ActiveJob::TestHelper if defined?(ActiveJob::TestHelper) # rubocop:disable Style/MixinUsage

class TestMailer < ActionMailer::Base
attr_accessor :arguments

def test_email(*_arguments)
error = StandardError.new('oh no')
raise(error)
end
end

it 'job is created' do
ActiveJob::Base.queue_adapter = :test
expect do
TestMailer.test_email(argument).deliver_later
end.to have_enqueued_job.on_queue('mailers')
end

it 'reraises the error so the job backend can handle the failure and retry' do
expect { TestJob.new(argument).perform(exception, job_id) }.to raise_error exception
it 'reports the error to Rollbar' do
expected_params = {
:job => 'ActionMailer::DeliveryJob',
:use_exception_level_filters => true,
:arguments => ['TestMailer', 'test_email', 'deliver_now', 12]
}
expect(Rollbar).to receive(:error).with(kind_of(StandardError), hash_including(expected_params))
perform_enqueued_jobs do
TestMailer.test_email(argument).deliver_later rescue nil # rubocop:disable Style/RescueModifier
end
end
end
Expand Down