diff --git a/rq/queue.py b/rq/queue.py index 1b8aacc..3fcb8a0 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -107,9 +107,12 @@ class Queue(object): def fetch_job(self, job_id): try: - return self.job_class.fetch(job_id, connection=self.connection) + job = self.job_class.fetch(job_id, connection=self.connection) except NoSuchJobError: self.remove(job_id) + else: + if job.origin == self.name: + return job def get_job_ids(self, offset=0, length=-1): """Returns a slice of job IDs in the queue.""" diff --git a/tests/test_queue.py b/tests/test_queue.py index 2edb163..015590a 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -462,6 +462,32 @@ class TestQueue(RQTestCase): self.assertEqual(q.job_ids, []) + def test_fetch_job_successful(self): + """Fetch a job from a queue.""" + q = Queue('example') + job_orig = q.enqueue(say_hello) + job_fetch = q.fetch_job(job_orig.id) + self.assertIsNotNone(job_fetch) + self.assertEqual(job_orig.id, job_fetch.id) + self.assertEqual(job_orig.description, job_fetch.description) + + def test_fetch_job_missing(self): + """Fetch a job from a queue which doesn't exist.""" + q = Queue('example') + job = q.fetch_job('123') + self.assertIsNone(job) + + def test_fetch_job_different_queue(self): + """Fetch a job from a queue which is in a different queue.""" + q1 = Queue('example1') + q2 = Queue('example2') + job_orig = q1.enqueue(say_hello) + job_fetch = q2.fetch_job(job_orig.id) + self.assertIsNone(job_fetch) + + job_fetch = q1.fetch_job(job_orig.id) + self.assertIsNotNone(job_fetch) + class TestFailedQueue(RQTestCase): def test_requeue_job(self):