En una aplicación Django intento enviar mails asíncronamente usando Celery, los objetos tasks son creados, pero no llegan a Celery.
Acá muestro los id de los tasks creados:
0: d51a86c2-219d-44a5-a004-2ff00f13f229
1: 47979e10-643c-4ced-808a-bdefb2a1779f
2: e52b0a58-3742-4547-9f37-0fe5b065a8ae
Pero en mi Celery shell.
python manage.py celery worker -l info -Ofair --settings=project.settings.staging
Sólo se ejecutan algunas tareas:
[2016-12-19 17:15:17,582: INFO/MainProcess] Received task: apps.enrollment.tasks.send_email_professor[d51a86c2-219d-44a5-a004-2ff00f13f229]
[2016-12-19 17:15:17,586: WARNING/Worker-5] 0
[2016-12-19 17:15:17,610: INFO/MainProcess] Received task: apps.enrollment.tasks.send_email_purchase[e52b0a58-3742-4547-9f37-0fe5b065a8ae]
[2016-12-19 17:15:17,616: WARNING/Worker-6] 2
[2016-12-19 17:15:17,992: WARNING/Worker-6] email sent
[2016-12-19 17:15:18,152: WARNING/Worker-5] email sent
A veces sólo la 1, a veces 0 y 2, y a veces 1 y 2. Los print los hago al inicio de cada task.
Mi configuración de Celery y Redis es:
BROKER_URL = 'redis://localhost:6379/0'
BROKER_TRANSPORT = 'redis'
BROKER_TRANSPORT_OPTIONS = {'visibility_timeout': 800000,}
CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
CELERYD_POOL_RESTARTS = True
CELERYD_CONCURRENCY = 8
CELERY_ACKS_LATE = True
CELERYD_PREFETCH_MULTIPLIER = 1
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
REDIS_NOTIFICATION_HOST = 'localhost'
REDIS_NOTIFICATION_PORT = 6379
REDIS_NOTIFICATION_DB = 5
Un ejemplo de una de los tasks:
@app.task
def send_email_purchase_notification_admin(enrollment):
print '2'
course = enrollment.course
user = enrollment.user
professor = course.professor
from_email = "Project <[email protected]>"
subject = "Congrats!"
msg = EmailMessage(
subject=subject, from_email=from_email, to=["[email protected]"]
)
msg.template_name = "purchase-notification-admin"
msg.merge_vars = {
"[email protected]": {
'FNAME': ' '.join(
[user.first_name, user.last_name]
) if user.first_name.strip() else user.username,
'COURSE': course.title.title(),
'PROFESSOR': ' '.join([professor.first_name, professor.last_name]),
'PRICE': str(course.price),
},
}
msg.tags = ["payment"]
send_mail(msg)