diff --git a/rq/job.py b/rq/job.py index 380783fa..6faefa96 100644 --- a/rq/job.py +++ b/rq/job.py @@ -57,7 +57,10 @@ def get_current_job(): """Returns the Job instance that is currently being executed. If this function is invoked from outside a job context, None is returned. """ - return _job_stack.top + job_id = _job_stack.top + if job_id is None: + return None + return Job.fetch(job_id) class Job(object): @@ -332,11 +335,11 @@ class Job(object): # Job execution def perform(self): # noqa """Invokes the job function with the job arguments.""" - _job_stack.push(self) + _job_stack.push(self.id) try: self._result = self.func(*self.args, **self.kwargs) finally: - assert self == _job_stack.pop() + assert self.id == _job_stack.pop() return self._result diff --git a/tests/test_job.py b/tests/test_job.py index 0d67a17f..f75d8788 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -209,4 +209,7 @@ class TestJob(RQTestCase): # Executing the job function from outside of RQ throws an exception job = Job.create(func=access_self) - self.assertEqual(job.perform(), job.id) + job.save() + id = job.perform() + self.assertEqual(job.id, id) + self.assertEqual(job.func, access_self)