diff --git a/lib/resque/plugins/retry.rb b/lib/resque/plugins/retry.rb index a0a4967..2b2810b 100644 --- a/lib/resque/plugins/retry.rb +++ b/lib/resque/plugins/retry.rb @@ -343,7 +343,7 @@ def before_perform_retry(*args) Resque.redis.setnx(retry_key, -1) # default to -1 if not set. @retry_attempt = Resque.redis.incr(retry_key) # increment by 1. log_message "attempt: #{@retry_attempt} set in Redis", args - @retry_attempt + Resque.redis.expire(retry_key, @retry_delay.to_i + @expire_retry_key_after.to_i) if @expire_retry_key_after end # Resque after_perform hook diff --git a/test/multiple_failure_test.rb b/test/multiple_failure_test.rb index 307ef37..7d9a614 100644 --- a/test/multiple_failure_test.rb +++ b/test/multiple_failure_test.rb @@ -130,6 +130,16 @@ def test_custom_retry_identifier_job assert_equal 1, MockFailureBackend.errors.size end + def test_failure_with_retry_bumps_key_expire + Resque.enqueue(FailFiveTimesWithExpiryJob, 'foo') + retry_key = FailFiveTimesWithExpiryJob.redis_retry_key('foo') + + Resque.redis.expects(:expire).times(4).with(retry_key, 3600) + 4.times do + perform_next_job(@worker) + end + end + def teardown Resque::Failure.backend = @old_failure_backend end diff --git a/test/retry_test.rb b/test/retry_test.rb index 990318d..ea8ed4e 100644 --- a/test/retry_test.rb +++ b/test/retry_test.rb @@ -265,4 +265,9 @@ def test_retry_delay assert_equal 13, PerExceptionClassRetryCountJob.retry_delay(Timeout::Error) end + def test_expire_key_set + Resque.redis.expects(:expire).once.with(ExpiringJob.redis_retry_key('expiry_test'), 3600) + Resque.enqueue(ExpiringJob, 'expiry_test') + perform_next_job(@worker) + end end diff --git a/test/test_jobs.rb b/test/test_jobs.rb index 96ae112..90ae2fd 100644 --- a/test/test_jobs.rb +++ b/test/test_jobs.rb @@ -19,6 +19,15 @@ def self.perform(*args) end end +class ExpiringJob + extend Resque::Plugins::Retry + @queue = :testing + @expire_retry_key_after = 60 * 60 + + def self.perform(*args) + end +end + class RetryDefaultSettingsJob extend Resque::Plugins::Retry @queue = :testing @@ -135,6 +144,16 @@ def self.perform(*args) end end +class FailFiveTimesWithExpiryJob < RetryDefaultsJob + @queue = :testing + @retry_limit = 6 + @expire_retry_key_after = 60 * 60 + + def self.perform(*args) + raise if retry_attempt <= 4 + end +end + class ExponentialBackoffJob < RetryDefaultsJob extend Resque::Plugins::ExponentialBackoff @queue = :testing