From f7956bc5f46589bff700077c16f9d14088274478 Mon Sep 17 00:00:00 2001 From: jar3b Date: Wed, 5 Dec 2018 20:11:07 +0300 Subject: [PATCH] Remove connection creation from backend --- queued_mailer/__init__.py | 2 +- queued_mailer/backend.py | 6 ++---- queued_mailer/tasks.py | 7 +++---- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/queued_mailer/__init__.py b/queued_mailer/__init__.py index 576f56f..57d631c 100644 --- a/queued_mailer/__init__.py +++ b/queued_mailer/__init__.py @@ -1 +1 @@ -# coding: utf-8 \ No newline at end of file +# coding: utf-8 diff --git a/queued_mailer/backend.py b/queued_mailer/backend.py index 0ea0d8f..3fdfa5c 100644 --- a/queued_mailer/backend.py +++ b/queued_mailer/backend.py @@ -4,7 +4,6 @@ from django.utils.encoding import force_text from .logger import logger from .tasks import send_message -from .utils import get_email_connection def _get_message_recipients(email_message): @@ -20,13 +19,12 @@ class EmailBackend(BaseEmailBackend): def send_messages(self, email_messages): num_sent = 0 - connection = get_email_connection() for email in email_messages: try: - send_message.apply_async([email, connection]) + send_message.apply_async([email, ]) num_sent += 1 except Exception as e: - logger.error("cannot send message %s: %r" % (_get_message_recipients(email), e)) + logger.error("cannot send message to '%s': %r" % (_get_message_recipients(email), e)) return num_sent diff --git a/queued_mailer/tasks.py b/queued_mailer/tasks.py index e3a2e49..738b11c 100644 --- a/queued_mailer/tasks.py +++ b/queued_mailer/tasks.py @@ -16,7 +16,7 @@ logger = get_task_logger(__name__) @celery.task(bind=True, queue=TASK_QUEUE_NAME, acks_late=True, default_retry_delay=20, autoretry_for=(EmailTransportException,), retry_kwargs={'max_retries': 5}) -def send_message(self, email, connection=None): +def send_message(self, email): logger.debug('task started') start_time = time.time() @@ -25,8 +25,7 @@ def send_message(self, email, connection=None): raise Exception('Invalid message class, only django.core.mail.EmailMessage is supported') try: - if connection is None: - connection = get_email_connection() + connection = get_email_connection() email.connection = connection email.send() @@ -41,4 +40,4 @@ def send_message(self, email, connection=None): raise elapsed_time = time.time() - start_time - logger.warning("message sent, elasped time %s" % elapsed_time) + logger.warning("message sent, elapsed time %s" % elapsed_time)