Skip to content

Commit

Permalink
Merge pull request #1327 from cisagov/nmb/email-sent-incremental
Browse files Browse the repository at this point in the history
Set transition domain's email_sent flag as the emails are sent
  • Loading branch information
neilmb authored Nov 13, 2023
2 parents ce23849 + f303082 commit bcbf555
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/registrar/management/commands/send_domain_invitations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging
import copy
import time

from django.core.management import BaseCommand
from registrar.models import TransitionDomain
Expand Down Expand Up @@ -65,8 +66,6 @@ def handle(self, **options):
self.send_emails()
logger.info("done sending emails")

self.update_domains_as_sent()

logger.info("done sending emails and updating transition_domains")
else:
logger.info("not sending emails")
Expand Down Expand Up @@ -114,6 +113,9 @@ def send_emails(self):
if len(self.emails_to_send) > 0:
for email_data in self.emails_to_send:
self.send_email(email_data)
# wait 1/10 second until sending the next email to keep us
# safely under a rate of 10 emails per second
time.sleep(0.1)
else:
logger.info("no emails to send")

Expand Down Expand Up @@ -143,11 +145,13 @@ def send_email(self, email_data):
# to True
for domain in email_data["domains"]:
self.domains_with_errors.append(domain)

def update_domains_as_sent(self):
"""set email_sent to True in all transition_domains which have
been processed successfully"""
for transition_domain in self.transition_domains:
if transition_domain.domain_name not in self.domains_with_errors:
transition_domain.email_sent = True
transition_domain.save()
else:
# email was sent no exception, mark all these transition domains
# as email_sent.
this_email = email_data["email"]
for domain_name in email_data["domains"]:
# self.transition_domains is a queryset so we can sub-select
# from it and use the objects to mark them as sent
this_transition_domain = self.transition_domains.get(username=this_email, domain_name=domain_name)
this_transition_domain.email_sent = True
this_transition_domain.save()

0 comments on commit bcbf555

Please sign in to comment.