Fixed mailer connection issues
This commit is contained in:
parent
598bb39f84
commit
d6f85f07df
|
@ -27,7 +27,8 @@
|
||||||
<span class="name">{{ seller.get_name }}</span>
|
<span class="name">{{ seller.get_name }}</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="contact">
|
<div class="contact">
|
||||||
<p>{{ seller.address }}<br>
|
<p>{{ seller.vat }}<br>
|
||||||
|
{{ seller.address }}<br>
|
||||||
{{ seller.zipcode }} - {% trans seller.city %}<br>
|
{{ seller.zipcode }} - {% trans seller.city %}<br>
|
||||||
{% trans seller.get_country_display %}<br>
|
{% trans seller.get_country_display %}<br>
|
||||||
</p>
|
</p>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.conf import settings as djsettings
|
from django.conf import settings as djsettings
|
||||||
|
from django.core.mail import get_connection
|
||||||
from django.core.mail.backends.base import BaseEmailBackend
|
from django.core.mail.backends.base import BaseEmailBackend
|
||||||
|
|
||||||
from orchestra.core.caches import get_request_cache
|
from orchestra.core.caches import get_request_cache
|
||||||
|
@ -27,6 +28,7 @@ class EmailBackend(BaseEmailBackend):
|
||||||
is_bulk = True
|
is_bulk = True
|
||||||
default_priority = Message.NORMAL if is_bulk else Message.CRITICAL
|
default_priority = Message.NORMAL if is_bulk else Message.CRITICAL
|
||||||
num_sent = 0
|
num_sent = 0
|
||||||
|
connection = None
|
||||||
for message in email_messages:
|
for message in email_messages:
|
||||||
priority = message.extra_headers.get('X-Mail-Priority', default_priority)
|
priority = message.extra_headers.get('X-Mail-Priority', default_priority)
|
||||||
content = message.message().as_string()
|
content = message.message().as_string()
|
||||||
|
@ -40,8 +42,12 @@ class EmailBackend(BaseEmailBackend):
|
||||||
)
|
)
|
||||||
if priority == Message.CRITICAL:
|
if priority == Message.CRITICAL:
|
||||||
# send immidiately
|
# 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:
|
else:
|
||||||
message.save()
|
message.save()
|
||||||
num_sent += 1
|
num_sent += 1
|
||||||
|
if connection is not None:
|
||||||
|
connection.close()
|
||||||
return num_sent
|
return num_sent
|
||||||
|
|
|
@ -14,17 +14,22 @@ from .models import Message
|
||||||
|
|
||||||
|
|
||||||
def send_message(message, connection=None, bulk=settings.MAILER_BULK_MESSAGES):
|
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()
|
message.last_try = timezone.now()
|
||||||
update_fields = ['last_try']
|
update_fields = ['last_try']
|
||||||
if message.state != message.QUEUED:
|
if message.state != message.QUEUED:
|
||||||
message.retries += 1
|
message.retries += 1
|
||||||
update_fields.append('retries')
|
update_fields.append('retries')
|
||||||
message.save(update_fields=update_fields)
|
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:
|
try:
|
||||||
connection.connection.sendmail(message.from_address, [message.to_address], smart_str(message.content))
|
connection.connection.sendmail(message.from_address, [message.to_address], smart_str(message.content))
|
||||||
except (SocketError,
|
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):
|
def send_pending(bulk=settings.MAILER_BULK_MESSAGES):
|
||||||
try:
|
try:
|
||||||
with LockFile('/dev/shm/mailer.send_pending.lock'):
|
with LockFile('/dev/shm/mailer.send_pending.lock'):
|
||||||
connection = None
|
connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend')
|
||||||
cur, total = 0, 0
|
cur, total = 0, 0
|
||||||
for message in Message.objects.filter(state=Message.QUEUED).order_by('priority', 'last_try', 'created_at'):
|
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()
|
connection.close()
|
||||||
cur = 0
|
cur = 0
|
||||||
connection = send_message(message, connection, bulk)
|
send_message(message, connection, bulk)
|
||||||
cur += 1
|
cur += 1
|
||||||
total += 1
|
total += 1
|
||||||
now = timezone.now()
|
now = timezone.now()
|
||||||
|
@ -57,14 +62,15 @@ def send_pending(bulk=settings.MAILER_BULK_MESSAGES):
|
||||||
delta = timedelta(seconds=seconds)
|
delta = timedelta(seconds=seconds)
|
||||||
qs = qs | Q(retries=retries, last_try__lte=now-delta)
|
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'):
|
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()
|
connection.close()
|
||||||
cur = 0
|
cur = 0
|
||||||
connection = send_message(message, connection, bulk)
|
send_message(message, connection, bulk)
|
||||||
cur += 1
|
cur += 1
|
||||||
total += 1
|
total += 1
|
||||||
if connection is not None:
|
|
||||||
connection.close()
|
|
||||||
return total
|
return total
|
||||||
except OperationLocked:
|
except OperationLocked:
|
||||||
pass
|
pass
|
||||||
|
finally:
|
||||||
|
if connection.connection is not None:
|
||||||
|
connection.close()
|
||||||
|
|
|
@ -9,9 +9,9 @@ from . import engine, settings
|
||||||
|
|
||||||
|
|
||||||
@task
|
@task
|
||||||
def send_message(message):
|
def send_message(message, connection=None):
|
||||||
message.save()
|
message.save()
|
||||||
engine.send_message(message)
|
engine.send_message(message, connection=connection)
|
||||||
|
|
||||||
|
|
||||||
@periodic_task(run_every=crontab(hour=7, minute=30))
|
@periodic_task(run_every=crontab(hour=7, minute=30))
|
||||||
|
|
Loading…
Reference in New Issue