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): def ready(self):
from . import signal_handlers from . import signal_handlers
from .signals import django_ready from .signals import django_ready
if 'migrate' in sys.argv or 'compilemessages' in sys.argv: excludes = ['migrate', 'compilemessages', 'makemigrations']
for i in excludes:
if i in sys.argv:
return return
django_ready.send(CommonConfig) django_ready.send(CommonConfig)

View File

@ -11,6 +11,14 @@ class Migration(migrations.Migration):
] ]
operations = [ 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( migrations.CreateModel(
name='CeleryTaskExecution', name='CeleryTaskExecution',
fields=[ fields=[
@ -25,28 +33,4 @@ class Migration(migrations.Migration):
('date_finished', models.DateTimeField(null=True)), ('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) @receiver(django_ready)
def sync_registered_tasks(*args, **kwargs): def sync_registered_tasks(*args, **kwargs):
with transaction.atomic(): with transaction.atomic():
try:
db_tasks = CeleryTask.objects.all() db_tasks = CeleryTask.objects.all()
except Exception as e:
return
celery_task_names = [key for key in app.tasks] 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: db_tasks.exclude(name__in=celery_task_names).delete()
if task.name not in celery_task_names: not_in_db_tasks = set(celery_task_names) - set(db_task_names)
task.delete() tasks_to_create = [CeleryTask(name=name) for name in not_in_db_tasks]
for task in celery_task_names: CeleryTask.objects.bulk_create(tasks_to_create)
if task not in db_task_names:
CeleryTask(name=task).save()
@signals.before_task_publish.connect @signals.before_task_publish.connect