Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't catch Celery Operational Error in Django

Tags:

django

celery

I have the following code:

def save()
   super().save(*args, **kwargs)
   try:
       transaction.on_commit(lambda: c_task.delay(a, b, self.pk))
   except Exception as e:
       print(e)

@app.task(bind=True, name='c_task', max_retries=4, soft_time_limit_exception=300)
def c_task(self, a, b, i):

    from .models import ModelA

    try:
        json_data = entity(a,b,i, const)
        .....
    except Exception as e:

        raise self.retry(exc=e, countdown=exponential_backoff(self))

If Redis server fails I receive and OperationalError. I try to catch it using the Celery OperationalError or generic exception as before.

The line is still executing and throw the error. If Redis fails I don't want everything to fail, because my code has a 'workaround', but I can't catch the error.

Traceback:

 transaction.on_commit(lambda: c_task.delay(a, b, self.pk))

\lib\site-packages\celery\app\task.py in delay

            return self.apply_async(args, kwargs)

lib\site-packages\celery\app\task.py in apply_async

                **options

\lib\site-packages\celery\app\base.py in send_task

                    amqp.send_task_message(P, name, message, **options)

\lib\contextlib.py in __exit__

                    self.gen.throw(type, value, traceback)

\lib\site-packages\kombu\connection.py in _reraise_as_library_errors

                        sys.exc_info()[2])

\lib\site-packages\vine\five.py in reraise

                raise value.with_traceback(tb)

\lib\site-packages\kombu\connection.py in _reraise_as_library_errors

                yield
\lib\site-packages\celery\app\base.py in send_task

                    self.backend.on_task_call(P, task_id)
\lib\site-packages\celery\backends\redis.py in on_task_call

                self.result_consumer.consume_from(task_id)

\lib\site-packages\celery\backends\redis.py in consume_from

                return self.start(task_id)

 \lib\site-packages\celery\backends\redis.py in start

            self._consume_from(initial_task_id)

\lib\site-packages\celery\backends\redis.py in _consume_from

                self._pubsub.subscribe(key)
\lib\site-packages\redis\client.py in subscribe

            ret_val = self.execute_command('SUBSCRIBE', *iterkeys(new_channels))

 \lib\site-packages\redis\client.py in execute_command

            self._execute(connection, connection.send_command, *args)

\lib\site-packages\redis\client.py in _execute

                connection.connect()

d\lib\site-packages\redis\connection.py in connect

                raise ConnectionError(self._error_message(e))
like image 748
user3541631 Avatar asked Apr 18 '18 08:04

user3541631


1 Answers

You need to change below

transaction.on_commit(lambda: c_task.delay(a, b, self.pk))

to

def run_task(a, b, pk):
   try:
      c_task.delay(a, b, pk)
   except Exception as ex:
      print(ex)
transaction.on_commit(lambda: run_task(a, b, self.pk))

This will make sure the connection exception is handled

like image 143
Tarun Lalwani Avatar answered Sep 29 '22 09:09

Tarun Lalwani