From 54bc04bb45add19544912cb2821369c972c33ee3 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Fri, 28 Jul 2017 13:36:30 +0700 Subject: [PATCH 1/2] job.save() shouldn't crash on unpickleable return value. --- rq/job.py | 6 ++++-- tests/test_job.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/rq/job.py b/rq/job.py index da57e1d..83baacb 100644 --- a/rq/job.py +++ b/rq/job.py @@ -441,7 +441,6 @@ class Job(object): You can exclude serializing the `meta` dictionary by setting `include_meta=False`. - """ obj = {} obj['created_at'] = utcformat(self.created_at or utcnow()) @@ -458,7 +457,10 @@ class Job(object): if self.ended_at is not None: obj['ended_at'] = utcformat(self.ended_at) if self._result is not None: - obj['result'] = dumps(self._result) + try: + obj['result'] = dumps(self._result) + except: + obj['result'] = 'Unpickleable return value' if self.exc_info is not None: obj['exc_info'] = self.exc_info if self.timeout is not None: diff --git a/tests/test_job.py b/tests/test_job.py index 46d7d39..4f570af 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -5,6 +5,7 @@ from __future__ import (absolute_import, division, print_function, from datetime import datetime import time +from Queue import Queue from tests import fixtures, RQTestCase from tests.helpers import strip_microseconds @@ -292,6 +293,13 @@ class TestJob(RQTestCase): serialized2.pop('meta') self.assertDictEqual(serialized, serialized2) + def test_unpickleable_result(self): + """Unpickleable job result doesn't crash job.to_dict()""" + job = Job.create(func=fixtures.say_hello, args=('Lionel',)) + job._result = Queue() + data = job.to_dict() + self.assertEqual(data['result'], 'Unpickleable return value') + 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) From 0efb87a46be08f051399c343f41d292855f783bf Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Fri, 28 Jul 2017 13:49:38 +0700 Subject: [PATCH 2/2] Fixed test error in Python 3. --- tests/test_job.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/test_job.py b/tests/test_job.py index 4f570af..67b87e2 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -5,11 +5,17 @@ from __future__ import (absolute_import, division, print_function, from datetime import datetime import time -from Queue import Queue +import sys +is_py2 = sys.version[0] == '2' +if is_py2: + import Queue as queue +else: + import queue as queue + from tests import fixtures, RQTestCase from tests.helpers import strip_microseconds -from rq.compat import PY2, as_text +from rq.compat import PY2 from rq.exceptions import NoSuchJobError, UnpickleError from rq.job import Job, get_current_job, JobStatus, cancel_job, requeue_job from rq.queue import Queue, get_failed_queue @@ -296,7 +302,7 @@ class TestJob(RQTestCase): def test_unpickleable_result(self): """Unpickleable job result doesn't crash job.to_dict()""" job = Job.create(func=fixtures.say_hello, args=('Lionel',)) - job._result = Queue() + job._result = queue.Queue() data = job.to_dict() self.assertEqual(data['result'], 'Unpickleable return value')