lib/resque/plugins/retry.rb in resque-retry-0.2.1 vs lib/resque/plugins/retry.rb in resque-retry-0.2.2
- old
+ new
@@ -84,12 +84,18 @@
# @abstract
# Number of seconds to delay until the job is retried.
#
# @return [Number] number of seconds to delay
- def retry_delay
- @retry_delay ||= 0
+ def retry_delay(exception_class = nil)
+ if @retry_exceptions.is_a?(Hash)
+ delay = @retry_exceptions[exception_class] || 0
+ # allow an array of delays.
+ delay.is_a?(Array) ? delay[retry_attempt] || delay.last : delay
+ else
+ @retry_delay ||= 0
+ end
end
# @abstract
# Number of seconds to sleep after job is requeued
#
@@ -120,11 +126,15 @@
#
# Default: `nil` - this will retry all exceptions.
#
# @return [Array, nil]
def retry_exceptions
- @retry_exceptions ||= nil
+ if @retry_exceptions.is_a?(Hash)
+ @retry_exceptions.keys
+ else
+ @retry_exceptions ||= nil
+ end
end
# Test if the retry criteria is valid.
#
# @param [Exception] exception
@@ -189,16 +199,20 @@
def retry_criteria_check(&block)
retry_criteria_checks << block
end
# Retries the job.
- def try_again(*args)
- if retry_delay <= 0
+ def try_again(exception, *args)
+ # some plugins define retry_delay and have it take no arguments, so rather than break those,
+ # we'll just check here to see whether it takes the additional exception class argument or not
+ temp_retry_delay = ([-1, 1].include?(method(:retry_delay).arity) ? retry_delay(exception.class) : retry_delay)
+
+ if temp_retry_delay <= 0
# If the delay is 0, no point passing it through the scheduler
Resque.enqueue(self, *args_for_retry(*args))
else
- Resque.enqueue_in(retry_delay, self, *args_for_retry(*args))
+ Resque.enqueue_in(temp_retry_delay, self, *args_for_retry(*args))
end
sleep(sleep_after_requeue) if sleep_after_requeue > 0
end
# Resque before_perform hook.
@@ -221,10 +235,10 @@
#
# Checks if our retry criteria is valid, if it is we try again.
# Otherwise the retry attempt count is deleted from Redis.
def on_failure_retry(exception, *args)
if retry_criteria_valid?(exception, *args)
- try_again(*args)
+ try_again(exception, *args)
else
Resque.redis.del(redis_retry_key(*args))
end
end