From d6f85f07dfe19c46bce67cf21d086c0393039437 Mon Sep 17 00:00:00 2001 From: Marc Aymerich Date: Mon, 9 Nov 2015 19:07:06 +0000 Subject: [PATCH] Fixed mailer connection issues --- .../bills/templates/bills/microspective.html | 3 +- orchestra/contrib/mailer/backends.py | 8 ++++- orchestra/contrib/mailer/engine.py | 30 +++++++++++-------- orchestra/contrib/mailer/tasks.py | 4 +-- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/orchestra/contrib/bills/templates/bills/microspective.html b/orchestra/contrib/bills/templates/bills/microspective.html index 86b1cbbf..98a9f4c8 100644 --- a/orchestra/contrib/bills/templates/bills/microspective.html +++ b/orchestra/contrib/bills/templates/bills/microspective.html @@ -27,7 +27,8 @@ {{ seller.get_name }}
-

{{ seller.address }}
+

{{ seller.vat }}
+ {{ seller.address }}
{{ seller.zipcode }} - {% trans seller.city %}
{% trans seller.get_country_display %}

diff --git a/orchestra/contrib/mailer/backends.py b/orchestra/contrib/mailer/backends.py index a520ee4a..dd66cafd 100644 --- a/orchestra/contrib/mailer/backends.py +++ b/orchestra/contrib/mailer/backends.py @@ -1,4 +1,5 @@ from django.conf import settings as djsettings +from django.core.mail import get_connection from django.core.mail.backends.base import BaseEmailBackend from orchestra.core.caches import get_request_cache @@ -27,6 +28,7 @@ class EmailBackend(BaseEmailBackend): is_bulk = True default_priority = Message.NORMAL if is_bulk else Message.CRITICAL num_sent = 0 + connection = None for message in email_messages: priority = message.extra_headers.get('X-Mail-Priority', default_priority) content = message.message().as_string() @@ -40,8 +42,12 @@ class EmailBackend(BaseEmailBackend): ) if priority == Message.CRITICAL: # send immidiately - send_message.apply_async(message) + if connection is None: + connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend') + send_message.apply_async(message, connection=connection) else: message.save() num_sent += 1 + if connection is not None: + connection.close() return num_sent diff --git a/orchestra/contrib/mailer/engine.py b/orchestra/contrib/mailer/engine.py index 14f65c49..62e45772 100644 --- a/orchestra/contrib/mailer/engine.py +++ b/orchestra/contrib/mailer/engine.py @@ -14,17 +14,22 @@ from .models import Message def send_message(message, connection=None, bulk=settings.MAILER_BULK_MESSAGES): - if connection is None: - # Reset connection with django - connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend') - connection.open() - error = None message.last_try = timezone.now() update_fields = ['last_try'] if message.state != message.QUEUED: message.retries += 1 update_fields.append('retries') message.save(update_fields=update_fields) + if connection is None: + connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend') + if connection.connection is None: + try: + connection.open() + except Exception as err: + message.defer() + message.log(error) + return + error = None try: connection.connection.sendmail(message.from_address, [message.to_address], smart_str(message.content)) except (SocketError, @@ -42,13 +47,13 @@ def send_message(message, connection=None, bulk=settings.MAILER_BULK_MESSAGES): def send_pending(bulk=settings.MAILER_BULK_MESSAGES): try: with LockFile('/dev/shm/mailer.send_pending.lock'): - connection = None + connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend') cur, total = 0, 0 for message in Message.objects.filter(state=Message.QUEUED).order_by('priority', 'last_try', 'created_at'): - if cur >= bulk and connection is not None: + if cur >= bulk: connection.close() cur = 0 - connection = send_message(message, connection, bulk) + send_message(message, connection, bulk) cur += 1 total += 1 now = timezone.now() @@ -57,14 +62,15 @@ def send_pending(bulk=settings.MAILER_BULK_MESSAGES): delta = timedelta(seconds=seconds) qs = qs | Q(retries=retries, last_try__lte=now-delta) for message in Message.objects.filter(state=Message.DEFERRED).filter(qs).order_by('priority', 'last_try'): - if cur >= bulk and connection is not None: + if cur >= bulk: connection.close() cur = 0 - connection = send_message(message, connection, bulk) + send_message(message, connection, bulk) cur += 1 total += 1 - if connection is not None: - connection.close() return total except OperationLocked: pass + finally: + if connection.connection is not None: + connection.close() diff --git a/orchestra/contrib/mailer/tasks.py b/orchestra/contrib/mailer/tasks.py index 90fa9076..c05d9fed 100644 --- a/orchestra/contrib/mailer/tasks.py +++ b/orchestra/contrib/mailer/tasks.py @@ -9,9 +9,9 @@ from . import engine, settings @task -def send_message(message): +def send_message(message, connection=None): message.save() - engine.send_message(message) + engine.send_message(message, connection=connection) @periodic_task(run_every=crontab(hour=7, minute=30))