From 299f4b47d26b7a8addb42420dda524c4e78fba87 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 4 Jun 2026 15:01:15 -0400 Subject: Back off Zulip connection retries --- main.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 77ce660..96eb7ef 100644 --- a/main.py +++ b/main.py @@ -50,14 +50,27 @@ if not SYSTEM_PROMPT_PATH.is_absolute(): SYSTEM_PROMPT = SYSTEM_PROMPT_PATH.read_text().strip() -log.info("Connecting to Zulip...") -client = zulip.Client(config_file=".zuliprc") -log.info("Connected as %s", client.email) +client = None executor = ThreadPoolExecutor(max_workers=WORKERS, thread_name_prefix="slopbot") agent_slots = Semaphore(AGENT_WORKERS) +def connect_zulip() -> zulip.Client: + """Connect to Zulip with exponential backoff instead of crash-looping.""" + backoff = 5 + while True: + try: + log.info("Connecting to Zulip...") + connected = zulip.Client(config_file=".zuliprc") + log.info("Connected as %s", connected.email) + return connected + except Exception as e: + log.warning("Failed to connect to Zulip (%s); retrying in %ss", e, backoff) + time.sleep(backoff) + backoff = min(backoff * 2, 300) + + # --------------------------------------------------------------------------- # Zulip context # --------------------------------------------------------------------------- @@ -233,6 +246,8 @@ def handle_event(event: dict) -> None: def main() -> None: + global client + client = connect_zulip() log.info("Listening for messages...") client.call_on_each_event(handle_event, event_types=["message"]) -- cgit v1.2.3