When executing async task celery is raising an exception
#case where error is thrown
send_registration_email.delay("test", "[email protected]", {})
This error does not appear when I execute code by omitting celery
#case where code is executed correctly
send_registration_email("test", "[email protected]", {})
How can I execute my async task with celery, so I will and get rid of this error ?
Error
[2015-10-12 14:50:57,176: ERROR/MainProcess] Task tasks.core.email.send_registration_email[5f96bee3-9df7-42ce-b726-c7086e82b954] raised unexpected: NameError("global name 'Mailer' is not defined",)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/srv/www/compare/htdocs/tasks/core/email.py", line 6, in send_registration_email
@shared_task
NameError: global name 'Mailer' is not defined
Celery Task
# email.py
from __future__ import absolute_import
from celery import shared_task
from utilities.helpers.mailer import Mailer
@shared_task
def send_registration_email(email_type="", recipient="", data={}):
Mailer.send_email(email_type, recipient, data)
Mailer Class
# mailer.py
from __future__ import absolute_import
from django.core.mail.message import EmailMultiAlternatives
from django.template.loader import get_template
class Mailer():
@staticmethod
def send_email(email_type="", recipient="", data={}):
try:
email = Mailer.create_email(email_type, recipient, data)
email.send()
return True
except Exception as e:
return False
@classmethod
def create_email(self, email_type, recipient, data):
subject = ""
message = ""
sender_email = "[email protected]"
if email_type == "test":
subject = "Test subject"
content_html = "<html><body><h1>Test</h1></body></html>"
email = EmailMultiAlternatives(subject, None, sender_email, [recipient])
email.attach_alternative(content_html, "text/html")
return email
from utilities.helpers.mailer import Mailer?