From 7779eb8f4be816c37ee964fe2220667890cda071 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Mon, 18 Nov 2024 12:58:10 +0100 Subject: [PATCH] fixup --- lib/mini_scheduler.rb | 11 +++++++++-- spec/mini_scheduler/manager_spec.rb | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/mini_scheduler.rb b/lib/mini_scheduler.rb index e6bea57..962a6ca 100644 --- a/lib/mini_scheduler.rb +++ b/lib/mini_scheduler.rb @@ -17,16 +17,23 @@ def self.configure yield self end + SidekiqExceptionHandler = + if defined?(Sidekiq.default_configuration) # Sidekiq 7+ + ->(ex, ctx, _config = nil) { Sidekiq.default_configuration.handle_exception(ex, ctx) } + else # Sidekiq 6.5 + ->(ex, ctx, _config = nil) { Sidekiq.handle_exception(ex, ctx) } + end + def self.job_exception_handler(&blk) @job_exception_handler = blk if blk @job_exception_handler end - def self.handle_job_exception(ex, context = {}) + def self.handle_job_exception(ex, context = {}, _config = nil) if job_exception_handler job_exception_handler.call(ex, context) else - Sidekiq.default_configuration.handle_exception(ex, context) + SidekiqExceptionHandler.call(ex, context) end end diff --git a/spec/mini_scheduler/manager_spec.rb b/spec/mini_scheduler/manager_spec.rb index 821c848..c26590a 100644 --- a/spec/mini_scheduler/manager_spec.rb +++ b/spec/mini_scheduler/manager_spec.rb @@ -418,7 +418,7 @@ def expect_job_failure(ex, ctx) class TempSidekiqLogger attr_accessor :exception, :context - def call(ex, ctx, _component) + def call(ex, ctx, _config = nil) self.exception = ex self.context = ctx end @@ -426,9 +426,17 @@ def call(ex, ctx, _component) let(:logger) { TempSidekiqLogger.new } - before { Sidekiq.default_configuration.error_handlers << logger } + let(:error_handlers) do + if defined?(Sidekiq.default_configuration) + Sidekiq.default_configuration.error_handlers + else + Sidekiq.error_handlers + end + end - after { Sidekiq.default_configuration.error_handlers.delete(logger) } + before { error_handlers << logger } + + after { error_handlers.delete(logger) } it "captures failed jobs" do manager.blocking_tick @@ -438,7 +446,9 @@ def call(ex, ctx, _component) end context "with custom handler" do - before { MiniScheduler.job_exception_handler { |ex, ctx| expect_job_failure(ex, ctx) } } + before do + MiniScheduler.job_exception_handler { |ex, ctx, _config = nil| expect_job_failure(ex, ctx) } + end after { MiniScheduler.instance_variable_set :@job_exception_handler, nil }