Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
scheduled_email: Create ScheduledEmail objects in a transaction.
Alex Vandiverd4f6b938590c
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex 344034d5225..385625d070f 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -345,27 +345,28 @@ def send_future_email(# For logging the emailassert (to_user_ids is None) ^ (to_emails is None)- email = ScheduledEmail.objects.create(- type=EMAIL_TYPES[template_name],- scheduled_timestamp=timezone_now() + delay,- realm=realm,- data=orjson.dumps(email_fields).decode(),- )+ with transaction.atomic():+ email = ScheduledEmail.objects.create(+ type=EMAIL_TYPES[template_name],+ scheduled_timestamp=timezone_now() + delay,+ realm=realm,+ data=orjson.dumps(email_fields).decode(),+ )- # We store the recipients in the ScheduledEmail object itself,- # rather than the JSON data object, so that we can find and clear- # them using clear_scheduled_emails.- try:- if to_user_ids is not None:- email.users.add(*to_user_ids)- else:- assert to_emails is not None- assert len(to_emails) == 1- email.address = parseaddr(to_emails[0])[1]- email.save()- except Exception as e:- email.delete()- raise e+ # We store the recipients in the ScheduledEmail object itself,+ # rather than the JSON data object, so that we can find and clear+ # them using clear_scheduled_emails.+ try:+ if to_user_ids is not None:+ email.users.add(*to_user_ids)+ else:+ assert to_emails is not None+ assert len(to_emails) == 1+ email.address = parseaddr(to_emails[0])[1]+ email.save()+ except Exception as e:+ email.delete()+ raise edef send_email_to_admins(