fix periodic tasks getting disabled #517

This commit is contained in:
FuzzyGrim
2025-05-20 20:00:13 +02:00
parent bd17006e16
commit b9919498c5
4 changed files with 21 additions and 20 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 5.2 on 2025-05-20 17:52
from django.db import migrations
def enable_import_tasks(apps, schema_editor):
PeriodicTask = apps.get_model('django_celery_beat', 'PeriodicTask')
PeriodicTask.objects.filter(name__startswith='Import ').update(enabled=True)
class Migration(migrations.Migration):
dependencies = [
('app', '0041_alter_anime_end_date_alter_anime_start_date_and_more'),
('django_celery_beat', '0019_alter_periodictasks_options'),
]
operations = [
migrations.RunPython(enable_import_tasks, reverse_code=migrations.RunPython.noop),
]

View File

@@ -1,7 +1,7 @@
import logging
from celery import states
from celery.signals import before_task_publish, worker_init
from celery.signals import before_task_publish
from django.db.backends.signals import connection_created
from django.dispatch import receiver
from django_celery_results.models import TaskResult
@@ -38,19 +38,3 @@ def create_task_result_on_publish(sender=None, headers=None, body=None, **kwargs
task_args=headers.get("argsrepr", ""),
task_kwargs=headers.get("kwargsrepr", ""),
)
@worker_init.connect(weak=False)
def cleanup_periodic_tasks(*_, **__):
"""Clean up periodic tasks by disabling those not registered."""
from celery import current_app as app
from django_celery_beat.models import PeriodicTask
registered_tasks = [item.get("task") for item in app.conf.beat_schedule.values()]
for periodic_task in PeriodicTask.objects.exclude(
task="celery.backend_cleanup",
).filter(enabled=True):
if periodic_task.task not in registered_tasks:
periodic_task.enabled = False
periodic_task.save()
logger.info("Disabled periodic task: %s", periodic_task.task)

View File

@@ -330,7 +330,6 @@ CELERY_TIMEZONE = TIME_ZONE
CELERY_WORKER_HIJACK_ROOT_LOGGER = False
CELERY_WORKER_CONCURRENCY = 1
CELERY_WORKER_MAX_TASKS_PER_CHILD = 1
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True
CELERY_BEAT_SYNC_EVERY = 1
CELERY_TASK_TRACK_STARTED = True
@@ -340,6 +339,7 @@ CELERY_RESULT_EXTENDED = True
CELERY_RESULT_BACKEND = "django-db"
CELERY_CACHE_BACKEND = "default"
CELERY_RESULT_EXPIRES = 60 * 60 * 24 * 7 # 7 days
CELERY_BEAT_SCHEDULER = "django_celery_beat.schedulers:DatabaseScheduler"
# https://docs.celeryq.dev/en/stable/userguide/configuration.html#task-serializer
CELERY_TASK_SERIALIZER = "pickle"
@@ -366,7 +366,6 @@ CELERY_BEAT_SCHEDULE = {
"schedule": crontab(hour=DAILY_DIGEST_HOUR, minute=0),
},
}
# Allauth settings
if CSRF_TRUSTED_ORIGINS:
# Check if all origins start with http:// or https://

View File

@@ -32,7 +32,7 @@ stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:celery-beat]
command=sh -c 'if [ "${ENV_DEBUG:-False}" = "True" ]; then LOGLEVEL=DEBUG; else LOGLEVEL=INFO; fi; celery --app config beat -S django --loglevel $LOGLEVEL'
command=sh -c 'if [ "${ENV_DEBUG:-False}" = "True" ]; then LOGLEVEL=DEBUG; else LOGLEVEL=INFO; fi; celery --app config beat --loglevel $LOGLEVEL'
user=abc
stopasgroup=true
priority=15