diff options
| author | Ben Sima <ben@bensima.com> | 2026-06-04 15:01:15 -0400 |
|---|---|---|
| committer | Ben Sima <ben@bensima.com> | 2026-06-04 15:01:15 -0400 |
| commit | 299f4b47d26b7a8addb42420dda524c4e78fba87 (patch) | |
| tree | 4d5501ee1554cdc4463bb3f36cdc4bbb12f855c4 /main.py | |
| parent | 064ac261533f146cf675af63d18c7505e1bc74b7 (diff) | |
Back off Zulip connection retries
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -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"]) |
