Skip to content

Commit

Permalink
Added -p or --processes option to send_queued_mail management command.
Browse files Browse the repository at this point in the history
  • Loading branch information
selwin committed Jul 12, 2013
1 parent 62f78aa commit 20ceb04
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
8 changes: 4 additions & 4 deletions post_office/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,20 @@ def get_queued():
.order_by('-priority')


def send_queued(num_processes=1):
def send_queued(processes=1):
"""
Sends out all queued mails that has scheduled_time less than now or None
"""
queued_emails = get_queued()

if queued_emails:
if num_processes == 1:
if processes == 1:
total_sent, total_failed = _send_bulk(queued_emails)
else:
# Group emails into X sublists
# taken from http://www.garyrobinson.net/2008/04/splitting-a-pyt.html
email_lists = [queued_emails[i::num_processes] for i in range(num_processes)]
pool = Pool(num_processes)
email_lists = [queued_emails[i::processes] for i in range(processes)]
pool = Pool(processes)
results = pool.map(_send_bulk, email_lists)
total_sent = sum([result[0] for result in results])
total_failed = sum([result[1] for result in results])
Expand Down
10 changes: 8 additions & 2 deletions post_office/management/commands/send_queued_mail.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import tempfile
from optparse import make_option

from django.core.management.base import BaseCommand

from ...lockfile import FileLock
from ..utils import send_queued
from ...mail import send_queued


class Command(BaseCommand):

option_list = BaseCommand.option_list + (
make_option('-p', '--processes', type='int',
help='Number of processes used to send emails', default=1),
)

def handle(self, *args, **options):
with FileLock(tempfile.gettempdir() + "/post_office", timeout=1):
send_queued()
send_queued(options['processes'])
11 changes: 10 additions & 1 deletion post_office/tests/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.core.management import call_command
from django.test import TestCase

from ..models import Email
from ..models import Email, STATUS

try:
from django.utils.timezone import now
Expand Down Expand Up @@ -31,3 +31,12 @@ def test_cleanup_mail(self):
email.save()
call_command('cleanup_mail', days=30)
self.assertEqual(Email.objects.count(), 0)

def test_send_queued_mail(self):
"""
Quick check that ``send_queued_mail`` doesn't error out.
"""
email = Email.objects.create(from_email='from@example.com',
to='to@example.com', status=STATUS.queued)
call_command('send_queued_mail', processes=1)
self.assertEqual(Email.objects.filter(status=STATUS.sent).count(), 1)
2 changes: 1 addition & 1 deletion post_office/tests/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_send_queued_mail_multi_processes(self):
Email.objects.create(**kwargs)
Email.objects.create(**kwargs)
Email.objects.create(**kwargs)
total_sent, total_failed = send_queued(num_processes=2)
total_sent, total_failed = send_queued(processes=2)
self.assertEqual(total_sent, 3)

@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
Expand Down

0 comments on commit 20ceb04

Please sign in to comment.