""" Celery configuration for ThrillWiki. This module sets up Celery for background task processing including: - Trending calculations - Cache warming - Analytics processing - Email notifications Celery uses the same Django settings module as the main application, which can be configured via DJANGO_SETTINGS_MODULE environment variable. """ import os from celery import Celery from decouple import config # Use the same Django settings module as the main application # Default to production (matching WSGI/ASGI), can be overridden via environment # Honor existing DJANGO_SETTINGS_MODULE if already set if "DJANGO_SETTINGS_MODULE" not in os.environ: os.environ["DJANGO_SETTINGS_MODULE"] = "config.django.production" app = Celery("thrillwiki") # Get Redis URL from environment variable with fallback REDIS_URL = config("REDIS_URL", default="redis://localhost:6379/1") # Celery Configuration - set directly without loading from Django settings first app.conf.update( # Broker settings broker_url=REDIS_URL, result_backend=REDIS_URL, # Task settings task_serializer="json", accept_content=["json"], result_serializer="json", timezone="America/New_York", enable_utc=True, # Worker settings worker_prefetch_multiplier=1, task_acks_late=True, worker_max_tasks_per_child=1000, # Task routing task_routes={ "apps.core.tasks.trending.*": {"queue": "trending"}, "apps.core.tasks.analytics.*": {"queue": "analytics"}, "apps.core.tasks.cache.*": {"queue": "cache"}, }, # Beat schedule for periodic tasks beat_schedule={ "calculate-trending-content": { "task": "apps.core.tasks.trending.calculate_trending_content", "schedule": 300.0, # Every 5 minutes }, "warm-trending-cache": { "task": "apps.core.tasks.trending.warm_trending_cache", "schedule": 900.0, # Every 15 minutes }, "cleanup-old-analytics": { "task": "apps.core.tasks.analytics.cleanup_old_analytics", "schedule": 86400.0, # Daily }, "rides-daily-closing-check": { "task": "rides.check_overdue_closings", "schedule": 86400.0, # Daily at midnight }, }, # Task result settings result_expires=3600, # 1 hour task_ignore_result=False, # Error handling task_reject_on_worker_lost=True, task_soft_time_limit=300, # 5 minutes task_time_limit=600, # 10 minutes ) # Load task modules from all registered Django apps. app.autodiscover_tasks() @app.task(bind=True) def debug_task(self): """Debug task for testing Celery setup.""" print(f"Request: {self.request!r}")