Example #1
0
    def test_create_and_cancel_job_enqueue_dependents_in_registry(self):
        """Ensure job.cancel() works properly with enqueue_dependents=True and when the job is in a registry"""
        queue = Queue(connection=self.testconn)
        dependency = queue.enqueue(fixtures.raise_exc)
        dependent = queue.enqueue(fixtures.say_hello, depends_on=dependency)

        self.assertEqual(1, len(queue.get_jobs()))
        self.assertEqual(1, len(queue.deferred_job_registry))
        w = Worker([queue])
        w.work(burst=True, max_jobs=1)
        dependency.refresh()
        dependent.refresh()
        self.assertEqual(0, len(queue.get_jobs()))
        self.assertEqual(1, len(queue.deferred_job_registry))
        self.assertEqual(1, len(queue.failed_job_registry))
        cancel_job(dependency.id, enqueue_dependents=True)
        dependency.refresh()
        dependent.refresh()
        self.assertEqual(1, len(queue.get_jobs()))
        self.assertEqual(0, len(queue.deferred_job_registry))
        self.assertEqual(0, len(queue.failed_job_registry))
        self.assertEqual(1, len(queue.canceled_job_registry))
        registry = CanceledJobRegistry(connection=self.testconn, queue=queue)
        self.assertIn(dependency, registry)
        self.assertEqual(dependency.get_status(), JobStatus.CANCELED)
        self.assertNotIn(dependency, queue.failed_job_registry)
        self.assertIn(dependent, queue.get_jobs())
        self.assertEqual(dependent.get_status(), JobStatus.QUEUED)
        # If job is deleted, it's also removed from CanceledJobRegistry
        dependency.delete()
        self.assertNotIn(dependency, registry)
Example #2
0
 def test_create_and_cancel_job(self):
     """test creating and using cancel_job deletes job properly"""
     queue = Queue(connection=self.testconn)
     job = queue.enqueue(fixtures.say_hello)
     self.assertEqual(1, len(queue.get_jobs()))
     cancel_job(job.id)
     self.assertEqual(0, len(queue.get_jobs()))
Example #3
0
 def test_create_and_cancel_job_with_serializer(self):
     """test creating and using cancel_job (with serializer) deletes job properly"""
     queue = Queue(connection=self.testconn, serializer=JSONSerializer)
     job = queue.enqueue(fixtures.say_hello)
     self.assertEqual(1, len(queue.get_jobs()))
     cancel_job(job.id, serializer=JSONSerializer)
     self.assertEqual(0, len(queue.get_jobs()))
Example #4
0
 def test_create_and_cancel_job(self):
     """test creating and using cancel_job deletes job properly"""
     queue = Queue(connection=self.testconn)
     job = queue.enqueue(fixtures.say_hello)
     self.assertEqual(1, len(queue.get_jobs()))
     cancel_job(job.id)
     self.assertEqual(0, len(queue.get_jobs()))
Example #5
0
 def test_create_failed_and_cancel_job(self):
     """test creating and using cancel_job deletes job properly"""
     failed_queue = get_failed_queue(connection=self.testconn)
     job = failed_queue.enqueue(fixtures.say_hello)
     job.set_status(JobStatus.FAILED)
     self.assertEqual(1, len(failed_queue.get_jobs()))
     cancel_job(job.id)
     self.assertEqual(0, len(failed_queue.get_jobs()))
Example #6
0
 def test_create_failed_and_cancel_job(self):
     """test creating and using cancel_job deletes job properly"""
     failed_queue = get_failed_queue(connection=self.testconn)
     job = failed_queue.enqueue(fixtures.say_hello)
     job.set_status(JobStatus.FAILED)
     self.assertEqual(1, len(failed_queue.get_jobs()))
     cancel_job(job.id)
     self.assertEqual(0, len(failed_queue.get_jobs()))
Example #7
0
    def test_create_and_cancel_job(self):
        """Ensure job.cancel() works properly"""
        queue = Queue(connection=self.testconn)
        job = queue.enqueue(fixtures.say_hello)
        self.assertEqual(1, len(queue.get_jobs()))
        cancel_job(job.id)
        self.assertEqual(0, len(queue.get_jobs()))
        registry = CanceledJobRegistry(connection=self.testconn, queue=queue)
        self.assertIn(job, registry)
        self.assertEqual(job.get_status(), JobStatus.CANCELED)

        # If job is deleted, it's also removed from CanceledJobRegistry
        job.delete()
        self.assertNotIn(job, registry)
Example #8
0
    def test_create_and_cancel_job_fails_already_canceled(self):
        """Ensure job.cancel() fails on already canceld job"""
        queue = Queue(connection=self.testconn)
        job = queue.enqueue(fixtures.say_hello, job_id='fake_job_id')
        self.assertEqual(1, len(queue.get_jobs()))

        # First cancel should be fine
        cancel_job(job.id)
        self.assertEqual(0, len(queue.get_jobs()))
        registry = CanceledJobRegistry(connection=self.testconn, queue=queue)
        self.assertIn(job, registry)
        self.assertEqual(job.get_status(), JobStatus.CANCELED)

        # Second cancel should fail
        self.assertRaisesRegex(
            InvalidJobOperation,
            r'Cannot cancel already canceled job: fake_job_id', cancel_job,
            job.id)
Example #9
0
    def test_create_and_cancel_job_enqueue_dependents(self):
        """Ensure job.cancel() works properly with enqueue_dependents=True"""
        queue = Queue(connection=self.testconn)
        dependency = queue.enqueue(fixtures.say_hello)
        dependent = queue.enqueue(fixtures.say_hello, depends_on=dependency)

        self.assertEqual(1, len(queue.get_jobs()))
        self.assertEqual(1, len(queue.deferred_job_registry))
        cancel_job(dependency.id, enqueue_dependents=True)
        self.assertEqual(1, len(queue.get_jobs()))
        self.assertEqual(0, len(queue.deferred_job_registry))
        registry = CanceledJobRegistry(connection=self.testconn, queue=queue)
        self.assertIn(dependency, registry)
        self.assertEqual(dependency.get_status(), JobStatus.CANCELED)
        self.assertIn(dependent, queue.get_jobs())
        self.assertEqual(dependent.get_status(), JobStatus.QUEUED)
        # If job is deleted, it's also removed from CanceledJobRegistry
        dependency.delete()
        self.assertNotIn(dependency, registry)
Example #10
0
    def job(self, *args):
        args = list(args)
        id = args.pop(0)

        Data.connect()

        if not args:
            return Data.job(id)
        else:
            action = args.pop(0)
        if action == 'requeue':
            requeue_job(id)
        elif action == 'delete':
            try:
                Job(id).refresh()
                cancel_job(id)
            except Exception, e:
                if RAISE_EXCEPTIONS:
                    raise e
                else:
                    logger.warn(e)
    def job(self, *args):
        args = list(args)
        id = args.pop(0)

        Data.connect()

        if not args:
            return Data.job(id)
        else:
            action = args.pop(0)
        if action == "requeue":
            requeue_job(id)
        elif action == "delete":
            try:
                Job(id).refresh()
                cancel_job(id)
            except Exception, e:
                if RAISE_EXCEPTIONS:
                    raise e
                else:
                    logger.warn(e)