def test_cleared_context(self): changes = {'id': 'unique id', 'args': ['some', 1], 'wibble': 'wobble'} ctx = Context() ctx.update(changes) ctx.clear() defaults = dict(default_context, children=[]) assert get_context_as_dict(ctx) == defaults assert get_context_as_dict(Context()) == defaults
def test_cleared_context(self): changes = dict(id='unique id', args=['some', 1], wibble='wobble') ctx = Context() ctx.update(changes) ctx.clear() defaults = dict(default_context, children=[]) assert get_context_as_dict(ctx) == defaults assert get_context_as_dict(Context()) == defaults
def test_updated_context(self): expected = dict(default_context) changes = {'id': 'unique id', 'args': ['some', 1], 'wibble': 'wobble'} ctx = Context() expected.update(changes) ctx.update(changes) assert get_context_as_dict(ctx) == expected assert get_context_as_dict(Context()) == default_context
def test_cleared_context(self): changes = dict(id='unique id', args=['some', 1], wibble='wobble') ctx = Context() ctx.update(changes) ctx.clear() defaults = dict(default_context, children=[]) self.assertDictEqual(get_context_as_dict(ctx), defaults) self.assertDictEqual(get_context_as_dict(Context()), defaults)
def test_updated_context(self): expected = dict(default_context) changes = dict(id='unique id', args=['some', 1], wibble='wobble') ctx = Context() expected.update(changes) ctx.update(changes) assert get_context_as_dict(ctx) == expected assert get_context_as_dict(Context()) == default_context
def test_updated_context(self): expected = dict(default_context) changes = dict(id='unique id', args=['some', 1], wibble='wobble') ctx = Context() expected.update(changes) ctx.update(changes) self.assertDictEqual(get_context_as_dict(ctx), expected) self.assertDictEqual(get_context_as_dict(Context()), default_context)
def test_cleared_context(self): changes = dict(id="unique id", args=["some", 1], wibble="wobble") ctx = Context() ctx.update(changes) ctx.clear() defaults = dict(default_context, children=[]) self.assertDictEqual(get_context_as_dict(ctx), defaults) self.assertDictEqual(get_context_as_dict(Context()), defaults)
def test_context_get(self): expected = dict(default_context) changes = dict(id="unique id", args=["some", 1], wibble="wobble") ctx = Context() expected.update(changes) ctx.update(changes) ctx_dict = get_context_as_dict(ctx, getter=Context.get) self.assertDictEqual(ctx_dict, expected) self.assertDictEqual(get_context_as_dict(Context()), default_context)
def mark_as_failure(self, task_id, exc, traceback=None, request=None, store_result=True, call_errbacks=True, state=states.FAILURE): """Mark task as executed with failure.""" if store_result: self.store_result(task_id, exc, state, traceback=traceback, request=request) if request: # This task may be part of a chord if request.chord: self.on_chord_part_return(request, state, exc) # It might also have chained tasks which need to be propagated to, # this is most likely to be exclusive with being a direct part of a # chord but we'll handle both cases separately. # # The `chain_data` try block here is a bit tortured since we might # have non-iterable objects here in tests and it's easier this way. try: chain_data = iter(request.chain) except (AttributeError, TypeError): chain_data = tuple() for chain_elem in chain_data: chain_elem_opts = chain_elem['options'] # If the state should be propagated, we'll do so for all # elements of the chain. This is only truly important so # that the last chain element which controls completion of # the chain itself is marked as completed to avoid stalls. if self.store_result and state in states.PROPAGATE_STATES: try: chained_task_id = chain_elem_opts['task_id'] except KeyError: pass else: self.store_result(chained_task_id, exc, state, traceback=traceback, request=chain_elem) # If the chain element is a member of a chord, we also need # to call `on_chord_part_return()` as well to avoid stalls. if 'chord' in chain_elem_opts: failed_ctx = Context(chain_elem) failed_ctx.update(failed_ctx.options) failed_ctx.id = failed_ctx.options['task_id'] failed_ctx.group = failed_ctx.options['group_id'] self.on_chord_part_return(failed_ctx, state, exc) # And finally we'll fire any errbacks if call_errbacks and request.errbacks: self._call_task_errbacks(request, exc, traceback)
def mark_as_failure(self, task_id, exc, traceback=None, request=None, store_result=True, call_errbacks=True, state=states.FAILURE): """Mark task as executed with failure.""" if store_result: self.store_result(task_id, exc, state, traceback=traceback, request=request) if request: # This task may be part of a chord if request.chord: self.on_chord_part_return(request, state, exc) # It might also have chained tasks which need to be propagated to, # this is most likely to be exclusive with being a direct part of a # chord but we'll handle both cases separately. # # The `chain_data` try block here is a bit tortured since we might # have non-iterable objects here in tests and it's easier this way. try: chain_data = iter(request.chain) except (AttributeError, TypeError): chain_data = tuple() for chain_elem in chain_data: # Reconstruct a `Context` object for the chained task which has # enough information to for backends to work with chain_elem_ctx = Context(chain_elem) chain_elem_ctx.update(chain_elem_ctx.options) chain_elem_ctx.id = chain_elem_ctx.options.get('task_id') chain_elem_ctx.group = chain_elem_ctx.options.get('group_id') # If the state should be propagated, we'll do so for all # elements of the chain. This is only truly important so # that the last chain element which controls completion of # the chain itself is marked as completed to avoid stalls. # # Some chained elements may be complex signatures and have no # task ID of their own, so we skip them hoping that not # descending through them is OK. If the last chain element is # complex, we assume it must have been uplifted to a chord by # the canvas code and therefore the condition below will ensure # that we mark something as being complete as avoid stalling. if ( store_result and state in states.PROPAGATE_STATES and chain_elem_ctx.task_id is not None ): self.store_result( chain_elem_ctx.task_id, exc, state, traceback=traceback, request=chain_elem_ctx, ) # If the chain element is a member of a chord, we also need # to call `on_chord_part_return()` as well to avoid stalls. if 'chord' in chain_elem_ctx.options: self.on_chord_part_return(chain_elem_ctx, state, exc) # And finally we'll fire any errbacks if call_errbacks and request.errbacks: self._call_task_errbacks(request, exc, traceback)
def update_sent_state(sender=None, body=None, exchange=None, routing_key=None, **kwargs): # App may not be loaded on init from django_celery_fulldbresult.models import SCHEDULED task = current_app.tasks.get(sender) save = False status = None schedule_eta = getattr( settings, "DJANGO_CELERY_FULLDBRESULT_SCHEDULE_ETA", False) track_publish = getattr( settings, "DJANGO_CELERY_FULLDBRESULT_TRACK_PUBLISH", False) ignore_result = getattr(task, "ignore_result", False) or\ getattr(settings, "CELERY_IGNORE_RESULT", False) if schedule_eta and body.get("eta") and not body.get("chord")\ and not body.get("taskset"): status = SCHEDULED save = True elif track_publish and not ignore_result: status = PENDING save = True if save: backend = task.backend if task else current_app.backend request = Context() request.update(**body) request.date_submitted = now() request.delivery_info = { "exchange": exchange, "routing_key": routing_key } backend.store_result( body["id"], None, status, traceback=None, request=request) if status == SCHEDULED: raise SchedulingStopPublishing(task_id=body["id"])