Skip to content

Commit

Permalink
break after batch
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpoi committed Dec 19, 2024
1 parent 529bb66 commit 0fc7547
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions demoapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
'CELERY_ENABLED': True,
'MAX_RETRIES': 0,
'BATCH_SIZE': 100,
'BREAK_AFTER_BATCH': True,
'BATCH_DELIVERY_TIMEOUT': 30,
'MESSAGE_ID_ENABLED': True,
'DEFAULT_PRIORITY': 'medium',
Expand Down
5 changes: 4 additions & 1 deletion sendmail/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from sendmail.models.log import Log
from sendmail.settings import (get_available_backends, get_batch_size, get_default_language, get_email_address_model,
get_log_level, get_max_retries, get_message_id_enabled, get_message_id_fqdn,
get_retry_timedelta, get_sending_order)
get_retry_timedelta, get_sending_order, get_break_after_batch)
from sendmail.signals import email_queued
from sendmail.utils import (create_attachments, get_emailmerge, get_language_from_code, get_or_create_recipient,
get_recipients_objects, parse_emails, parse_priority, set_recipients,
Expand Down Expand Up @@ -333,6 +333,9 @@ def send_signal_after_commit():
for batch in split_into_batches(emails):
email_queued.send(sender=EmailModel, emails=batch)

if get_break_after_batch():
break

transaction.on_commit(send_signal_after_commit)

return emails
Expand Down
3 changes: 3 additions & 0 deletions sendmail/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ def get_email_address_setting():
# def get_override_recipients():
# return get_config().get('OVERRIDE_RECIPIENTS', None)

def get_break_after_batch():
return get_config().get('BREAK_AFTER_BATCH', False)


def get_max_retries():
return get_config().get('MAX_RETRIES', 0)
Expand Down
5 changes: 4 additions & 1 deletion sendmail/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.utils.timezone import now

from sendmail.mail import _send_bulk, get_queued
from sendmail.settings import get_celery_enabled
from sendmail.settings import get_celery_enabled, get_break_after_batch
from sendmail.utils import cleanup_expired_mails

try:
Expand Down Expand Up @@ -44,6 +44,9 @@ def send_queued_mail(*args, **kwargs):
except Exception as e:
raise e

if get_break_after_batch():
break

db_connection.close()


Expand Down

0 comments on commit 0fc7547

Please sign in to comment.