From 86e0e1d421c6d1f249fbfa8e45cf47b6566eb208 Mon Sep 17 00:00:00 2001 From: Margus Laak Date: Wed, 12 Feb 2020 17:48:41 +0200 Subject: [PATCH] Add -x, --exit-after-batch arguments to send_queued_mail command --- README.rst | 5 +++++ post_office/management/commands/send_queued_mail.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/README.rst b/README.rst index 4a9fc897..ba66fa39 100644 --- a/README.rst +++ b/README.rst @@ -457,6 +457,11 @@ Management Commands | ``--lockfile`` or ``-L`` | Full path to file used as lock file. Defaults to | | | ``/tmp/post_office.lock`` | +---------------------------+--------------------------------------------------+ +| ``--exit-after-batch`` or | Exit the command after sending one batch of | +| ``-x`` | emails. By combining BATCH_SIZE and crontab, | +| | you can send emails through servers with small | +| | throttling values. | ++---------------------------+--------------------------------------------------+ * ``cleanup_mail`` - delete all emails created before an X number of days diff --git a/post_office/management/commands/send_queued_mail.py b/post_office/management/commands/send_queued_mail.py index ab739e9d..e5e314c5 100644 --- a/post_office/management/commands/send_queued_mail.py +++ b/post_office/management/commands/send_queued_mail.py @@ -34,6 +34,11 @@ def add_arguments(self, parser): type=int, help='"0" to log nothing, "1" to only log errors', ) + parser.add_argument( + '-x', '--exit-after-batch', + action='store_true', + help='Exit script after sending one batch of emails', + ) def handle(self, *args, **options): logger.info('Acquiring lock for sending queued emails at %s.lock' % @@ -53,6 +58,9 @@ def handle(self, *args, **options): # Close DB connection to avoid multiprocessing errors connection.close() + if options.get('exit_after_batch'): + break + if not Email.objects.filter(status=STATUS.queued) \ .filter(Q(scheduled_time__lte=now()) | Q(scheduled_time=None)).exists(): break