Skip to content

Commit

Permalink
Add checks to ensure that ignore_exceptions is properly configured
Browse files Browse the repository at this point in the history
  • Loading branch information
branliu0 committed Oct 28, 2015
1 parent 58cc041 commit 838f4b3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/resque/plugins/retry.rb
Expand Up @@ -43,13 +43,30 @@ module Retry
# @api public
class AmbiguousRetryStrategyException < StandardError; end

# Raised if there is a problem with the configuration of resque-retry.
#
# @api public
class RetryConfigurationException < StandardError; end

# Fail fast, when extended, if the "receiver" is misconfigured
#
# @api private
def self.extended(receiver)
if receiver.instance_variable_get('@fatal_exceptions') && receiver.instance_variable_get('@retry_exceptions')
raise AmbiguousRetryStrategyException.new(%{You can't define both "@fatal_exceptions" and "@retry_exceptions"})
end

# Check that ignore_exceptions is a subset of retry_exceptions
retry_exceptions = receiver.instance_variable_get('@retry_exceptions')
if retry_exceptions.is_a?(Hash)
exceptions = retry_exceptions.keys
else
exceptions = Array(retry_exceptions)
end
excess_exceptions = Array(receiver.instance_variable_get('@ignore_exceptions')) - exceptions
unless excess_exceptions.empty?
raise RetryConfigurationException, "The following exceptions are defined in @ignore_exceptions but not in @retry_exceptions: #{excess_exceptions.join(', ')}."
end
end

# Copy retry criteria checks, try again callbacks, and give up callbacks
Expand Down
12 changes: 12 additions & 0 deletions test/ignore_exceptions_test.rb
Expand Up @@ -23,4 +23,16 @@ def test_ignore_exceptions
perform_next_job(@worker)
assert_equal '1', Resque.redis.get(retry_key), 'retry counter'
end

def test_ignore_exception_configuration_1
assert_raises Resque::Plugins::Retry::RetryConfigurationException do
IgnoreExceptionsImproperlyConfiguredJob1.extend(Resque::Plugins::Retry)
end
end

def test_ignore_exception_configuration_2
assert_raises Resque::Plugins::Retry::RetryConfigurationException do
IgnoreExceptionsImproperlyConfiguredJob2.extend(Resque::Plugins::Retry)
end
end
end
13 changes: 13 additions & 0 deletions test/test_jobs.rb
Expand Up @@ -591,3 +591,16 @@ def self.perform
"Hello, World!"
end
end

class IgnoreExceptionsImproperlyConfiguredJob1
# Manually extend Resque::Plugins::Retry in the test code to catch the
# exception.
@ignore_exceptions = [CustomException]
end

class IgnoreExceptionsImproperlyConfiguredJob2
# Manually extend Resque::Plugins::Retry in the test code to catch the
# exception.
@ignore_exceptions = [CustomException]
@retry_exceptions = { AnotherCustomException => 0 }
end

0 comments on commit 838f4b3

Please sign in to comment.