Skip to content

Commit

Permalink
Option to enable scoped block for DelayedJob plugin (#1087)
Browse files Browse the repository at this point in the history
When enabled, this adds job data to any Rollbar reporting that happens
within the job, and prevents changes to the scope from polluting the
global scope for other jobs.

Co-authored-by: Walt Jones <waltjones@gmail.com>
  • Loading branch information
iangreenleaf and waltjones committed Dec 19, 2023
1 parent 54b192d commit 4556623
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/rollbar/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Configuration
:disable_monkey_patch,
:disable_rack_monkey_patch,
:dj_threshold,
:dj_use_scoped_block,
:enable_error_context,
:enabled,
:endpoint,
Expand Down Expand Up @@ -102,6 +103,7 @@ def initialize
@disable_rack_monkey_patch = false
@enable_error_context = true
@dj_threshold = 0
@dj_use_scoped_block = false
@async_skip_report_handler = nil
@enabled = nil # set to true when configure is called
@endpoint = DEFAULT_ENDPOINT
Expand Down
7 changes: 6 additions & 1 deletion lib/rollbar/plugins/delayed_job/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def self.wrap_worker!
def self.invoke_job_callback
proc do |job, *args, &block|
begin
block.call(job, *args)
if Rollbar.configuration.dj_use_scoped_block
data = Rollbar::Delayed.build_job_data(job)
Rollbar.scoped(:request => data) { block.call(job, *args) }
else
block.call(job, *args)
end
rescue StandardError => e
report(e, job)

Expand Down
57 changes: 57 additions & 0 deletions spec/rollbar/plugins/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def do_job_please!(_a, _b)
end
end

class PassingJob
def do_job_please!(_a, _b)
Rollbar.log("A job!")
end
end

before do
Delayed::Backend::Test.prepare_worker

Expand Down Expand Up @@ -49,6 +55,57 @@ def do_job_please!(_a, _b)
end
end

describe 'scope within job' do
context 'when Rollbar.configuration.dj_use_scoped_block is false (default)' do
before do
Rollbar.configuration.dj_use_scoped_block = false
end

it 'does not wrap job in .scoped block' do
expect(Rollbar).not_to receive(:scoped).and_call_original
PassingJob.new.delay.do_job_please!(:foo, :bar)
end

it 'does not include job data in scope' do
job_scope = nil

Rollbar.stub(:log) do |_msg|
job_scope = Rollbar.scope_object
nil
end

PassingJob.new.delay.do_job_please!(:foo, :bar)

expect(job_scope).not_to have_key(:request)
end
end

context 'when Rollbar.configuration.dj_use_scoped_block is true' do
before do
Rollbar.configuration.dj_use_scoped_block = true
end

it 'wraps job in .scoped block' do
expect(Rollbar).to receive(:scoped).and_call_original
PassingJob.new.delay.do_job_please!(:foo, :bar)
end

it 'includes job data in scope' do
job_scope = nil

Rollbar.stub(:log) do |_msg|
job_scope = Rollbar.scope_object
nil
end

PassingJob.new.delay.do_job_please!(:foo, :bar)

expect(job_scope).to have_key(:request)
expect(job_scope[:request]).to include("name" => "PassingJob#do_job_please!")
end
end
end

context 'with failed deserialization' do
let(:old_expected_args) do
[/Delayed::DeserializationError/, { :use_exception_level_filters => true }]
Expand Down

0 comments on commit 4556623

Please sign in to comment.