Skip to content

Commit 35d0968

Browse files
authored
Merge pull request #138 from leukeleu/add-runmailer
Add runmailer
2 parents af5252c + 8e01a7b commit 35d0968

File tree

6 files changed

+41
-3
lines changed

6 files changed

+41
-3
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ ADDITIONAL CONTRIBUTORS include:
1313
* Renato Alves
1414
* Paul Brown
1515
* Sebastian Pipping
16+
* Jaap Roes

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Change log
44
2.1.1 - Unreleased
55
------------------
66

7+
* Add ``runmailer`` management command. This command starts a loop that
8+
frequently checks the database for new emails. The wait time between
9+
checks can be controlled using the ``MAILER_EMPTY_QUEUE_SLEEP`` setting.
10+
711
2.1 - 2020-12-05
812
----------------
913

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ To remove successful log entries older than a week, add this to a cron job file
7777

7878
Use the `-r failure` option to remove only failed log entries instead, or `-r all` to remove them all.
7979

80+
Note that the ``send_mail`` cronjob can only run at a maximum frequency of once each minute. If a maximum
81+
delay of 60 seconds between creating an email and sending it is too much, an alternative is available.
82+
83+
Using ``./manage.py runmailer`` a long running process is started that will check the database
84+
for new emails each ``MAILER_EMPTY_QUEUE_SLEEP`` (default: 30 seconds).
85+
8086
Documentation and support
8187
-------------------------
8288

docs/usage.rst

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,18 @@ or all managers as defined in the ``MANAGERS`` setting by calling::
6464
Clear queue with command extensions
6565
===================================
6666

67-
With mailer in your INSTALLED_APPS, there will be three new manage.py commands
68-
you can run:
67+
With mailer in your ``INSTALLED_APPS``, there will be four new
68+
``manage.py`` commands you can run:
6969

7070
* ``send_mail`` will clear the current message queue. If there are any
7171
failures, they will be marked deferred and will not be attempted again by
7272
``send_mail``.
7373

74+
* ``runmailer`` similar to ``send_mail``, but will keep running and checking the
75+
database for new messages each ``MAILER_EMPTY_QUEUE_SLEEP`` (default: 30) seconds.
76+
Can be used *instead* of ``send_mail`` to circumvent the maximum frequency
77+
of once per minutes inherent to cron.
78+
7479
* ``retry_deferred`` will move any deferred mail back into the normal queue
7580
(so it will be attempted again on the next ``send_mail``).
7681

@@ -102,7 +107,12 @@ this command from the virtualenv. The same, naturally, applies also if you're
102107
executing it with cron. The `Pinax documentation`_ explains that in more
103108
details.
104109

110+
If you intend to use ``manage.py runmailer`` instead of ``send_mail`` it's
111+
up to you to keep this command running in the background. This can be achieved
112+
using `supervisord`_ or similar software.
113+
105114
.. _pinax documentation: http://pinaxproject.com/docs/dev/deployment.html#sending-mail-and-notices
115+
.. _supervisord: http://supervisord.org/
106116

107117
Controlling the delivery process
108118
================================

src/mailer/engine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def send_loop():
267267
"""
268268

269269
while True:
270-
while not Message.objects.all():
270+
while not Message.objects.all().exists():
271271
logger.debug("sleeping for %s seconds before checking queue again" % EMPTY_QUEUE_SLEEP)
272272
time.sleep(EMPTY_QUEUE_SLEEP)
273273
send_all()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sys
2+
from datetime import datetime
3+
4+
from django.core.management import BaseCommand
5+
6+
from mailer.engine import send_loop
7+
8+
9+
class Command(BaseCommand):
10+
"""Start the django-mailer send loop"""
11+
12+
def handle(self, *args, **options):
13+
self.stdout.write(datetime.now().strftime('%B %d, %Y - %X'))
14+
self.stdout.write('Starting django-mailer send loop.')
15+
quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-C'
16+
self.stdout.write('Quit the loop with %s.' % quit_command)
17+
send_loop()

0 commit comments

Comments
 (0)