From 88c5a04396a1fa035cf2480df5ba2936bec2ffac Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Tue, 12 Feb 2019 13:38:21 -0800 Subject: [PATCH 1/4] Automatically add Rollbar::ActiveJob to ActionMailer::DeliveryJob --- lib/rollbar/plugins/active_job.rb | 3 ++ spec/rollbar/plugins/active_job_spec.rb | 42 ++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/rollbar/plugins/active_job.rb b/lib/rollbar/plugins/active_job.rb index b90fc4d7..d34cc2e8 100644 --- a/lib/rollbar/plugins/active_job.rb +++ b/lib/rollbar/plugins/active_job.rb @@ -13,3 +13,6 @@ def self.included(base) end end end + +# Automatically add to ActionMailer::DeliveryJob +ActionMailer::DeliveryJob.include Rollbar::ActiveJob if defined?(ActionMailer::DeliveryJob) diff --git a/spec/rollbar/plugins/active_job_spec.rb b/spec/rollbar/plugins/active_job_spec.rb index b0ccf60c..323fb79e 100644 --- a/spec/rollbar/plugins/active_job_spec.rb +++ b/spec/rollbar/plugins/active_job_spec.rb @@ -25,21 +25,53 @@ def perform(exception, job_id) before { reconfigure_notifier } let(:exception) { StandardError.new('oh no') } - let(:job_id) { "123" } + let(:job_id) { '123' } let(:argument) { 12 } - it "reports the error to Rollbar" do + it 'reports the error to Rollbar' do expected_params = { - :job => "TestJob", + :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 + 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 + 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 '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 end From 9bbc164baa098f5612b5becd098e7d67085e3ed0 Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Wed, 13 Feb 2019 15:19:39 -0800 Subject: [PATCH 2/4] #include is private in some versions of ActionMailer::DeliveryJob --- lib/rollbar/plugins/active_job.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rollbar/plugins/active_job.rb b/lib/rollbar/plugins/active_job.rb index d34cc2e8..de1bab9d 100644 --- a/lib/rollbar/plugins/active_job.rb +++ b/lib/rollbar/plugins/active_job.rb @@ -15,4 +15,4 @@ def self.included(base) end # Automatically add to ActionMailer::DeliveryJob -ActionMailer::DeliveryJob.include Rollbar::ActiveJob if defined?(ActionMailer::DeliveryJob) +ActionMailer::DeliveryJob.send(:include, Rollbar::ActiveJob) if defined?(ActionMailer::DeliveryJob) From 4c1deff9ddb3fe744a1745eb0eb3beaa31f1d106 Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Mon, 18 Feb 2019 12:21:17 -0800 Subject: [PATCH 3/4] fix auto-merge from master --- spec/rollbar/plugins/active_job_spec.rb | 57 ++++++------------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/spec/rollbar/plugins/active_job_spec.rb b/spec/rollbar/plugins/active_job_spec.rb index c5908221..323fb79e 100644 --- a/spec/rollbar/plugins/active_job_spec.rb +++ b/spec/rollbar/plugins/active_job_spec.rb @@ -2,52 +2,23 @@ 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 +describe Rollbar::ActiveJob do + class TestJob + # To mix in rescue_from + include ActiveSupport::Rescuable + include Rollbar::ActiveJob - attr_reader :job_id - attr_accessor :arguments + 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 } - - 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 - end + def initialize(*arguments) + @arguments = arguments + 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 + 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 From c581c3433e85ca6367dc6f951434592292993211 Mon Sep 17 00:00:00 2001 From: Walt Jones Date: Mon, 18 Feb 2019 15:03:16 -0800 Subject: [PATCH 4/4] use ActiveJob test helpers for #perform_later --- spec/rollbar/delay/active_job_spec.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spec/rollbar/delay/active_job_spec.rb b/spec/rollbar/delay/active_job_spec.rb index e401faaf..05f9fce1 100644 --- a/spec/rollbar/delay/active_job_spec.rb +++ b/spec/rollbar/delay/active_job_spec.rb @@ -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