aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordavid <[email protected]>2026-09-08 02:12:15 -0300
committerdavid <[email protected]>2026-09-08 02:12:15 -0300
commit64af28f8207697dc061f6b275f7e1b7b7965e122 (patch)
tree5486180c030e523940f6ba5a1de32888cc85ce27
parentbdd7d1f57ce748c00929394c207c3d2dc9f12bda (diff)
downloadfaggot-chan-64af28f8207697dc061f6b275f7e1b7b7965e122.tar.gz
faggot-chan-64af28f8207697dc061f6b275f7e1b7b7965e122.zip
gr
-rw-r--r--bot.py33
1 files changed, 18 insertions, 15 deletions
diff --git a/bot.py b/bot.py
index cdb14e4..c912ad5 100644
--- a/bot.py
+++ b/bot.py
@@ -110,21 +110,24 @@ _main_ws = None # kept open across messages instead of reconnecting every time
async def call_main(payload: str) -> tuple[bool, str]:
global _main_ws
- try:
- if _main_ws is None or _main_ws.closed:
- _main_ws = await websockets.connect(MAIN_URI, open_timeout=None, ping_timeout=None)
- await _main_ws.send(payload)
- raw = await _main_ws.recv()
- data = json.loads(raw)
- if "error" in data:
- return False, data["error"]
- return True, data["ok"]
- except websockets.exceptions.ConnectionClosedOK:
- _main_ws = None
- return False, f"[{now_str()}] websocket closed cleanly (no reply)"
- except Exception as e:
- _main_ws = None
- return False, f"[{now_str()}] websocket error: {e}"
+ for attempt in range(2): # retry once with a fresh connection if the cached one turned out dead
+ try:
+ if _main_ws is None:
+ _main_ws = await websockets.connect(MAIN_URI, open_timeout=None, ping_timeout=None)
+ await _main_ws.send(payload)
+ raw = await _main_ws.recv()
+ data = json.loads(raw)
+ if "error" in data:
+ return False, data["error"]
+ return True, data["ok"]
+ except websockets.exceptions.ConnectionClosedOK:
+ _main_ws = None
+ return False, f"[{now_str()}] websocket closed cleanly (no reply)"
+ except Exception as e:
+ _main_ws = None
+ if attempt == 0:
+ continue # one retry with a fresh connection before giving up
+ return False, f"[{now_str()}] websocket error: {e}"
async def handle_error(error_msg: str, notify_channel=None, notify_user=None):
print(error_msg)