Merge pull request #1327 from cisagov/nmb/email-sent-incremental

Set transition domain's email_sent flag as the emails are sent
This commit is contained in:
Neil MartinsenBurrell 2023-11-13 17:01:24 -06:00 committed by GitHub
commit bcbf5557ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
import logging
import copy
import time
from django.core.management import BaseCommand
from registrar.models import TransitionDomain
@ -65,8 +66,6 @@ class Command(BaseCommand):
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")
@ -114,6 +113,9 @@ class Command(BaseCommand):
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")
@ -143,11 +145,13 @@ class Command(BaseCommand):
# 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()