Skip to content

Commit

Permalink
Fix Sidekiq error handler not taking effect
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmorrison committed Jul 1, 2024
1 parent 70722e4 commit abee430
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
20 changes: 10 additions & 10 deletions lib/rails_semantic_logger/extensions/sidekiq/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,25 @@ def call(_exception, ctx)
class Config
remove_const :ERROR_HANDLER

ERROR_HANDLER = lambda { |_ex, ctx, _cfg = Sidekiq.default_configuration|
ERROR_HANDLER = ->(ex, ctx, cfg = Sidekiq.default_configuration) do
unless ctx.empty?
job_hash = ctx[:job] || {}
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
end
}
end
end
else
# Sidekiq >= 6.5
# TODO: Not taking effect. See test/sidekiq_test.rb
def self.default_error_handler(_exception, ctx)
return if ctx.empty?

job_hash = ctx[:job] || {}
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
Sidekiq.error_handlers.delete(Sidekiq::DEFAULT_ERROR_HANDLER)
Sidekiq.error_handlers << ->(ex, ctx) do
unless ctx.empty?
job_hash = ctx[:job] || {}
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
end
end
end

Expand Down
14 changes: 2 additions & 12 deletions test/sidekiq_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,8 @@ class SidekiqTest < Minitest::Test
end
elsif Sidekiq::VERSION.to_i == 6
let(:processor) do
# TODO: Sidekiq 6.5 default handler is not accepting the patch
handler = lambda { |_ex, ctx|
unless ctx.empty?
job_hash = ctx[:job] || {}
klass = job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"]
logger = klass ? SemanticLogger[klass] : Sidekiq.logger
ctx[:context] ? logger.warn(ctx[:context], ctx) : logger.warn(ctx)
end
}
config = Sidekiq
config[:error_handlers] = [handler]
config[:fetch] = Sidekiq::BasicFetch.new(config)
config = Sidekiq
config[:fetch] = Sidekiq::BasicFetch.new(config)
Sidekiq::Processor.new(config) { |*args| }
end
elsif Sidekiq::VERSION.to_i < 7
Expand Down

0 comments on commit abee430

Please sign in to comment.