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 A542881134; Sun, 30 Oct 2022 05:49:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667108985; bh=LSxygQWi97fVcDEDrMY1Dq/1FF0YTyxYX5WLSfOwb7g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YbXRPRS3IMBkLKh7ZEJcWi20Y7rFZPeU4osGcJi70/JTydAtTRPukczJ00eE8fkdf 7CnptqJr5kyKmWSpBl/GSA9RjW20W6iMsyjjxj7aQJ2HvgjvoFzVOYwlqqFQw2bwnt DEJ8AHKnsfccjnxWcvlxvOfoxOFMI1NslJZkAyzm2IYYhPb/4T1/HN1uo/o4ATSWIc k/BT93IaTMBZVZ922GFho1XZQ2ujY0BkWNELqlc1SMKTqBmjXWASQvvKyEdWoFUlix YUl0yCc1uZcUun3PowUBQnkcAOWe715IFxRsEX0/PpRXl6zuZRrOsNPevQmsAdA6QJ bHjCheJTa/SCA== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH v2 2/2] daemon: telegram: Handle MySQL error Date: Sun, 30 Oct 2022 12:49:30 +0700 Message-Id: <20221030054930.573374-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221030054930.573374-1-ammarfaizi2@gnuweeb.org> References: <20221030054930.573374-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. Signed-off-by: Ammar Faizi --- daemon/telegram/mailer/listener.py | 31 ++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 1c92f23..4b48e21 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -7,6 +7,7 @@ from pyrogram.types import Message from apscheduler.schedulers.asyncio import AsyncIOScheduler from telegram.packages import DaemonClient +from mysql import connector from atom import Scraper from atom import utils from enums import Platform @@ -43,13 +44,35 @@ 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 connector.errors.OperationalError as e: + await self.handle_db_error(e) + except connector.errors.DatabaseError as e: + await self.handle_db_error(e) + except: + print(traceback.format_exc()) if not self.isRunnerFixed: self.isRunnerFixed = True -- Ammar Faizi