From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [182.253.183.90]) by gnuweeb.org (Postfix) with ESMTPSA id C8FB781417; Mon, 31 Oct 2022 16:13:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667232789; bh=bwTpMQ7133BZ38p3qrmE3NFSOR+cGfjjAxKymQmtUlI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FuuCZZOAtgGtDHADvoEOxsVEv2cwzCO2lihhpqsHaUWF4vhyH8C0+DMznGy99elIo l0vVQEfZktQafDt464Q9QLOqqT3aqM6VU+eGUITSu/g3OlTaXMFpANmBdLOE6flRbI 4ObssisAjfk2WbVdf54jV0whBL23dfAd4XOkw1LD2/fKeYH65LzSK6nFtyesTbSibG T6wpO/dQXIcZ6uZ6oQSEVWc4mp19qmW2be1XuCQh8W/zFot+gWj7bozULXkqGq8Oi0 fTejLy6MccM9rJ9SOL5941DhXXdOj/2V8jtnJ6SDtJp9MxYFPicrmBbJHwXJ5hj94X 6/kzLJ3/HvwSg== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH RESEND v3 2/2] daemon: telegram: Handle MySQL error Date: Mon, 31 Oct 2022 23:12:56 +0700 Message-Id: <20221031161256.199149-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221031161256.199149-1-ammarfaizi2@gnuweeb.org> References: <20221031161256.199149-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: A previous patch creates a mechanism to allow the caller to reconnect to the database by calling db.connect(). Handle MySQL error in the main daemon loop path. Do reconnect to the database if such an error happens. This way, the daemon can automatically recover from the MySQL server restart without having the user restart the daemon. v3: - Simplify try and except statement (comment from Muhammad Rizki). v2: * no changes * Signed-off-by: Ammar Faizi --- daemon/telegram/mailer/listener.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 1c92f23..46ccf93 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -5,6 +5,7 @@ # from pyrogram.types import Message +from mysql.connector.errors import OperationalError, DatabaseError from apscheduler.schedulers.asyncio import AsyncIOScheduler from telegram.packages import DaemonClient from atom import Scraper @@ -43,13 +44,33 @@ class Bot(): ) + async def handle_db_error(self, e): + # + # TODO(ammarfaizi2): + # Ideally, we also want to log and report this situation. + # + print(f"Database error: {str(e)}") + print("Reconnecting to the database...") + + # + # Don't do this too often to avoid reconnect burst. + # + delay_in_secs = 3 + reconnect_attempts = 3 + + self.db.ping(reconnect=True, attempts=reconnect_attempts, + delay=delay_in_secs) + + async def __run(self): print("[__run]: Running...") - for url in self.db.get_atom_urls(): - try: + try: + for url in self.db.get_atom_urls(): await self.__handle_atom_url(url) - except: - print(traceback.format_exc()) + except (OperationalError, DatabaseError) as e: + await self.handle_db_error(e) + except: + print(traceback.format_exc()) if not self.isRunnerFixed: self.isRunnerFixed = True -- Ammar Faizi