From 0489154cd791337634ee712f7a959605bbc8803a Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Wed, 7 Nov 2012 11:44:45 -0500 Subject: [PATCH] zephyr_mirror: don't crash when the zephyr server returns SERVNAK errors. Zephyr has this great property where a small fraction of the time, the Zephyr server rejects your attempt to subscribe to something for no particular reason, returning a "SERVNAK" error. (imported from commit 6d5ed033d46d77a5b02539a816453724740f8fb0) --- bots/zephyr_mirror.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/bots/zephyr_mirror.py b/bots/zephyr_mirror.py index 77d2409..3a8ff9a 100755 --- a/bots/zephyr_mirror.py +++ b/bots/zephyr_mirror.py @@ -137,7 +137,17 @@ current_zephyr_subs = set() def ensure_subscribed(sub): if sub in current_zephyr_subs: return - subs.add((sub, '*', '*')) + try: + subs.add((sub, '*', '*')) + except IOError: + # Since we haven't added the subscription to + # current_zephyr_subs yet, we can just return (so that we'll + # continue processing normal messages) and we'll end up + # retrying the next time the bot checks its subscriptions are + # up to date. + traceback.print_exc() + print "Error subscribing to stream %s; will retry later." % (sub,) + return current_zephyr_subs.add(sub) def update_subscriptions_from_humbug(): @@ -282,13 +292,27 @@ def decode_unicode_byte_strings(zeph): zeph[field] = decoded return zeph +def zephyr_subscribe_autoretry(sub): + while True: + try: + subs.add(sub) + return + except IOError: + # Probably a SERVNAK from the zephyr server, but print the + # traceback just in case it's something else + traceback.print_exc() + print "Error subscribing to personals; retrying." + time.sleep(1) + def zephyr_to_humbug(options): if options.forward_class_messages: update_subscriptions_from_humbug() if options.forward_personals: - subs.add(("message", "*", "%me%")) + # Subscribe to personals; we really can't operate without + # those subscriptions, so just retry until it works. + zephyr_subscribe_autoretry(("message", "*", "%me%")) if subscribed_to_mail_messages(): - subs.add(("mail", "inbox", "%me%")) + zephyr_subscribe_autoretry(("mail", "inbox", "%me%")) if options.resend_log: with open('/mit/tabbott/Private/zephyrs', 'r') as log: