import os
from celery import Celery
from celery.signals import setup_logging

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mnemosyne.settings")

app = Celery("mnemosyne")

# Load configuration from Django settings using 'CELERY_' namespace
app.config_from_object("django.conf:settings", namespace="CELERY")

# Auto-discover tasks from all registered Django app configs
app.autodiscover_tasks()


@setup_logging.connect
def config_loggers(*args, **kwargs):
    """
    Prevent Celery from overriding Django's logging configuration.

    Celery normally replaces all logging handlers at worker startup. Connecting
    to this signal tells Celery to skip its own logging setup entirely, so the
    LOGGING config in settings.py remains in effect for worker processes.
    """
    from logging.config import dictConfig
    from django.conf import settings

    dictConfig(settings.LOGGING)
