schedule_messages: Update do_schedule_messages to send event on commit.

Earlier, we were using 'send_event' in 'do_schedule_messages' which
can lead to a situation, if any db operation is added after the
'send_event' in future, where we enqueue events but the action
function fails at a later stage.

Events should not be sent until we know we're not rolling back.

Fixes part of #30489.
This commit is contained in:
Prakhar Pratyush 2024-07-31 19:59:55 +05:30 committed by Tim Abbott
parent 7302d533fe
commit 04347bb315
1 changed files with 10 additions and 9 deletions

View File

@ -24,7 +24,7 @@ from zerver.lib.scheduled_messages import access_scheduled_message
from zerver.lib.string_validation import check_stream_topic
from zerver.models import Client, Realm, ScheduledMessage, Subscription, UserProfile
from zerver.models.users import get_system_bot
from zerver.tornado.django_api import send_event
from zerver.tornado.django_api import send_event, send_event_on_commit
SCHEDULED_MESSAGE_LATE_CUTOFF_MINUTES = 10
@ -104,14 +104,15 @@ def do_schedule_messages(
scheduled_message.has_attachment = True
scheduled_message.save(update_fields=["has_attachment"])
event = {
"type": "scheduled_messages",
"op": "add",
"scheduled_messages": [
scheduled_message.to_dict() for scheduled_message, ignored in scheduled_messages
],
}
send_event(sender.realm, event, [sender.id])
event = {
"type": "scheduled_messages",
"op": "add",
"scheduled_messages": [
scheduled_message.to_dict() for scheduled_message, ignored in scheduled_messages
],
}
send_event_on_commit(sender.realm, event, [sender.id])
return [scheduled_message.id for scheduled_message, ignored in scheduled_messages]