Harden Celery schedule parsing and startup safety
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
import logging
|
||||
import os
|
||||
from celery import Celery
|
||||
from celery.schedules import crontab
|
||||
from django.conf import settings
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.development")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
app = Celery("hoopscout")
|
||||
app.config_from_object("django.conf:settings", namespace="CELERY")
|
||||
@ -27,15 +29,25 @@ def _parse_cron_expression(expression: str) -> dict[str, str]:
|
||||
|
||||
def build_periodic_schedule() -> dict:
|
||||
if not settings.INGESTION_SCHEDULE_ENABLED:
|
||||
logger.info("Periodic ingestion schedule disabled by INGESTION_SCHEDULE_ENABLED=0.")
|
||||
return {}
|
||||
|
||||
schedule_kwargs = _parse_cron_expression(settings.INGESTION_SCHEDULE_CRON)
|
||||
return {
|
||||
"ingestion.scheduled_provider_sync": {
|
||||
"task": "apps.ingestion.tasks.scheduled_provider_sync",
|
||||
"schedule": crontab(**schedule_kwargs),
|
||||
try:
|
||||
schedule_kwargs = _parse_cron_expression(settings.INGESTION_SCHEDULE_CRON)
|
||||
return {
|
||||
"ingestion.scheduled_provider_sync": {
|
||||
"task": "apps.ingestion.tasks.scheduled_provider_sync",
|
||||
"schedule": crontab(**schedule_kwargs),
|
||||
}
|
||||
}
|
||||
}
|
||||
except Exception as exc: # noqa: BLE001
|
||||
logger.error(
|
||||
"Invalid periodic ingestion schedule config. Task disabled. "
|
||||
"INGESTION_SCHEDULE_CRON=%r error=%s",
|
||||
settings.INGESTION_SCHEDULE_CRON,
|
||||
exc,
|
||||
)
|
||||
return {}
|
||||
|
||||
|
||||
app.conf.beat_schedule = build_periodic_schedule()
|
||||
|
||||
Reference in New Issue
Block a user