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

FIX: record the original exception info when job can be retried #29

Merged
merged 1 commit into from
May 6, 2022
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
2 changes: 2 additions & 0 deletions lib/sidekiq/logging/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def log_job_exception(job, started_at, exc)

payload['message'] += ": fail: #{payload['duration']} sec"
payload['job_status'] = 'fail'

exc = exc.cause || exc if exc.is_a? Sidekiq::JobRetry::Handled
payload['error_message'] = exc.message
payload['error'] = exc.class
payload['error_backtrace'] = %('#{exc.backtrace.join("\n")}')
Expand Down
24 changes: 24 additions & 0 deletions spec/sidekiq/logstash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ def process(worker, params = [], encrypt: false)
end
end

context 'when job raises a error' do
it 'logs the exception with job retry' do
mock_redis = double(:Redis)
allow(Sidekiq).to receive(:redis).and_yield(mock_redis)

expect(mock_redis).to receive(:zadd).with('retry', any_args).once
expect { process(SpecWorker, [true]) }.to raise_error(RuntimeError)

expect(log_messages.last['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_messages.last['error']).to eq('RuntimeError')
expect(log_messages.last['error_backtrace'].split("\n").first).to include('workers/spec_worker.rb:7')
end

it 'logs the exception without job retry' do
allow(SpecWorker).to receive(:get_sidekiq_options).and_return({ 'retry' => false, 'queue' => 'default' })

expect { process(SpecWorker, [true]) }.to raise_error(RuntimeError)

expect(log_messages.last['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_messages.last['error']).to eq('RuntimeError')
expect(log_messages.last['error_backtrace'].split("\n").first).to include('workers/spec_worker.rb:7')
end
end

context 'with job_start_log enabled' do
before do
Sidekiq::Logstash.configure do |config|
Expand Down
2 changes: 1 addition & 1 deletion spec/workers/spec_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class SpecWorker
include Sidekiq::Worker

def perform(fail = false, _params = {})
raise RuntimeError if fail
raise 'You know nothing, Jon Snow.' if fail
end
end