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=-1.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,NO_DNS_FOR_FROM, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from [192.168.1.2] (unknown [101.128.126.183]) by gnuweeb.org (Postfix) with ESMTPSA id 56B15805A4; Mon, 31 Oct 2022 16:34:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667234059; bh=biFpqjPrA0yhcvkdpPxy5LugMxEU2qEidXYZQYza00A=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=JdPrTFqcmLG6UnHRiY4qeDMQjzkGW6EWAt5BV4ahGJkndrDPMb2ZsjpBEPWxNkCtz 25kJb/z5HyfLip2zMmna8WLRFYb/cnw0diivLmAcmXM1qcDnuCqUZbK9cJD9sSs1cL f3bsGCxZwP5ps7jDTDpGAjr1/x1kMOsBxcsfBHFdTtV7CX7I36j7HYmDvFmdMUy8h+ uuent5Wr/vuJpszgamvzW8PJwziIsPWOdbebVjPWYwFbkA9Cw358hXKqFgIH4Sjatm TduRafRhQ+baxG8Uo0Yyn+tvFmsBF7GjpGIIUNQ0Iv6Veio4URDwvRU6/uIR7EipQQ axQxbuE2ehneQ== Message-ID: Date: Mon, 31 Oct 2022 23:34:15 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [PATCH RESEND v3 2/2] daemon: telegram: Handle MySQL error Content-Language: en-US To: Ammar Faizi , GNU/Weeb Mailing List Cc: Alviro Iskandar Setiawan References: <20221031161256.199149-1-ammarfaizi2@gnuweeb.org> <20221031161256.199149-3-ammarfaizi2@gnuweeb.org> From: Muhammad Rizki In-Reply-To: <20221031161256.199149-3-ammarfaizi2@gnuweeb.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: On 31/10/22 23.12, Ammar Faizi wrote: > 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 This fine too. Can't wait for you to apply. I will continue the logger version until its applied, thanks.