import os
import logging
from logging import handlers
from celery import Celery
from celery.signals import setup_logging
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'company.settings')
# Создание асинхронной очереди и загрузка конфигурации.
app = Celery('company')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Настройка логирования для Celery.
@setup_logging.connect
def config_loggers(*args, **kwargs) -> None:
logger = logging.getLogger('celery')
logger.setLevel(logging.INFO)
formatter = logging.Formatter(
fmt='[{levelname}]-[{asctime}]-[{module}]-[{process:d}]-[{thread:d}]: {message}',
style='{',
)
handler = handlers.RotatingFileHandler(
filename=settings.LOGS_DIR / 'celery/worker.log',
maxBytes=300,
backupCount=5,
)
handler.setFormatter(formatter)
logger.addHandler(handler)
# Поиск всех тасок.
app.autodiscover_tasks()
celery -A company worker --loglevel=info --concurrency=1 -P eventlet
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'company.settings')
# Поиск всех тасок.
app.autodiscover_tasks(['company.application_handler'])