python - Pass parameter to Celery task's on_failure method -
i'm creating custom celery task class in order override happens if/when task reaches max retries (on_failure). need update user model's status if task fails.
below custom task class:
class readytask(task): def run(self, user): try: user.get_results() except exception exc: raise self.retry(exc=exc, max_retries=3) def on_failure(self, exc, task_id, *args, **kwargs): user.status = status.ready user.save() how can pass user object on_failure() method update status?
i believe can inspect args or kwargs user's id if send task call it, parameter. easier if make in kwargs don't have arg position checking. grab user id , make change?
thus, not send run function instead argument/keyword-argument function task calling, via function.apply(kwargs), or function.apply_async(kwargs=kwargs) or function.delay(kwargs).
so:
user_id = kwargs.get('user_id')
# resolve user object, update object
Comments
Post a Comment