From 11551b8abcab2ac1018e9cf2c35b0b590091d2cf Mon Sep 17 00:00:00 2001 From: miteruzo Date: Tue, 2 Dec 2025 12:39:26 +0900 Subject: [PATCH] =?UTF-8?q?=E5=AE=89=E5=85=A8=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 0c7b9b5..027ac57 100644 --- a/main.py +++ b/main.py @@ -164,7 +164,11 @@ def fetch_kiriban_list ( env = os.environ, capture_output = True, text = True) - kl: list[list[int | str]] = json.loads (result.stdout) + kl: list[list[int | str]] + try: + kl = json.loads (result.stdout) + except Exception: + kl = [] return [(cast (int, k[0]), video_info, str_to_datetime (cast (str, k[2]))) for k in kl @@ -287,14 +291,25 @@ async def report_hot_spring_time ( async def reconnect_db ( ) -> None: while True: - await asyncio.sleep (1800) - try: + await asyncio.sleep (600) + run_with_mysql_retry (DB.reconnect, 'mysql') + + +def ensure_mysql_alive (): + conn = DB.connection ('mysql').get_connection () + conn.ping (True) + + +def run_with_mysql_retry (fn, *args, **kwargs): + try: + ensure_mysql_alive () + return fn (*args, **kwargs) + except Exception as ex: + if getattr (ex, 'args', [None])[0] in (2006, 2013): DB.reconnect ('mysql') - except Exception as e: - if getattr (e, 'args', [None])[0] == 2006: - print ('堕ちたな(確信).') - else: - raise + ensure_mysql_alive () + return fn (*args, **kwargs) + raise def _add_query ( @@ -310,8 +325,9 @@ def _add_query ( query.model = GPTModel.GPT4_O.value query.sent_at = datetime.now () query.answered = False - query.transfer_data = transfer_data - query.save () + if transfer_data is not None: + query.transfer_data = transfer_data + run_with_mysql_retry (query.save) def _format_elapsed (