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))
                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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With