Browse Source

エラー落ち軽減?

main
みてるぞ 1 week ago
parent
commit
ca250d507d
2 changed files with 35 additions and 11 deletions
  1. +34
    -10
      main.py
  2. +1
    -1
      nizika_ai

+ 34
- 10
main.py View File

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


+ 1
- 1
nizika_ai

@@ -1 +1 @@
Subproject commit 5dae2ae038e1109f3c70d853d2b7dd7542e5e88e
Subproject commit 1f75763038adccd59699274816cbc37a944c8768

Loading…
Cancel
Save