Skip to content

Commit

Permalink
adds queue argument to dsl
Browse files Browse the repository at this point in the history
  • Loading branch information
moofkit committed Oct 15, 2024
1 parent 1f3adb2 commit a51daa3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/sidekiq/rescue/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/sidekiq/rescue/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a51daa3

Please sign in to comment.