lib/resque/plugins/retry.rb in resque-retry-1.2.1 vs lib/resque/plugins/retry.rb in resque-retry-1.3.0
- old
+ new
@@ -152,14 +152,33 @@
# other than try the exact same job again
#
# @return [Array] new job arguments
#
# @api public
- def args_for_retry(*args)
- args
+ def retry_args(*args)
+ # Here for backwards compatibility. If an "args_for_retry" method exists
+ # invoke it, but warn that it is deprecated (and will be removed in a
+ # future revision)
+ if respond_to?(:args_for_retry)
+ warn "`Resque::Plugins::Retry#args_for_retry` is deprecated, please use `Resque::Plugins::Retry#retry_args` instead."
+ args_for_retry(*args)
+ else
+ args
+ end
end
+ # @abstract
+ # Modify the arguments used to retry the job based on the exception.
+ # Use this to do something other than try the exact same job again.
+ #
+ # @return [Array] new job arguments
+ #
+ # @api public
+ def retry_args_for_exception(exception, *args)
+ retry_args(*args)
+ end
+
# Convenience method to test whether you may retry on a given
# exception
#
# @param [Exception] an instance of Exception. Deprecated: can
# also be a Class
@@ -221,10 +240,18 @@
else
@retry_exceptions ||= nil
end
end
+ # @abstract
+ # The number of seconds to set the TTL to on the resque-retry key in redis
+ #
+ # @return [Number] number of seconds
+ #
+ # @api public
+ attr_reader :expire_retry_key_after
+
# Test if the retry criteria is valid
#
# @param [Exception] exception
# @param [Array] args job arguments
# @return [Boolean]
@@ -320,15 +347,17 @@
# the existence of this to determine if the job should be sent to the
# parent failure backend (e.g. failed queue) or not. Removing this means
# jobs that fail before ::perform will be both retried and sent to the failed queue.
Resque.redis.setnx(redis_retry_key(*args), -1)
+ retry_args = retry_args_for_exception(exception, *args)
+
if temp_retry_delay <= 0
# If the delay is 0, no point passing it through the scheduler
- Resque.enqueue(retry_in_queue, *args_for_retry(*args))
+ Resque.enqueue(retry_in_queue, *retry_args)
else
- Resque.enqueue_in(temp_retry_delay, retry_in_queue, *args_for_retry(*args))
+ Resque.enqueue_in(temp_retry_delay, retry_in_queue, *retry_args)
end
# remove retry key from redis if we handed retry off to another queue.
clean_retry_key(*args) if retry_job_delegate
@@ -336,22 +365,28 @@
sleep(sleep_after_requeue) if sleep_after_requeue > 0
end
# Resque before_perform hook
#
- # Increments and sets the `@retry_attempt` count.
+ # Increments `@retry_attempt` count and updates the "retry_key" expiration
+ # time (if applicable)
#
# @api private
def before_perform_retry(*args)
log_message 'before_perform_retry', args
@on_failure_retry_hook_already_called = false
# store number of retry attempts.
retry_key = redis_retry_key(*args)
- Resque.redis.setnx(retry_key, -1) # default to -1 if not set.
- @retry_attempt = Resque.redis.incr(retry_key) # increment by 1.
+ Resque.redis.setnx(retry_key, -1)
+ @retry_attempt = Resque.redis.incr(retry_key)
log_message "attempt: #{@retry_attempt} set in Redis", args
- Resque.redis.expire(retry_key, @retry_delay.to_i + @expire_retry_key_after.to_i) if @expire_retry_key_after
+
+ # set/update the "retry_key" expiration
+ if expire_retry_key_after
+ log_message "updating expiration for retry key: #{retry_key}", args
+ Resque.redis.expire(retry_key, retry_delay + expire_retry_key_after)
+ end
end
# Resque after_perform hook
#
# Deletes retry attempt count from Redis.