Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -x, --exit-after-batch arguments to send_queued_mail command #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions post_office/management/commands/send_queued_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this feature makes sense. Can you slightly change this to --max-batches? --max-batches=2 means it will exit after two batches of emails are sent.

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' %
Expand All @@ -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
Expand Down