Have client and server exchange a server generation number

This will allow the client to detect when the server has restarted.

(imported from commit 89e75916719d967beb2520be6263f79f897d9ec1)
This commit is contained in:
Zev Benjamin 2012-10-16 15:15:01 -04:00
parent 24c1a2d7f1
commit 103bf321b4
4 changed files with 20 additions and 3 deletions

View File

@ -50,6 +50,7 @@
{# Not escaped, because it's guaranteed by the model to be an integer. #} {# Not escaped, because it's guaranteed by the model to be an integer. #}
var initial_pointer = {{ user_profile.pointer }}; var initial_pointer = {{ user_profile.pointer }};
var server_generation = {{ server_generation }};
var email = "{{ user_profile.user.email|escapejs }}"; var email = "{{ user_profile.user.email|escapejs }}";
var have_initial_messages = {{ have_initial_messages|escapejs }}; var have_initial_messages = {{ have_initial_messages|escapejs }};

View File

@ -7,6 +7,7 @@ var globals =
// index.html // index.html
+ ' initial_pointer email stream_list people_list have_initial_messages' + ' initial_pointer email stream_list people_list have_initial_messages'
+ ' server_generation'
// compose.js // compose.js
+ ' show_compose hide_compose toggle_compose clear_compose_box compose_button' + ' show_compose hide_compose toggle_compose clear_compose_box compose_button'

View File

@ -57,9 +57,12 @@ var selected_message; // = get_message_row(selected_message_id)
var received = { var received = {
first: -1, first: -1,
last: -1, last: -1,
failures: 0 failures: 0,
server_generation: -1 /* to be filled in on document.ready */
}; };
$(function () { received.server_generation = server_generation; });
// The "message groups", i.e. blocks of messages collapsed by recipient. // The "message groups", i.e. blocks of messages collapsed by recipient.
// Each message table has a list of lists. // Each message table has a list of lists.
var message_groups = { var message_groups = {

View File

@ -28,6 +28,9 @@ import socket
import re import re
import hashlib import hashlib
import urllib import urllib
import time
SERVER_GENERATION = int(time.time())
def require_post(view_func): def require_post(view_func):
def _wrapped_view_func(request, *args, **kwargs): def _wrapped_view_func(request, *args, **kwargs):
@ -174,7 +177,8 @@ def home(request):
'have_initial_messages': 'have_initial_messages':
'true' if num_messages > 0 else 'false', 'true' if num_messages > 0 else 'false',
'show_debug': 'show_debug':
settings.DEBUG and ('show_debug' in request.GET) }, settings.DEBUG and ('show_debug' in request.GET),
'server_generation': SERVER_GENERATION},
context_instance=RequestContext(request)) context_instance=RequestContext(request))
@login_required @login_required
@ -203,12 +207,14 @@ def format_updates_response(messages, mit_sync_bot=False, apply_markdown=False,
return {'messages': [message.to_dict(apply_markdown) for message in messages], return {'messages': [message.to_dict(apply_markdown) for message in messages],
"result": "success", "result": "success",
"msg": "", "msg": "",
'where': where} 'where': where,
'server_generation': SERVER_GENERATION}
def return_messages_immediately(request, handler, user_profile, **kwargs): def return_messages_immediately(request, handler, user_profile, **kwargs):
first = request.POST.get("first") first = request.POST.get("first")
last = request.POST.get("last") last = request.POST.get("last")
failures = request.POST.get("failures") failures = request.POST.get("failures")
client_server_generation = request.POST.get("server_generation")
if first is None or last is None: if first is None or last is None:
# When an API user is first querying the server to subscribe, # When an API user is first querying the server to subscribe,
# there's no reason to reply immediately. # there's no reason to reply immediately.
@ -251,6 +257,12 @@ def return_messages_immediately(request, handler, user_profile, **kwargs):
handler.finish(format_updates_response([], where="bottom", **kwargs)) handler.finish(format_updates_response([], where="bottom", **kwargs))
return True return True
if client_server_generation is not None and int(client_server_generation) != SERVER_GENERATION:
# No messages, but still return immediately to inform the
# client that they should reload
handler.finish(format_updates_response([], where="bottom", **kwargs))
return True
return False return False
def get_updates_backend(request, user_profile, handler, **kwargs): def get_updates_backend(request, user_profile, handler, **kwargs):