From a0113c83cfdc5df3f57ef15a8a8f1f5605c82df8 Mon Sep 17 00:00:00 2001 From: Alexey Katichev Date: Sun, 30 Apr 2017 16:43:31 +0300 Subject: [PATCH] introduce job.update_meta() to store updated meta to Redis (#823) * introduce job.update_meta() to store updated meta to Redis This closes nvie/rq#811 * rename update_meta to save_meta --- rq/job.py | 5 +++++ tests/test_job.py | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/rq/job.py b/rq/job.py index 703de66..0ccf736 100644 --- a/rq/job.py +++ b/rq/job.py @@ -493,6 +493,11 @@ class Job(object): connection.hmset(key, self.to_dict(include_meta=include_meta)) self.cleanup(self.ttl, pipeline=connection) + def save_meta(self): + """Stores job meta from the job instance to the corresponding Redis key.""" + meta = dumps(self.meta) + self.connection.hset(self.key, 'meta', meta) + def cancel(self): """Cancels the given job, which will prevent the job from ever being ran (or inspected). diff --git a/tests/test_job.py b/tests/test_job.py index 87342eb..1491f7f 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -272,6 +272,26 @@ class TestJob(RQTestCase): job2 = Job.fetch(job.id) self.assertEqual(job2.meta['foo'], 'bar') + def test_custom_meta_is_rewriten_by_save_meta(self): + """New meta data can be stored by save_meta.""" + job = Job.create(func=fixtures.say_hello, args=('Lionel',)) + job.save() + serialized = job.to_dict() + + job.meta['foo'] = 'bar' + job.save_meta() + + raw_meta = self.testconn.hget(job.key, 'meta') + self.assertEqual(loads(raw_meta)['foo'], 'bar') + + job2 = Job.fetch(job.id) + self.assertEqual(job2.meta['foo'], 'bar') + + # nothing else was changed + serialized2 = job2.to_dict() + serialized2.pop('meta') + self.assertDictEqual(serialized, serialized2) + def test_result_ttl_is_persisted(self): """Ensure that job's result_ttl is set properly""" job = Job.create(func=fixtures.say_hello, args=('Lionel',), result_ttl=10)