Skip to content

Commit

Permalink
Make the gem compatible with Sidekiq 7.
Browse files Browse the repository at this point in the history
  • Loading branch information
iMacTia committed Nov 4, 2022
1 parent e918893 commit fad708e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
2 changes: 1 addition & 1 deletion sidekiq-logstash.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = '>= 2.5.0'

spec.add_dependency 'logstash-event', '~> 1.2'
spec.add_runtime_dependency 'sidekiq', '>= 6.0', '<7'
spec.add_dependency 'sidekiq', '~> 7.0'

spec.add_development_dependency 'factory_bot', '~> 6.1'
spec.add_development_dependency 'rake', '>= 10'
Expand Down
25 changes: 10 additions & 15 deletions spec/sidekiq/logstash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ def process(worker, params = [], encrypt: false)
let(:buffer) { StringIO.new }
let(:logger) { Logger.new(buffer) }
let(:job) { build(:job) }
let(:processor_options) { Sidekiq }
let(:processor) { ::Sidekiq::Processor.new(processor_options) }
let(:processor) { ::Sidekiq::Processor.new(Sidekiq.default_configuration.default_capsule) }
let(:log_message) { JSON.parse(buffer.string) }
let(:log_messages) { buffer.string.split("\n").map { |log| JSON.parse(log) } }

before do
logger.formatter = Sidekiq::Logging::LogstashFormatter.new
Sidekiq.logger = logger
processor_options[:job_logger] = Sidekiq::LogstashJobLogger
processor_options[:fetch] = ::Sidekiq::BasicFetch.new(processor_options)
Sidekiq.default_configuration.tap do |config|
config.logger = logger
end
end

it 'has a version number' do
Expand Down Expand Up @@ -102,25 +101,21 @@ def process(worker, params = [], encrypt: false)

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')
expect(log_messages[1]['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_messages[1]['error']).to eq('RuntimeError')
expect(log_messages[1]['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')
expect(log_messages[0]['error_message']).to eq('You know nothing, Jon Snow.')
expect(log_messages[0]['error']).to eq('RuntimeError')
expect(log_messages[0]['error_backtrace'].split("\n").first).to include('workers/spec_worker.rb:7')
end
end

Expand Down

0 comments on commit fad708e

Please sign in to comment.