From a51daa3ae47562f6866a5ddb537ea813f27ea457 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivliev <3938172+moofkit@users.noreply.github.com> Date: Tue, 15 Oct 2024 11:43:47 +0300 Subject: [PATCH] adds queue argument to dsl --- lib/sidekiq/rescue/dsl.rb | 22 +++++++++++++++++----- spec/sidekiq/rescue/dsl_spec.rb | 6 ++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/sidekiq/rescue/dsl.rb b/lib/sidekiq/rescue/dsl.rb index cd82cf2..91f0ec4 100644 --- a/lib/sidekiq/rescue/dsl.rb +++ b/lib/sidekiq/rescue/dsl.rb @@ -23,15 +23,19 @@ module ClassMethods # @raise [ArgumentError] if delay is not an Integer or Float # @raise [ArgumentError] if limit is not an Integer # @raise [ArgumentError] if jitter is not an Integer or Float + # @raise [ArgumentError] if queue is not a String # @example # sidekiq_rescue NetworkError, delay: 60, limit: 10 def sidekiq_rescue(*errors, delay: Sidekiq::Rescue.config.delay, limit: Sidekiq::Rescue.config.limit, - jitter: Sidekiq::Rescue.config.jitter) + jitter: Sidekiq::Rescue.config.jitter, queue: nil) unpacked_errors = validate_and_unpack_error_argument(errors) validate_delay_argument(delay) validate_limit_argument(limit) validate_jitter_argument(jitter) - assign_sidekiq_rescue_options(errors: unpacked_errors, delay: delay, limit: limit, jitter: jitter) + validate_queue_argument(queue) + assign_sidekiq_rescue_options( + errors: unpacked_errors, delay: delay, limit: limit, jitter: jitter, queue: queue + ) end # Find the error group and options for the given exception. @@ -77,10 +81,18 @@ def validate_jitter_argument(jitter) "jitter must be integer or float" end - def assign_sidekiq_rescue_options(errors:, delay:, limit:, jitter:) + def validate_queue_argument(queue) + return if queue.nil? || queue.is_a?(String) + + raise ArgumentError, + "queue must be a string" + end + + def assign_sidekiq_rescue_options(errors:, delay:, limit:, jitter:, queue:) self.sidekiq_rescue_options ||= {} - self.sidekiq_rescue_options = self.sidekiq_rescue_options.merge(errors => { delay: delay, limit: limit, - jitter: jitter }) + self.sidekiq_rescue_options = self.sidekiq_rescue_options.merge(errors => { + delay: delay, limit: limit, jitter: jitter, queue: queue + }.compact) end end end diff --git a/spec/sidekiq/rescue/dsl_spec.rb b/spec/sidekiq/rescue/dsl_spec.rb index 8742244..67adbb9 100644 --- a/spec/sidekiq/rescue/dsl_spec.rb +++ b/spec/sidekiq/rescue/dsl_spec.rb @@ -47,6 +47,12 @@ def define_dsl(...) expect(job_class.sidekiq_rescue_options.dig([TestError], :delay)).to be_a(Proc) end + it "sets the queue" do + define_dsl { sidekiq_rescue TestError, queue: "slow" } + + expect(job_class.sidekiq_rescue_options.dig([TestError], :queue)).to eq("slow") + end + it "raises an ArgumentError if delay proc has no arguments" do expect { define_dsl { sidekiq_rescue TestError, delay: -> { 10 } } }.to raise_error( ArgumentError,