mirror of
https://git.squeakyrat.moe/faggot-chan/
synced 2026-11-05 02:45:45 +01:00
gr
This commit is contained in:
1 parent
bdd7d1f57c
commit
64af28f820
1 file changed
+18
-15
@@ -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]:
|
async def call_main(payload: str) -> tuple[bool, str]:
|
||||||
global _main_ws
|
global _main_ws
|
||||||
try:
|
for attempt in range(2): # retry once with a fresh connection if the cached one turned out dead
|
||||||
if _main_ws is None or _main_ws.closed:
|
try:
|
||||||
_main_ws = await websockets.connect(MAIN_URI, open_timeout=None, ping_timeout=None)
|
if _main_ws is None:
|
||||||
await _main_ws.send(payload)
|
_main_ws = await websockets.connect(MAIN_URI, open_timeout=None, ping_timeout=None)
|
||||||
raw = await _main_ws.recv()
|
await _main_ws.send(payload)
|
||||||
data = json.loads(raw)
|
raw = await _main_ws.recv()
|
||||||
if "error" in data:
|
data = json.loads(raw)
|
||||||
return False, data["error"]
|
if "error" in data:
|
||||||
return True, data["ok"]
|
return False, data["error"]
|
||||||
except websockets.exceptions.ConnectionClosedOK:
|
return True, data["ok"]
|
||||||
_main_ws = None
|
except websockets.exceptions.ConnectionClosedOK:
|
||||||
return False, f"[{now_str()}] websocket closed cleanly (no reply)"
|
_main_ws = None
|
||||||
except Exception as e:
|
return False, f"[{now_str()}] websocket closed cleanly (no reply)"
|
||||||
_main_ws = None
|
except Exception as e:
|
||||||
return False, f"[{now_str()}] websocket error: {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):
|
async def handle_error(error_msg: str, notify_channel=None, notify_user=None):
|
||||||
print(error_msg)
|
print(error_msg)
|
||||||
|
|||||||
Reference in new issue
Block a user