zulip_update: Catch and carry on if a realm fails.

It is better to log the exception and _try_ all realms, than to fail
early due to a bad realm and fail to notify the rest.
This commit is contained in:
Alex Vandiver 2024-05-24 15:19:24 +00:00 committed by Tim Abbott
parent 5c2fd1de5a
commit 174ae2b409
2 changed files with 87 additions and 86 deletions

View File

@ -40,7 +40,7 @@ from zerver.lib.user_counts import realm_user_count_by_role
from zerver.lib.user_groups import create_system_user_groups_for_realm
from zerver.lib.user_message import UserMessageLite, bulk_insert_ums
from zerver.lib.utils import generate_api_key, process_list_in_batches
from zerver.lib.zulip_update_announcements import send_zulip_update_announcements
from zerver.lib.zulip_update_announcements import send_zulip_update_announcements_to_realm
from zerver.models import (
AlertWord,
Attachment,
@ -1559,7 +1559,9 @@ def do_import_realm(import_dir: Path, subdomain: str, processes: int = 1) -> Rea
realm=realm, event_type=RealmAuditLog.REALM_EXPORTED, acting_user=None
).exists()
if not is_realm_imported_from_other_zulip_server:
send_zulip_update_announcements(skip_delay=False, realm_imported_from_other_product=realm)
send_zulip_update_announcements_to_realm(
realm, skip_delay=False, realm_imported_from_other_product=True
)
return realm

View File

@ -1,3 +1,4 @@
import logging
from dataclasses import dataclass
from datetime import timedelta
from typing import List, Optional
@ -220,21 +221,21 @@ def send_messages_and_update_level(
realm.save(update_fields=["zulip_update_announcements_level"])
def send_zulip_update_announcements(
skip_delay: bool, realm_imported_from_other_product: Optional[Realm] = None
def send_zulip_update_announcements(skip_delay: bool) -> None:
latest_zulip_update_announcements_level = get_latest_zulip_update_announcements_level()
for realm in get_realms_behind_zulip_update_announcements_level(
level=latest_zulip_update_announcements_level
):
try:
send_zulip_update_announcements_to_realm(realm, skip_delay)
except Exception as e: # nocoverage
logging.exception(e)
def send_zulip_update_announcements_to_realm(
realm: Realm, skip_delay: bool, realm_imported_from_other_product: bool = False
) -> None:
latest_zulip_update_announcements_level = get_latest_zulip_update_announcements_level()
if realm_imported_from_other_product:
realms = [realm_imported_from_other_product]
else:
realms = list(
get_realms_behind_zulip_update_announcements_level(
level=latest_zulip_update_announcements_level
)
)
for realm in realms:
# Refresh the realm from the database and check its
# properties, to protect against racing with another copy of
# ourself.
@ -275,7 +276,7 @@ def send_zulip_update_announcements(
and is_group_direct_message_sent_to_admins_within_days(realm, days=1)
and not skip_delay
):
continue
return
# Send an introductory message just before the first update message.
with override_language(realm.default_language):
@ -283,9 +284,7 @@ def send_zulip_update_announcements(
stream = realm.zulip_update_announcements_stream
assert stream.recipient_id is not None
topic_has_messages = messages_for_topic(
realm.id, stream.recipient_id, topic_name
).exists()
topic_has_messages = messages_for_topic(realm.id, stream.recipient_id, topic_name).exists()
if not topic_has_messages:
content_of_introductory_message = (