Home > Software engineering >  Docker/Django - How to make sure that all migrations are completed bofor application start?
Docker/Django - How to make sure that all migrations are completed bofor application start?

Time:09-29

at my dockerized Django application I have the following bash function at my docker-entrypoint.sh. This basically only checks if the database is available:

function check_mariadb {
  while ! mysqladmin --user=$MYSQL_USER --password=$MYSQL_PASSWORD --host $MYSQL_HOST ping --silent &> /dev/null; do
    echo "Waiting for MariaDB service to become available"
    sleep 3
    done
    echo "MariaDB is up and available"
}

As my application can start in 3 modes (as application, Celery_worker or Celery_beat) I somehow have to make sure that all migration are done before celery starts. Otherwise I'm running into issues that celery is missing one of these tables:

django_celery_results_chordcounter
django_celery_results_groupresult
django_celery_results_taskresult

Can somebody give me a hint what might be the best practices to check for open migration in this context? And only let celery start if all migrations are done?!... Would be awesome if this could also be handled in a simple bash function like the one above.

Would be awesome If I could do more than just:

python manage.py showmigrations | grep '\[ \]'

Thanks in advance.

CodePudding user response:

In your docker-compose.yaml, you can add a healthcheck to the Django container:

healthcheck:
  test: ["CMD", "curl --fail http://localhost:8000/ || exit 1"]
  interval: 10s
  timeout: 5s
  retries: 5

Then you can add depends_on to your celery/celerybeat container:

depends_on:
  django:
    condition: service_healthy

This will start the celery container only after the django healthcheck passes. In the healthcheck we simply poll localhost:8000, because when the server's returning responses, we can be sure the migrations have been applied.

CodePudding user response:

For the moment I guess that I will stay with this solution:

function check_migrations_status {
  while python manage.py showmigrations | grep '\[ \]'  &> /dev/null; do
    echo "Waiting for all migrations to be completed, please wait ..."
    sleep 3
    done
}
  • Related