changeset 513:9e7ae8462f3f

Using celery to send mail out of the request/response cycle.
author Brian Neal <bgneal@gmail.com>
date Tue, 13 Dec 2011 02:07:55 +0000
parents 3fd369e1197f
children 6d816aa586c1
files gpp/accounts/forms.py gpp/core/functions.py gpp/core/tasks.py gpp/settings/base.py
diffstat 4 files changed, 32 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/accounts/forms.py	Sun Dec 11 21:31:10 2011 +0000
+++ b/gpp/accounts/forms.py	Tue Dec 13 02:07:55 2011 +0000
@@ -144,8 +144,7 @@
                 })
 
         subject = 'Registration Confirmation for ' + site.name
-        send_mail(subject, msg, admin_email, [self.cleaned_data['email']],
-                expedite=True)
+        send_mail(subject, msg, admin_email, [self.cleaned_data['email']])
         logging.info('Accounts/registration conf. email sent to %s for user %s; IP = %s',
                 self.cleaned_data['email'], pending_user.username, self.ip)
 
--- a/gpp/core/functions.py	Sun Dec 11 21:31:10 2011 +0000
+++ b/gpp/core/functions.py	Tue Dec 13 02:07:55 2011 +0000
@@ -3,34 +3,35 @@
 import re
 import logging
 
-import django.core.mail
 from django.contrib.sites.models import Site
 from django.conf import settings
 
-import mailer
+import core.tasks
 
 
-def send_mail(subject, message, from_email, recipient_list,
-        fail_silently=False, auth_user=None, auth_password=None,
-        expedite=False):
-    """The main gpp send email function.
-    Use this function to send email from the site. It will obey debug settings and
-    log all emails. The expedite flag, when True, will bypass the mail queue.
+def send_mail(subject, message, from_email, recipient_list, **kwargs):
     """
+    The main send email function. Use this function to send email from the
+    site. All applications should use this function instead of calling
+    Django's directly.
+    If settings.GPP_SEND_EMAIL is true, the email will be sent to a Celery task
+    to actually send the email. Otherwise it is dropped. In any event, the
+    email will be logged at the DEBUG level.
+
+    """
+    # Guard against empty email addresses
     recipient_list = [dest for dest in recipient_list if dest]
     if not recipient_list:
         logging.warning("Empty recipient_list in send_mail")
         return
 
-    if settings.MAILER_ENQUEUE_MAIL and not expedite:
-        mailer.enqueue_mail(subject, message, from_email, recipient_list)
-    elif settings.GPP_SEND_EMAIL:
-        django.core.mail.send_mail(subject, message, from_email, recipient_list,
-                fail_silently, auth_user, auth_password)
-
     logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s',
         from_email, str(recipient_list), subject, message)
 
+    if settings.GPP_SEND_EMAIL:
+        core.tasks.send_mail.delay(subject, message, from_email, recipient_list,
+                **kwargs)
+
 
 def email_admins(subject, message):
     """Emails the site admins. Goes through the site send_mail function."""
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/core/tasks.py	Tue Dec 13 02:07:55 2011 +0000
@@ -0,0 +1,16 @@
+"""
+Celery tasks for the core application.
+
+"""
+from celery.task import task
+import django.core.mail
+
+
+@task
+def add(x, y):
+    return x + y
+
+@task
+def send_mail(subject, message, from_email, recipient_list, **kwargs):
+    django.core.mail.send_mail(subject, message, from_email, recipient_list,
+            **kwargs)
--- a/gpp/settings/base.py	Sun Dec 11 21:31:10 2011 +0000
+++ b/gpp/settings/base.py	Tue Dec 13 02:07:55 2011 +0000
@@ -219,7 +219,6 @@
 #######################################################################
 GPP_LOG_LEVEL = 0
 GPP_SEND_EMAIL = True
-MAILER_ENQUEUE_MAIL = True
 GPP_NO_REPLY_EMAIL = 'no_reply'
 AVATAR_DIR = 'avatars'
 MAX_AVATAR_SIZE_BYTES = 2 * 1024 * 1024