def trace_task(uuid, args, kwargs, request=None): R = I = None kwargs = kwdict(kwargs) try: _task_stack.push(task) task_request = request push_request(task_request) try: # -*- PRE -*- if prerun_receivers: send_prerun(sender=task, task_id=uuid, task=task, args=args, kwargs=kwargs) loader_task_init(uuid, task) if track_started: store_result(uuid, {"pid": pid, "hostname": hostname}, STARTED) # -*- TRACE -*- try: R = retval = fun(*args, **kwargs) state = SUCCESS except RetryTaskError, exc: I = Info(RETRY, exc) state, retval = I.state, I.retval R = I.handle_error_state(task, eager=eager) except Exception, exc: if propagate: raise I = Info(FAILURE, exc) state, retval = I.state, I.retval R = I.handle_error_state(task, eager=eager) [subtask(errback).apply_async((uuid, )) for errback in task_request.errbacks or []] except BaseException, exc: raise
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 _task_stack.push(xyz) try: ts.apply_async(publisher=Publisher()) finally: _task_stack.pop() xyz.pop_request()
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()
def test_apply_async_with_parent(self): _task_stack.push(add) try: x = group([add.s(4, 4), add.s(8, 8)]) x.apply_async() self.assertTrue(add.request.children) finally: _task_stack.pop()
def setup(self): logger = self.logger = get_logger("celery.task") logger.handlers = [] logging.root.manager.loggerDict.pop(logger.name, None) self.uid = uuid() @current_app.task def test_task(): pass test_task.logger.handlers = [] self.task = test_task from celery.state import _task_stack _task_stack.push(test_task)
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()
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()