Exemplo n.º 1
0
    def test_apply_async(self):

        applied = [0]

        class mocksubtask(Signature):

            def apply_async(self, *args, **kwargs):
                applied[0] += 1

        ts = TaskSet([mocksubtask(MockTask, (i, i))
                        for i in (2, 4, 8)])
        ts.apply_async()
        self.assertEqual(applied[0], 3)

        class Publisher(object):

            def send(self, *args, **kwargs):
                pass

        ts.apply_async(publisher=Publisher())

        # setting current_task

        @current_app.task
        def xyz():
            pass
        from celery._state import _task_stack
        xyz.push_request()
        _task_stack.push(xyz)
        try:
            ts.apply_async(publisher=Publisher())
        finally:
            _task_stack.pop()
            xyz.pop_request()
Exemplo n.º 2
0
    def test_apply_async(self):

        applied = [0]

        class mocksubtask(Signature):
            def apply_async(self, *args, **kwargs):
                applied[0] += 1

        ts = TaskSet([mocksubtask(MockTask, (i, i)) for i in (2, 4, 8)])
        ts.apply_async()
        self.assertEqual(applied[0], 3)

        class Publisher(object):
            def send(self, *args, **kwargs):
                pass

        ts.apply_async(publisher=Publisher())

        # setting current_task

        @current_app.task
        def xyz():
            pass

        from celery._state import _task_stack
        xyz.push_request()
        _task_stack.push(xyz)
        try:
            ts.apply_async(publisher=Publisher())
        finally:
            _task_stack.pop()
            xyz.pop_request()
Exemplo n.º 3
0
 def __call__(self, *args, **kwargs):
     _task_stack.push(self)
     self.push_request()
     try:
         return self.run(*args, **kwargs)
     finally:
         self.pop_request()
         _task_stack.pop()
Exemplo n.º 4
0
 def __call__(self, *args, **kwargs):
     _task_stack.push(self)
     self.push_request(args=args, kwargs=kwargs)
     try:
         return self.run(*args, **kwargs)
     finally:
         self.pop_request()
         _task_stack.pop()
Exemplo n.º 5
0
 def test_current_routing_key(self):
     task = Mock()
     _task_stack.push(task)
     try:
         task.request.reply_to = 'reply_to'
         self.assertEqual(self.b._routing_key('task_id'), 'reply_to')
     finally:
         _task_stack.pop()
Exemplo n.º 6
0
 def test_current_routing_key(self):
     task = Mock()
     _task_stack.push(task)
     try:
         task.request.reply_to = 'reply_to'
         self.assertEqual(self.b._routing_key('task_id'), 'reply_to')
     finally:
         _task_stack.pop()
Exemplo n.º 7
0
 def __call__(self, *args, **kwargs):
     _task_stack.push(self)
     self.push_request()
     try:
         # add self if this is a bound task
         if self.__self__ is not None:
             return self.run(self.__self__, *args, **kwargs)
         return self.run(*args, **kwargs)
     finally:
         self.pop_request()
         _task_stack.pop()
Exemplo n.º 8
0
 def __call__(self, *args, **kwargs):
     _task_stack.push(self)
     self.push_request(args=args, kwargs=kwargs)
     try:
         # add self if this is a bound task
         if self.__self__ is not None:
             return self.run(self.__self__, *args, **kwargs)
         return self.run(*args, **kwargs)
     finally:
         self.pop_request()
         _task_stack.pop()
Exemplo n.º 9
0
 def __call__(self, *args, **kwargs):
     logger = get_logger(__name__)
     handle_sigterm = lambda signum, frame: \
         logger.info('SIGTERM received, waiting till the task finished')
     signal.signal(signal.SIGTERM, handle_sigterm)
     _task_stack.push(self)
     self.push_request(args=args, kwargs=kwargs)
     try:
         return self.run(*args, **kwargs)
     finally:
         self.pop_request()
         _task_stack.pop()
Exemplo n.º 10
0
 def test_apply_async_with_parent(self):
     _task_stack.push(self.add)
     try:
         self.add.push_request(called_directly=False)
         try:
             assert not self.add.request.children
             x = group([self.add.s(4, 4), self.add.s(8, 8)])
             res = x()
             assert self.add.request.children
             assert res in self.add.request.children
             assert len(self.add.request.children) == 1
         finally:
             self.add.pop_request()
     finally:
         _task_stack.pop()
Exemplo n.º 11
0
 def test_apply_async_with_parent(self):
     _task_stack.push(self.add)
     try:
         self.add.push_request(called_directly=False)
         try:
             assert not self.add.request.children
             x = group([self.add.s(4, 4), self.add.s(8, 8)])
             res = x()
             assert self.add.request.children
             assert res in self.add.request.children
             assert len(self.add.request.children) == 1
         finally:
             self.add.pop_request()
     finally:
         _task_stack.pop()
Exemplo n.º 12
0
 def test_apply_async_with_parent(self):
     _task_stack.push(add)
     try:
         add.push_request(called_directly=False)
         try:
             assert not add.request.children
             x = group([add.s(4, 4), add.s(8, 8)])
             res = x()
             self.assertTrue(add.request.children)
             self.assertIn(res, add.request.children)
             self.assertEqual(len(add.request.children), 1)
         finally:
             add.pop_request()
     finally:
         _task_stack.pop()
