Fixed PHP timeout option merging bug

This commit is contained in:
Marc Aymerich 2015-06-05 12:39:58 +00:00
parent cf3dd5f373
commit 95edd9e31c
2 changed files with 28 additions and 19 deletions

View File

@ -7,18 +7,19 @@ from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from orchestra.utils.sys import LockFile from orchestra.utils.sys import LockFile, OperationLocked
from . import settings from . import settings
from .models import Message from .models import Message
def send_message(message, num=0, connection=None, bulk=100): def send_message(message, num=0, connection=None, bulk=settings.MAILER_BULK_MESSAGES):
if num >= bulk and connection is not None: if num >= bulk and connection is not None:
connection.close() connection.close()
connection = None connection = None
if connection is None: if connection is None:
# Reset connection with django # Reset connection with django
num = 0
connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend') connection = get_connection(backend='django.core.mail.backends.smtp.EmailBackend')
connection.open() connection.open()
error = None error = None
@ -34,19 +35,22 @@ def send_message(message, num=0, connection=None, bulk=100):
message.log(error) message.log(error)
def send_pending(bulk=100): def send_pending(bulk=settings.MAILER_BULK_MESSAGES):
with LockFile('/dev/shm/mailer.send_pending.lock'): try:
connection = None with LockFile('/dev/shm/mailer.send_pending.lock'):
num = 0 connection = None
for message in Message.objects.filter(state=Message.QUEUED).order_by('priority'): num = 0
send_message(message, num, connection, bulk) for message in Message.objects.filter(state=Message.QUEUED).order_by('priority'):
num += 1 send_message(message, num, connection, bulk)
now = timezone.now() num += 1
qs = Q() now = timezone.now()
for retries, seconds in enumerate(settings.MAILER_DEFERE_SECONDS): qs = Q()
delta = timedelta(seconds=seconds) for retries, seconds in enumerate(settings.MAILER_DEFERE_SECONDS):
qs = qs | Q(retries=retries, last_retry__lte=now-delta) delta = timedelta(seconds=seconds)
for message in Message.objects.filter(state=Message.DEFERRED).filter(qs).order_by('priority'): qs = qs | Q(retries=retries, last_retry__lte=now-delta)
send_message(message, num, connection, bulk) for message in Message.objects.filter(state=Message.DEFERRED).filter(qs).order_by('priority'):
if connection is not None: send_message(message, num, connection, bulk)
connection.close() if connection is not None:
connection.close()
except OperationLocked:
pass

View File

@ -17,3 +17,8 @@ MAILER_NON_QUEUED_PER_REQUEST_THRESHOLD = Setting('MAILER_NON_QUEUED_PER_REQUEST
2, 2,
help_text=_("Number of emails that will be sent immediately before starting to queue them."), help_text=_("Number of emails that will be sent immediately before starting to queue them."),
) )
MAILER_BULK_MESSAGES = Setting('MAILER_BULK_MESSAGES',
500,
)