api: Handle restart events in apply_events.

Event of type restart could not be handled properly, because of
its special behavior. For handling this event in most natural way
we recursively call `do_events_register` when restart event is
recieved, based on custom error created for this event.

Testing: Second call to get_user_events due to recursive calling
of do_event_register, is expected to not contain the restart event.
So new test added in test_event_system.py are based on above behavior
of get_user_events.

Fixes: #15541.
This commit is contained in:
m-e-l-u-h-a-n
2021-04-29 16:24:30 +05:30
committed by Tim Abbott
parent 2a4452e722
commit d2c18e28a4
3 changed files with 134 additions and 12 deletions

View File

@@ -69,6 +69,12 @@ from zerver.tornado.django_api import get_user_events, request_event_queue
from zproject.backends import email_auth_enabled, password_auth_enabled
class RestartEventException(Exception):
"""
Special error for handling restart events in apply_events.
"""
def add_realm_logo_fields(state: Dict[str, Any], realm: Realm) -> None:
state["realm_logo_url"] = get_realm_logo_url(realm, night=False)
state["realm_logo_source"] = get_realm_logo_source(realm, night=False)
@@ -492,6 +498,8 @@ def apply_events(
include_subscribers: bool,
) -> None:
for event in events:
if event["type"] == "restart":
raise RestartEventException()
if fetch_event_types is not None and event["type"] not in fetch_event_types:
# TODO: continuing here is not, most precisely, correct.
# In theory, an event of one type, e.g. `realm_user`,
@@ -1141,15 +1149,39 @@ def do_events_register(
# Apply events that came in while we were fetching initial data
events = get_user_events(user_profile, queue_id, -1)
apply_events(
user_profile,
state=ret,
events=events,
fetch_event_types=fetch_event_types,
client_gravatar=client_gravatar,
slim_presence=slim_presence,
include_subscribers=include_subscribers,
)
try:
apply_events(
user_profile,
state=ret,
events=events,
fetch_event_types=fetch_event_types,
client_gravatar=client_gravatar,
slim_presence=slim_presence,
include_subscribers=include_subscribers,
)
except RestartEventException:
# This represents a rare race condition, where Tornado
# restarted (and sent `restart` events) while we were waiting
# for fetch_initial_state_data to return. To avoid the client
# needing to reload shortly after loading, we recursively call
# do_events_register here.
ret = do_events_register(
user_profile,
user_client,
apply_markdown,
client_gravatar,
slim_presence,
event_types,
queue_lifespan_secs,
all_public_streams,
include_subscribers,
include_streams,
client_capabilities,
narrow,
fetch_event_types,
)
return ret
post_process_state(user_profile, ret, notification_settings_null)