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.140]) by gnuweeb.org (Postfix) with ESMTPSA id 6716080BE8; Thu, 27 Oct 2022 15:08:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666883316; bh=iWc5X/gTOMwJyKj3bhvMDcdjBneUj9d2K2icyUs9dzQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Iouu2zZJymXEjNmMYp2WrPaDsPV0QE3HUOV09TPwQsgzUoz4mDiGc4I0Kd7pbprxh gynxnrkgzey5tetqFYmIjtVNEpTGm8b/VeEQH3LhTKZAGp81FbPm1atzQGjFiPYtzN slnqcJlzQOnwC4t+xY92iI3gpAh/RtxS6Fbjw6l0g1H0YPtiHSYKuhPRGYuQw1dWyh PET7eoQJ55zwkCiErDBuNDKDmbv6Zus75BV7Mc19VSJV/PxQWPL++u0kIYoJl/3vgd sTZvQZwY78LmO5Xg7EWCES9v3/EW2bryZ1ytEeJd2c1eZCZuY3F+JPajpLGv1Wvk0c 9gcxhA/CtAoPw== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH v1 2/2] daemon: telegram: Handle MySQL error Date: Thu, 27 Oct 2022 22:08:23 +0700 Message-Id: <20221027150823.601914-3-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221027150823.601914-1-ammarfaizi2@gnuweeb.org> References: <20221027150823.601914-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 | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 1c92f23..8a61367 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 @@ -15,6 +16,7 @@ import re import traceback + class BotMutexes(): def __init__(self): self.send_to_tg = asyncio.Lock() @@ -43,13 +45,32 @@ 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...") + self.db.connect() + + # + # Don't do this too often, avoid connect() burst. + # + await asyncio.sleep(3) + + 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