From 03bd49511dd6ce3849583281004ede00e07fb4b0 Mon Sep 17 00:00:00 2001 From: Omar Khan Date: Tue, 17 Jul 2012 11:48:41 +0100 Subject: [PATCH] Allow instance methods to be enqueued Only works for picklable instances --- rq/job.py | 19 ++++++++++++++++--- tests/fixtures.py | 9 +++++++++ tests/test_job.py | 16 ++++++++++++++-- tests/test_queue.py | 15 ++++++++++++++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/rq/job.py b/rq/job.py index 90c3a0e..b14b743 100644 --- a/rq/job.py +++ b/rq/job.py @@ -1,4 +1,5 @@ import importlib +import inspect import times from uuid import uuid4 from cPickle import loads, dumps, UnpicklingError @@ -50,7 +51,11 @@ class Job(object): """ connection = kwargs.pop('connection', None) job = cls(connection=connection) - job._func_name = '%s.%s' % (func.__module__, func.__name__) + if inspect.ismethod(func): + job._instance = func.im_self + job._func_name = func.__name__ + else: + job._func_name = '%s.%s' % (func.__module__, func.__name__) job._args = args job._kwargs = kwargs job.description = job.get_call_string() @@ -66,10 +71,17 @@ class Job(object): if func_name is None: return None + if self.instance: + return getattr(self.instance, func_name) + module_name, func_name = func_name.rsplit('.', 1) module = importlib.import_module(module_name) return getattr(module, func_name) + @property + def instance(self): + return self._instance + @property def args(self): return self._args @@ -100,6 +112,7 @@ class Job(object): self._id = id self.created_at = times.now() self._func_name = None + self._instance = None self._args = None self._kwargs = None self.description = None @@ -141,7 +154,7 @@ class Job(object): def job_tuple(self): """Returns the job tuple that encodes the actual function call that this job represents.""" - return (self.func_name, self.args, self.kwargs) + return (self.func_name, self.instance, self.args, self.kwargs) @property def return_value(self): @@ -190,7 +203,7 @@ class Job(object): else: return times.to_universal(date_str) - self._func_name, self._args, self._kwargs = unpickle(data) + self._func_name, self._instance, self._args, self._kwargs = unpickle(data) self.created_at = to_date(created_at) self.origin = origin self.description = description diff --git a/tests/fixtures.py b/tests/fixtures.py index 36d0c8a..32003de 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -39,3 +39,12 @@ def create_file(path): def create_file_after_timeout(path, timeout): time.sleep(timeout) create_file(path) + + +class Calculator(object): + """Test instance methods.""" + def __init__(self, denominator): + self.denominator = denominator + + def calculate(x, y): + return x * y / self.denominator diff --git a/tests/test_job.py b/tests/test_job.py index 453ed06..18fe60f 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -1,7 +1,7 @@ import times from datetime import datetime from tests import RQTestCase -from tests.fixtures import some_calculation, say_hello +from tests.fixtures import Calculator, some_calculation, say_hello from tests.helpers import strip_milliseconds from cPickle import loads from rq.job import Job @@ -19,6 +19,7 @@ class TestJob(RQTestCase): # ...and nothing else self.assertIsNone(job.func) + self.assertIsNone(job.instance) self.assertIsNone(job.args) self.assertIsNone(job.kwargs) self.assertIsNone(job.origin) @@ -35,6 +36,7 @@ class TestJob(RQTestCase): self.assertIsNotNone(job.id) self.assertIsNotNone(job.created_at) self.assertIsNotNone(job.description) + self.assertIsNone(job.instance) # Job data is set... self.assertEquals(job.func, some_calculation) @@ -46,6 +48,15 @@ class TestJob(RQTestCase): self.assertIsNone(job.enqueued_at) self.assertIsNone(job.return_value) + def test_create_instance_method_job(self): + """Creation of jobs for instance methods.""" + c = Calculator(2) + job = Job.create(c.calculate, 3, 4) + + # Job data is set + self.assertEquals(job.func, c.calculate) + self.assertEquals(job.instance, c) + self.assertEquals(job.args, (3, 4)) def test_save(self): # noqa """Storing jobs.""" @@ -64,7 +75,7 @@ class TestJob(RQTestCase): """Fetching jobs.""" # Prepare test self.testconn.hset('rq:job:some_id', 'data', - "(S'tests.fixtures.some_calculation'\np0\n(I3\nI4\ntp1\n(dp2\nS'z'\np3\nI2\nstp4\n.") # noqa + "(S'tests.fixtures.some_calculation'\nN(I3\nI4\nt(dp1\nS'z'\nI2\nstp2\n.") # noqa self.testconn.hset('rq:job:some_id', 'created_at', "2012-02-07 22:13:24+0000") @@ -72,6 +83,7 @@ class TestJob(RQTestCase): job = Job.fetch('some_id') self.assertEquals(job.id, 'some_id') self.assertEquals(job.func_name, 'tests.fixtures.some_calculation') + self.assertIsNone(job.instance) self.assertEquals(job.args, (3, 4)) self.assertEquals(job.kwargs, dict(z=2)) self.assertEquals(job.created_at, datetime(2012, 2, 7, 22, 13, 24)) diff --git a/tests/test_queue.py b/tests/test_queue.py index a75abf5..b39b196 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -1,5 +1,5 @@ from tests import RQTestCase -from tests.fixtures import say_hello, div_by_zero +from tests.fixtures import Calculator, say_hello, div_by_zero from rq import Queue, get_failed_queue from rq.job import Job from rq.exceptions import InvalidJobOperationError @@ -132,6 +132,19 @@ class TestQueue(RQTestCase): # ...and assert the queue count when down self.assertEquals(q.count, 0) + def test_dequeue_instance_method(self): + """Dequeueing instance method jobs from queues.""" + q = Queue() + c = Calculator(2) + result = q.enqueue(c.calculate, 3, 4) + + job = q.dequeue() + # The instance has been pickled and unpickled, so it is now a separate + # object. Test for equality using each object's __dict__ instead. + self.assertEquals(job.instance.__dict__, c.__dict__) + self.assertEquals(job.func.__name__, 'calculate') + self.assertEquals(job.args, (3, 4)) + def test_dequeue_ignores_nonexisting_jobs(self): """Dequeuing silently ignores non-existing jobs."""