|
|
|
@@ -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 ( |
|
|
|
|