Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

send_email: Delete ScheduledEmail objects with no recipients.

Alex Vandiver77c146b8b028
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py
index 5ac89ddad16..5fa63d9a98d 100644
--- a/zerver/lib/send_email.py
+++ b/zerver/lib/send_email.py
@@ -477,10 +477,14 @@ def deliver_scheduled_emails(email: ScheduledEmail) -> None:
data = orjson.loads(email.data)
user_ids = list(email.users.values_list("id", flat=True))
if not user_ids and not email.address:
- # This state doesn't make sense, so something must be mutating,
- # or in the process of deleting, the object. We assume it will bring
- # things to a correct state, and we just do nothing except logging this event.
- logger.error("ScheduledEmail id %s has empty users and address attributes.", email.id)
+ # This state doesn't make sense, so something must have mutated the object
+ logger.warning(
+ "ScheduledEmail %s at %s had empty users and address attributes: %r",
+ email.id,
+ email.scheduled_timestamp,
+ data,
+ )
+ email.delete()
return
if user_ids:
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index d75f714f09b..ed54028152f 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -1698,16 +1698,21 @@ class ActivateTest(ZulipTestCase):
assert email is not None
email.users.remove(*to_user_ids)
+ email_id = email.id
+ scheduled_at = email.scheduled_timestamp
with self.assertLogs("zulip.send_email", level="INFO") as info_log:
deliver_scheduled_emails(email)
from django.core.mail import outbox
self.assert_length(outbox, 0)
- self.assertEqual(ScheduledEmail.objects.count(), 1)
+ self.assertEqual(ScheduledEmail.objects.count(), 0)
self.assertEqual(
info_log.output,
[
- f"ERROR:zulip.send_email:ScheduledEmail id {email.id} has empty users and address attributes."
+ f"WARNING:zulip.send_email:ScheduledEmail {email_id} at {scheduled_at} "
+ "had empty users and address attributes: "
+ "{'template_prefix': 'zerver/emails/followup_day1', 'from_name': None, "
+ "'from_address': None, 'language': None, 'context': {}}"
],
)