From ccfd4a02cbc9130c845b64ca6b5b7510c99fadde Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Tue, 21 Jan 2020 19:50:13 +0700 Subject: [PATCH] Failed jobs will now auto expire (#1182) --- rq/registry.py | 2 +- tests/test_registry.py | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/rq/registry.py b/rq/registry.py index 9232ddf..25c3bae 100644 --- a/rq/registry.py +++ b/rq/registry.py @@ -197,7 +197,7 @@ class FailedJobRegistry(BaseRegistry): job.exc_info = exc_string job.save(pipeline=p, include_meta=False) - job.cleanup(ttl=-1, pipeline=p) # failed job won't expire + job.cleanup(ttl=ttl, pipeline=p) p.zadd(self.key, {job.id: score}) if not pipeline: diff --git a/tests/test_registry.py b/tests/test_registry.py index b7921d1..8938c24 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -318,26 +318,25 @@ class TestFailedJobRegistry(RQTestCase): timestamp = current_timestamp() registry.add(job) - self.assertLess( - self.testconn.zscore(key, job.id), - timestamp + DEFAULT_FAILURE_TTL + 2 - ) - self.assertGreater( - self.testconn.zscore(key, job.id), - timestamp + DEFAULT_FAILURE_TTL - 2 - ) + score = self.testconn.zscore(key, job.id) + self.assertLess(score, timestamp + DEFAULT_FAILURE_TTL + 2) + self.assertGreater(score, timestamp + DEFAULT_FAILURE_TTL - 2) + + # Job key will also expire + job_ttl = self.testconn.ttl(job.key) + self.assertLess(job_ttl, DEFAULT_FAILURE_TTL + 2) + self.assertGreater(job_ttl, DEFAULT_FAILURE_TTL - 2) timestamp = current_timestamp() ttl = 5 - registry.add(job, ttl=5) - self.assertLess( - self.testconn.zscore(key, job.id), - timestamp + ttl + 2 - ) - self.assertGreater( - self.testconn.zscore(key, job.id), - timestamp + ttl - 2 - ) + registry.add(job, ttl=ttl) + score = self.testconn.zscore(key, job.id) + self.assertLess(score, timestamp + ttl + 2) + self.assertGreater(score, timestamp + ttl - 2) + + job_ttl = self.testconn.ttl(job.key) + self.assertLess(job_ttl, ttl + 2) + self.assertGreater(job_ttl, ttl - 2) def test_requeue(self): """FailedJobRegistry.requeue works properly"""