mirror of
https://github.com/pretix/pretix.git
synced 2026-01-05 21:32:26 +00:00
Refs #44 -- Added optional celery background queue for mails
This commit is contained in:
@@ -210,4 +210,21 @@ to speed up various operations::
|
||||
If no redis is configured, pretix will store sessions and locks in the database. If memcached
|
||||
is configured, memcached will be used for caching instead of redis.
|
||||
|
||||
.. _Python documentation: https://docs.python.org/3/library/configparser.html?highlight=configparser#supported-ini-file-structure
|
||||
Celery task queue
|
||||
-----------------
|
||||
|
||||
For processing long-running tasks asynchronously, pretix needs help of the celery task queue.
|
||||
For communicating between the web server and the task workers in both direction, a messaging
|
||||
queue and a result backend is needed. You can use a redis database for both directions, or
|
||||
an AMQP server (e.h. RabbitMQ) as a broker and redis or your database as a result backend::
|
||||
|
||||
[celery]
|
||||
broker=amqp://guest:guest@localhost:5672//
|
||||
backend=redis://localhost/0
|
||||
|
||||
RabbitMQ might be the better choice if you have a complex, multi-server, high-performance setup,
|
||||
but as you already should have a redis instance ready for session and lock storage, we recommend
|
||||
redis for convenience. See the `Celery documentation`_ for more details.
|
||||
|
||||
.. _Python documentation: https://docs.python.org/3/library/configparser.html?highlight=configparser#supported-ini-file-structure
|
||||
.. _Celery documentation: http://docs.celeryproject.org/en/latest/configuration.html
|
||||
@@ -1 +1,6 @@
|
||||
__version__ = "0.0.0"
|
||||
|
||||
try:
|
||||
from .celery import app as celery_app
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@@ -2,7 +2,6 @@ import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail import EmailMessage
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template.loader import get_template
|
||||
from django.utils import translation
|
||||
from django.utils.translation import ugettext as _
|
||||
@@ -66,10 +65,16 @@ def mail(user: User, subject: str, template: str, context: dict=None, event: Eve
|
||||
}
|
||||
)
|
||||
body += "\r\n"
|
||||
try:
|
||||
return mail_send([user.email], subject, body, sender)
|
||||
finally:
|
||||
translation.activate(_lng)
|
||||
|
||||
|
||||
def mail_send(to, subject, body, sender):
|
||||
email = EmailMessage(
|
||||
subject, body, sender,
|
||||
to=[user.email]
|
||||
to=to
|
||||
)
|
||||
|
||||
try:
|
||||
@@ -78,5 +83,10 @@ def mail(user: User, subject: str, template: str, context: dict=None, event: Eve
|
||||
except Exception:
|
||||
logger.exception('Error sending e-mail')
|
||||
return False
|
||||
finally:
|
||||
translation.activate(_lng)
|
||||
|
||||
|
||||
if settings.HAS_CELERY:
|
||||
from pretix.celery import app
|
||||
|
||||
mail_send_task = app.task(mail_send)
|
||||
mail_send = lambda *args, **kwargs: mail_send_task.apply_async(args=args, kwargs=kwargs)
|
||||
|
||||
12
src/pretix/celery.py
Normal file
12
src/pretix/celery.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import os
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pretix.settings")
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
if settings.HAS_CELERY:
|
||||
from celery import Celery
|
||||
app = Celery('pretix')
|
||||
|
||||
app.config_from_object('django.conf:settings')
|
||||
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
||||
@@ -114,6 +114,12 @@ if HAS_REDIS:
|
||||
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
|
||||
SESSION_CACHE_ALIAS = "redis"
|
||||
|
||||
HAS_CELERY = config.has_option('celery', 'broker')
|
||||
if HAS_CELERY:
|
||||
BROKER_URL = config.get('celery', 'broker')
|
||||
CELERY_RESULT_BACKEND = config.get('celery', 'backend')
|
||||
CELERY_SEND_TASK_ERROR_EMAILS = bool(ADMINS)
|
||||
|
||||
# Internal settings
|
||||
|
||||
STATIC_ROOT = '_static'
|
||||
@@ -330,3 +336,6 @@ LOGGING = {
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_RESULT_SERIALIZER = 'json'
|
||||
|
||||
2
src/requirements/celery.txt
Normal file
2
src/requirements/celery.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
celery>=3.1,<3.2
|
||||
|
||||
@@ -12,4 +12,4 @@ known_first_party = pretix
|
||||
known_third_party = versions
|
||||
multi_line_output = 5
|
||||
not_skip = __init__.py
|
||||
skip = make_testdata.py,wsgi.py,bootstrap
|
||||
skip = make_testdata.py,wsgi.py,bootstrap,celery.py
|
||||
|
||||
Reference in New Issue
Block a user