From dd95dd9f5eb8bcbc84ee468c06f3fd22febdf7a1 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Fri, 19 Jan 2024 12:37:23 +0530 Subject: [PATCH] dev: hold celery worker and beat when pending migrations --- apiserver/bin/beat | 1 + apiserver/bin/worker | 1 + .../commands/wait_for_migrations.py | 21 +++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 apiserver/plane/db/management/commands/wait_for_migrations.py diff --git a/apiserver/bin/beat b/apiserver/bin/beat index 45d357442..6485ea86f 100644 --- a/apiserver/bin/beat +++ b/apiserver/bin/beat @@ -2,4 +2,5 @@ set -e python manage.py wait_for_db +python manage.py wait_for_migrations celery -A plane beat -l info \ No newline at end of file diff --git a/apiserver/bin/worker b/apiserver/bin/worker index 9d2da1254..6d7869993 100755 --- a/apiserver/bin/worker +++ b/apiserver/bin/worker @@ -2,4 +2,5 @@ set -e python manage.py wait_for_db +python manage.py wait_for_migrations celery -A plane worker -l info \ No newline at end of file diff --git a/apiserver/plane/db/management/commands/wait_for_migrations.py b/apiserver/plane/db/management/commands/wait_for_migrations.py new file mode 100644 index 000000000..fc57ce0df --- /dev/null +++ b/apiserver/plane/db/management/commands/wait_for_migrations.py @@ -0,0 +1,21 @@ +# wait_for_migrations.py +import time +from django.core.management.base import BaseCommand +from django.db.migrations.executor import MigrationExecutor +from django.db import connections, DEFAULT_DB_ALIAS + +class Command(BaseCommand): + help = 'Wait for database migrations to complete before starting Celery worker/beat' + + def handle(self, *args, **kwargs): + while self._pending_migrations(): + self.stdout.write("Waiting for database migrations to complete...") + time.sleep(10) # wait for 10 seconds before checking again + + self.stdout.write("All migrations are complete. Safe to start Celery worker/beat.") + + def _pending_migrations(self): + connection = connections[DEFAULT_DB_ALIAS] + executor = MigrationExecutor(connection) + targets = executor.loader.graph.leaf_nodes() + return bool(executor.migration_plan(targets))