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.

MissedMessageWorker: Handle deleted messages.

Abhijeet Prasad Bodas9fcb6e51ce14
diff --git a/zerver/tests/test_queue_worker.py b/zerver/tests/test_queue_worker.py
index 2015ed6b896..e6778743a31 100644
--- a/zerver/tests/test_queue_worker.py
+++ b/zerver/tests/test_queue_worker.py
@@ -10,6 +10,7 @@ from unittest.mock import MagicMock, patch
import orjson
from django.conf import settings
+from django.db.utils import IntegrityError
from django.test import override_settings
from zerver.lib.email_mirror import RateLimitedRealmMirror
@@ -287,6 +288,30 @@ class WorkerTest(ZulipTestCase):
# All batches got processed. Verify that the timer isn't running.
self.assertEqual(mmw.timer_event, None)
+ # Hacky test coming up! We want to test the try-except block in the consumer which handles
+ # IntegrityErrors raised when the message was deleted before it processed the notification
+ # event.
+ # However, Postgres defers checking ForeignKey constraints to when the current transaction
+ # commits. This poses some difficulties in testing because of Django running tests inside a
+ # transaction which never commits. See https://code.djangoproject.com/ticket/22431 for more
+ # details, but the summary is that IntegrityErrors due to database constraints are raised at
+ # the end of the test, not inside the `try` block. So, we have the code inside the `try` block
+ # raise `IntegrityError` by mocking.
+ def raise_error(**kwargs: Any) -> None:
+ raise IntegrityError
+
+ fake_client.enqueue("missedmessage_emails", hamlet_event1)
+
+ with patch(
+ "zerver.models.ScheduledMessageNotificationEmail.objects.create",
+ side_effect=raise_error,
+ ), self.assertLogs(level="DEBUG") as debug_logs:
+ mmw.start()
+ self.assertIn(
+ "DEBUG:root:ScheduledMessageNotificationEmail row could not be created. The message may have been deleted. Skipping event.",
+ debug_logs.output,
+ )
+
# Check that the frequency of calling maybe_send_batched_emails is correct (5 seconds)
self.assertEqual(tm.call_args[0][0], 5)
diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.py
index f12a79bbd83..e9b3d4f9c3d 100644
--- a/zerver/worker/queue_processors.py
+++ b/zerver/worker/queue_processors.py
@@ -39,6 +39,7 @@ from django.conf import settings
from django.core.mail.backends.smtp import EmailBackend
from django.db import connection, transaction
from django.db.models import F
+from django.db.utils import IntegrityError
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
from django.utils.translation import override as override_language
@@ -595,17 +596,20 @@ class MissedMessageWorker(QueueProcessingWorker):
except IndexError:
scheduled_timestamp = timezone_now() + batch_duration
- entry = ScheduledMessageNotificationEmail(
- user_profile_id=user_profile_id,
- message_id=event["message_id"],
- trigger=event["trigger"],
- scheduled_timestamp=scheduled_timestamp,
- )
- if "mentioned_user_group_id" in event:
- entry.mentioned_user_group_id = event["mentioned_user_group_id"]
- entry.save()
+ try:
+ ScheduledMessageNotificationEmail.objects.create(
+ user_profile_id=user_profile_id,
+ message_id=event["message_id"],
+ trigger=event["trigger"],
+ scheduled_timestamp=scheduled_timestamp,
+ mentioned_user_group_id=event.get("mentioned_user_group_id"),
+ )
- self.ensure_timer()
+ self.ensure_timer()
+ except IntegrityError:
+ logging.debug(
+ "ScheduledMessageNotificationEmail row could not be created. The message may have been deleted. Skipping event."
+ )
def ensure_timer(self) -> None:
# The caller is responsible for ensuring self.lock is held when it calls this.