In a Django application I try to send emails asynchronously using Celery, the tasks objects are created, but they do not reach Celery.
Here is the id of the created tasks:
0: d51a86c2-219d-44a5-a004-2ff00f13f229
1: 47979e10-643c-4ced-808a-bdefb2a1779f
2: e52b0a58-3742-4547-9f37-0fe5b065a8ae
But in my Celery shell.
python manage.py celery worker -l info -Ofair --settings=project.settings.staging
Only some tasks are executed:
[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
Sometimes only 1, sometimes 0 and 2, and sometimes 1 and 2. I print them at the beginning of each task.
My configuration of Celery and Redis is:
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
An example of one of the 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)