dev: hold celery worker and beat when pending migrations

This commit is contained in:
pablohashescobar 2024-01-19 12:37:23 +05:30
parent e43139726f
commit dd95dd9f5e
3 changed files with 23 additions and 0 deletions

View File

@ -2,4 +2,5 @@
set -e
python manage.py wait_for_db
python manage.py wait_for_migrations
celery -A plane beat -l info

View File

@ -2,4 +2,5 @@
set -e
python manage.py wait_for_db
python manage.py wait_for_migrations
celery -A plane worker -l info

View File

@ -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))