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

Option to enable scoped block for DelayedJob plugin #1087

Merged
merged 2 commits into from
Dec 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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