pref: 修改 migrations

pull/8991/head
ibuler 2022-10-26 17:38:32 +08:00
parent 651228795e
commit 8f88b898d0
3 changed files with 21 additions and 34 deletions

View File

@ -10,6 +10,8 @@ class CommonConfig(AppConfig):
def ready(self):
from . import signal_handlers
from .signals import django_ready
if 'migrate' in sys.argv or 'compilemessages' in sys.argv:
return
excludes = ['migrate', 'compilemessages', 'makemigrations']
for i in excludes:
if i in sys.argv:
return
django_ready.send(CommonConfig)

View File

@ -11,6 +11,14 @@ class Migration(migrations.Migration):
]
operations = [
migrations.DeleteModel(name='CeleryTask'),
migrations.CreateModel(
name='CeleryTask',
fields=[
('id', models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False)),
('name', models.CharField(max_length=1024)),
]
),
migrations.CreateModel(
name='CeleryTaskExecution',
fields=[
@ -25,28 +33,4 @@ class Migration(migrations.Migration):
('date_finished', models.DateTimeField(null=True)),
],
),
migrations.RemoveField(
model_name='celerytask',
name='args',
),
migrations.RemoveField(
model_name='celerytask',
name='date_published',
),
migrations.RemoveField(
model_name='celerytask',
name='date_start',
),
migrations.RemoveField(
model_name='celerytask',
name='is_finished',
),
migrations.RemoveField(
model_name='celerytask',
name='kwargs',
),
migrations.RemoveField(
model_name='celerytask',
name='state',
),
]

View File

@ -20,16 +20,17 @@ TASK_LANG_CACHE_TTL = 1800
@receiver(django_ready)
def sync_registered_tasks(*args, **kwargs):
with transaction.atomic():
db_tasks = CeleryTask.objects.all()
try:
db_tasks = CeleryTask.objects.all()
except Exception as e:
return
celery_task_names = [key for key in app.tasks]
db_task_names = [task.name for task in db_tasks]
db_task_names = db_tasks.values_list('name', flat=True)
for task in db_tasks:
if task.name not in celery_task_names:
task.delete()
for task in celery_task_names:
if task not in db_task_names:
CeleryTask(name=task).save()
db_tasks.exclude(name__in=celery_task_names).delete()
not_in_db_tasks = set(celery_task_names) - set(db_task_names)
tasks_to_create = [CeleryTask(name=name) for name in not_in_db_tasks]
CeleryTask.objects.bulk_create(tasks_to_create)
@signals.before_task_publish.connect