1

I have a celery task which takes about 6 hours. At the end of it, Django (or possibly Celery) raises an exception "MySQL server has gone away".

After doing some reading, it appears that this is a known issue with long tasks. I don't (think I have) control over pinging or otherwise mid-task; but the exception is raised after the call which takes time has finished (but still within the task function).

Is there a call I can make within the function to re-establish the connection?

(I have run this task "locally" with the same RDS MySQL DB and not had the issue, but I am getting it when running on an AWS instance.)

Omroth
  • 883
  • 7
  • 24

1 Answers1

1

Eventually found what appears to have worked:

from django.db import close_old_connections
import time

def check_and_retry_django_db_connection():
    close_old_connections()

    db_conn = False
    while not db_conn:
        try:
            connection.ensure_connection()
            db_conn = True
        except OperationalError:
            print('Database unavailable, waiting 1 second...')
            time.sleep(1)

    print('Database available')

The key is the close_old_connections call - ensure_connection will not work otherwise.

Ian

Omroth
  • 883
  • 7
  • 24
  • where do you call this function? – Shil Nevado Jul 29 '19 at 07:34
  • at the end of the task function, after the long call but before the task terminates (and before I need to use the db connection again.) – Omroth Jul 30 '19 at 08:25
  • ah nice, my task has not django package. It's a celery task, a django command management triggers it and it's the demon which gives the error. But I thing daemon process has this problem too, not only tasks, as a mate had the problem in the past with other daemon too. – Shil Nevado Jul 30 '19 at 09:31
  • I'm sorry I can't be more help... let me know if you find the solution, – Omroth Jul 30 '19 at 09:55