Exemplo n.º 13
0
 def test_apply_async_with_parent(self):
     _task_stack.push(self.add)
     try:
         self.add.push_request(called_directly=False)
         try:
             assert not self.add.request.children
             x = group([self.add.s(4, 4), self.add.s(8, 8)])
             res = x()
             self.assertTrue(self.add.request.children)
             self.assertIn(res, self.add.request.children)
             self.assertEqual(len(self.add.request.children), 1)
         finally:
             self.add.pop_request()
     finally:
         _task_stack.pop()
Exemplo n.º 14
0
    def test_destination_for(self):
        req = Mock(name='request')
        req.reply_to = 'reply_to'
        req.correlation_id = 'corid'
        assert self.b.destination_for('task_id', req) == ('reply_to', 'corid')
        task = Mock()
        _task_stack.push(task)
        try:
            task.request.reply_to = 'reply_to'
            task.request.correlation_id = 'corid'
            assert self.b.destination_for('task_id', None) == (
                'reply_to', 'corid',
            )
        finally:
            _task_stack.pop()

        with pytest.raises(RuntimeError):
            self.b.destination_for('task_id', None)
Exemplo n.º 15
0
    def test_destination_for(self):
        req = Mock(name='request')
        req.reply_to = 'reply_to'
        req.correlation_id = 'corid'
        assert self.b.destination_for('task_id', req) == ('reply_to', 'corid')
        task = Mock()
        _task_stack.push(task)
        try:
            task.request.reply_to = 'reply_to'
            task.request.correlation_id = 'corid'
            assert self.b.destination_for('task_id', None) == (
                'reply_to', 'corid',
            )
        finally:
            _task_stack.pop()

        with pytest.raises(RuntimeError):
            self.b.destination_for('task_id', None)
Exemplo n.º 16
0
    def test_apply_async_adds_children(self):
        from celery._state import _task_stack

        @self.app.task(bind=True, shared=False)
        def a3cX1(self):
            pass

        @self.app.task(bind=True, shared=False)
        def a3cX2(self):
            pass

        _task_stack.push(a3cX1)
        try:
            a3cX1.push_request(called_directly=False)
            try:
                res = a3cX2.apply_async(add_to_parent=True)
                assert res in a3cX1.request.children
            finally:
                a3cX1.pop_request()
        finally:
            _task_stack.pop()
Exemplo n.º 17
0
    def test_apply_async_adds_children(self):
        from celery._state import _task_stack

        @self.app.task(bind=True, shared=False)
        def a3cX1(self):
            pass

        @self.app.task(bind=True, shared=False)
        def a3cX2(self):
            pass

        _task_stack.push(a3cX1)
        try:
            a3cX1.push_request(called_directly=False)
            try:
                res = a3cX2.apply_async(add_to_parent=True)
                self.assertIn(res, a3cX1.request.children)
            finally:
                a3cX1.pop_request()
        finally:
            _task_stack.pop()
Exemplo n.º 18
0
    def __call__(self, *args, **kw):
        """Run the task.

        Parameters:
            _run_asynchronously_ ... if `True` run the task its own transaction
                                     context, otherwise run it in inline
                                     (optional, default: `False` to be able to
                                      run tasks easily inline)
            _principal_id_ ... run asynchronous task as this user, ignored if
                               running synchronously (optional)

        Returns whatever the task returns itself.

        """
        run_asynchronously = kw.pop('_run_asynchronously_', False)
        principal_id = kw.pop('_principal_id_', None)
        # BBB This was removed in 1.2.0 but there still might be (scheduled)
        # tasks that transmit this argument, so we need to remove it.
        kw.pop('_task_id_', None)

        is_eager = self.app.conf['task_always_eager']
        if is_eager:
            # This is the only part of celery.Task.__call__ that actually is
            # relevant -- since in non-eager mode, it's not called at all:
            # celery.app.trace.build_tracer() says, "if the task doesn't define
            # a custom __call__ method we optimize it away by simply calling
            # the run method directly".
            # (Note that the push_request() call in __call__ would be actively
            # harmful in non-eager mode, since it hides the actual request that
            # was set by app.trace; but as it's not called, it's not an issue.)
            _task_stack.push(self)
        try:
            if run_asynchronously:
                result = self.run_in_worker(principal_id, args, kw)
            else:
                result = self.run_in_same_process(args, kw)
        finally:
            if is_eager:
                _task_stack.pop()
        return result
Exemplo n.º 19
0
    def test_apply_async_adds_children(self):
        from celery._state import _task_stack
        app = Celery(set_as_current=False)

        @app.task()
        def a3cX1(self):
            pass

        @app.task()
        def a3cX2(self):
            pass

        _task_stack.push(a3cX1)
        try:
            a3cX1.push_request(called_directly=False)
            try:
                res = a3cX2.apply_async(add_to_parent=True)
                self.assertIn(res, a3cX1.request.children)
            finally:
                a3cX1.pop_request()
        finally:
            _task_stack.pop()
Exemplo n.º 20
0
 def tearDown(self):
     from celery._state import _task_stack
     _task_stack.pop()
Exemplo n.º 21
0
 def teardown(self):
     from celery._state import _task_stack
     _task_stack.pop()