possibility to use celery for asynchronous email
This commit is contained in:
parent
078c0eae8c
commit
4fe47008cb
8 changed files with 63 additions and 3 deletions
5
celeryworker.sh
Executable file
5
celeryworker.sh
Executable file
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
. env/bin/activate
|
||||
|
||||
python manage.py celery worker --loglevel=info
|
|
@ -1,10 +1,14 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.core.mail import send_mass_mail
|
||||
from django.template.loader import render_to_string
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.conf import settings
|
||||
|
||||
if 'djcelery' in settings.INSTALLED_APPS:
|
||||
from issue.tasks import send_mass_mail
|
||||
else:
|
||||
from django.core.mail import send_mass_mail
|
||||
|
||||
from issue.models import *
|
||||
|
||||
|
||||
|
@ -41,6 +45,9 @@ def notify_new_issue(issue):
|
|||
data += [(subject, message,
|
||||
"%s <%s>" % (issue.author.username, from_addr), [dest_addr])]
|
||||
|
||||
if 'djcelery' in settings.INSTALLED_APPS:
|
||||
send_mass_mail.delay(tuple(data))
|
||||
else:
|
||||
send_mass_mail(tuple(data))
|
||||
|
||||
|
||||
|
@ -94,4 +101,7 @@ def notify_event(event, template):
|
|||
data += [(subject, message,
|
||||
'%s <%s>' % (event.author.username, from_addr), [dest_addr])]
|
||||
|
||||
if 'djcelery' in settings.INSTALLED_APPS:
|
||||
send_mass_mail.delay(tuple(data))
|
||||
else:
|
||||
send_mass_mail(tuple(data))
|
||||
|
|
8
issue/tasks.py
Normal file
8
issue/tasks.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
from django.core.mail import send_mass_mail as django_send_mass_mail
|
||||
|
||||
from celery import shared_task
|
||||
|
||||
|
||||
@shared_task
|
||||
def send_mass_mail(datatuple):
|
||||
django_send_mass_mail(datatuple)
|
|
@ -0,0 +1,3 @@
|
|||
from django.conf import settings
|
||||
if 'djcelery' in settings.INSTALLED_APPS:
|
||||
from ponytracker.celery import app as celery_app
|
18
ponytracker/celery.py
Normal file
18
ponytracker/celery.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
import os
|
||||
|
||||
from celery import Celery
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ponytracker.settings')
|
||||
|
||||
app = Celery('ponytracker')
|
||||
|
||||
app.config_from_object('django.conf:settings')
|
||||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def debug_task(self):
|
||||
print('Request: {0!r}'.format(self.request))
|
|
@ -1,6 +1,13 @@
|
|||
from ponytracker.settings import *
|
||||
|
||||
|
||||
### Uncomment to use celery worker
|
||||
### Don't forget to install a broker:
|
||||
### # http://docs.celeryproject.org/en/latest/getting-started/brokers/index.html#broker-overview
|
||||
#INSTALLED_APPS += ('djcelery',)
|
||||
#from djcelery import celery
|
||||
#celery.setup_loader()
|
||||
|
||||
SECRET_KEY='CHANGE ME'
|
||||
|
||||
DEBUG = False
|
||||
|
|
|
@ -142,3 +142,10 @@ BOOTSTRAP3 = {
|
|||
# The complete URL to the Bootstrap JavaScript file (None means derive it from base_url)
|
||||
'javascript_url': None,
|
||||
}
|
||||
|
||||
# Celery configuration
|
||||
BROKER_URL = 'redis://localhost:6379/0'
|
||||
CELERY_RESULT_BACKEND='djcelery.backends.database:DatabaseBackend'
|
||||
CELERY_ACCEPT_CONTENT = ['json']
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_RESULT_SERIALIZER = 'json'
|
||||
|
|
2
requirements.worker.txt
Normal file
2
requirements.worker.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
celery
|
||||
django-celery
|
Loading…
Add table
Add a link
Reference in a new issue