api: Make call_on_each_message restart if 'last' is too old.

This should fix the symptoms of the problem we've been having where a
few API clients using the MIT Zephyr mirroring system sometimes seem
to end up with a too-old value of last.

(imported from commit 9f2426fa6a7e8365e8d3443bfd2cce3238cc9510)
This commit is contained in:
Tim Abbott 2013-02-12 13:59:28 -05:00
parent e7f46e0823
commit eb98c14af6

View file

@ -195,15 +195,28 @@ class Client(object):
while True: while True:
if max_message_id is not None: if max_message_id is not None:
options["last"] = str(max_message_id) options["last"] = str(max_message_id)
elif options.get('last') is not None:
options.pop('last')
res = self.get_messages(options) res = self.get_messages(options)
if 'error' in res.get('result'): if 'error' in res.get('result'):
if self.verbose: if res["result"] == "http-error":
if res["result"] == "http-error": if self.verbose:
print "HTTP error fetching messages -- probably a server restart" print "HTTP error fetching messages -- probably a server restart"
elif res["result"] == "connection-error": elif res["result"] == "connection-error":
if self.verbose:
print "Connection error fetching messages -- probably server is temporarily down?" print "Connection error fetching messages -- probably server is temporarily down?"
else: else:
if self.verbose:
print "Server returned error:\n%s" % res["msg"] print "Server returned error:\n%s" % res["msg"]
if res["msg"].startswith("last value of") and \
"too old! Minimum valid is" in res["msg"]:
# We may have missed some messages while the
# network was down or something, but there's
# not really anything we can do about it other
# than resuming getting new ones.
#
# Reset max_message_id to just subscribe to new messages
max_message_id = None
# TODO: Make this back off once it's more reliable # TODO: Make this back off once it's more reliable
time.sleep(1) time.sleep(1)
continue continue