From 7ef3b5ade891db007bfea0b8e2901938f0885940 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Wed, 15 Feb 2012 21:53:14 +0100 Subject: [PATCH] Cleanup job hashes for jobs without result, too. --- rq/worker.py | 13 +++++++++---- tests/test_worker.py | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/rq/worker.py b/rq/worker.py index 26fc5d6..25830aa 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -333,14 +333,19 @@ class Worker(object): fq.quarantine(job, exc_info=traceback.format_exc()) return False + + if rv is None: + self.log.info('Job OK') else: - if rv is None: - self.log.info('Job OK') - else: - self.log.info('Job OK, result = %s' % (yellow(unicode(rv)),)) + self.log.info('Job OK, result = %s' % (yellow(unicode(rv)),)) + if rv is not None: p = conn.pipeline() p.hset(job.key, 'result', dumps(rv)) p.expire(job.key, self.rv_ttl) p.execute() + else: + # Cleanup immediately + job.delete() + return True diff --git a/tests/test_worker.py b/tests/test_worker.py index 3000cbc..19086e2 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -1,6 +1,6 @@ import os from tests import RQTestCase -from tests.fixtures import say_hello, div_by_zero, create_file +from tests.fixtures import say_hello, div_by_zero, do_nothing, create_file from tests.helpers import strip_milliseconds from rq import Queue, Worker, Job @@ -111,3 +111,21 @@ class TestWorker(RQTestCase): # Should not have created evidence of execution self.assertEquals(os.path.exists(SENTINEL_FILE), False) + def test_cleaning_up_of_jobs(self): + """Jobs get cleaned up after successful execution.""" + q = Queue() + job_with_rv = q.enqueue(say_hello, 'Franklin') + job_without_rv = q.enqueue(do_nothing) + + # Job hashes exists + self.assertEquals(self.testconn.type(job_with_rv.key), 'hash') + self.assertEquals(self.testconn.type(job_without_rv.key), 'hash') + + # Execute the job + w = Worker([q]) + w.work(burst=True) + + # Jobs with results expire after a certain TTL, while jobs without + # results are immediately removed + assert self.testconn.ttl(job_with_rv.key) > 0 + assert self.testconn.exists(job_without_rv.key) == False