diff --git a/main.py b/main.py index 027ac57..b1d55f5 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ import random import subprocess from asyncio import Lock from datetime import date, datetime, time, timedelta -from typing import Any, TypedDict, cast +from typing import Any, Callable, TypedDict, cast import nicolib import queries_to_answers as q2a @@ -292,24 +292,48 @@ async def reconnect_db ( ) -> None: while True: await asyncio.sleep (600) - run_with_mysql_retry (DB.reconnect, 'mysql') + try: + ensure_mysql_alive () + except Exception as ex: + if getattr (ex, 'args', [None])[0] in (2006, 2013): + safe_reconnect () + else: + raise -def ensure_mysql_alive (): +def ensure_mysql_alive ( +) -> None: conn = DB.connection ('mysql').get_connection () - conn.ping (True) + conn.ping () -def run_with_mysql_retry (fn, *args, **kwargs): +def safe_reconnect ( +) -> None: try: - ensure_mysql_alive () - return fn (*args, **kwargs) + DB.reconnect ('mysql') except Exception as ex: - if getattr (ex, 'args', [None])[0] in (2006, 2013): - DB.reconnect ('mysql') + if getattr (ex, 'args', [None])[0] not in (2006, 2013): + raise + + +def run_with_mysql_retry ( + fn: Callable[..., Any], + *args, + **kwargs, +) -> Any: + last = None + for _ in range (2): + try: ensure_mysql_alive () return fn (*args, **kwargs) - raise + except Exception as ex: + last = ex + if getattr (ex, 'args', [None])[0] in (2006, 2013): + safe_reconnect () + continue + raise + if last: + raise last def _add_query ( diff --git a/nizika_ai b/nizika_ai index 5dae2ae..1f75763 160000 --- a/nizika_ai +++ b/nizika_ai @@ -1 +1 @@ -Subproject commit 5dae2ae038e1109f3c70d853d2b7dd7542e5e88e +Subproject commit 1f75763038adccd59699274816cbc37a944c8768