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 autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.126.135]) by gnuweeb.org (Postfix) with ESMTPSA id 9DBF97E375; Sun, 8 Jan 2023 06:16:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673158583; bh=D3XJ3kPZaTSOMjlQSq7kat1792Mmzk+uFd69KLWYeiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=twSJquEMGMwDZB81zmtHFKTd9sylwRTrb4FQbGrrgFWjpXjTeqyR/yj9dbTXQIlFm CCbc3gvgScq6ssEMadEwebVawBSXjO4IHxFsJxhMDLLp/azjwm5fOkXpAGXS7b+edb MAdSZ7Wdk2EbtbyaumGS74wrj6w/N2K/woHdCdmX0S184vFS0Byi4PfGDqtQUe02VW 8ibdjtYkpHsP/BYgmNfWwAg5MRkgBWH/o7AmMwDdU0nXbAMzU07qcR9IwAERdz5Fkn 0Q4PSUiBJPysspn4z1HqQRGCnUf9l94lC+u5EqYkt4HZMgcAVCnhKIeZSGNaZNK5ns XUEEHyKrv2VRA== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v2 10/11] discord: listener: Add handle_db_error to handle the MySQL errors Date: Sun, 8 Jan 2023 13:15:42 +0700 Message-Id: <20230108061543.1780-11-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20230108061543.1780-1-kiizuha@gnuweeb.org> References: <20230108061543.1780-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: After adding a ping() method to the database, you should handle database errors such as connection errors. This function will be used to handle connection errors that occur when the MySQL service is restarted. Signed-off-by: Muhammad Rizki --- daemon/dscord/mailer/listener.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/daemon/dscord/mailer/listener.py b/daemon/dscord/mailer/listener.py index dc77ad2..cb2849a 100644 --- a/daemon/dscord/mailer/listener.py +++ b/daemon/dscord/mailer/listener.py @@ -6,6 +6,7 @@ import asyncio import re +from mysql.connector.errors import OperationalError, DatabaseError from apscheduler.schedulers.asyncio import AsyncIOScheduler from discord import File from discord import Message @@ -50,6 +51,24 @@ class Listener: self.runner = self.sched.add_job(func=self.__run) + async def handle_db_error(self, e): + # + # TODO(ammarfaizi2): + # Ideally, we also want to log and report this situation. + # + self.logger.error(f"Database error: {str(e)}") + self.logger.info("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): self.logger.info("Running...") url = None @@ -57,6 +76,8 @@ class Listener: try: for url in self.db.get_atom_urls(): await self.__handle_atom_url(url) + except (OperationalError, DatabaseError) as e: + await self.handle_db_error(e) except DaemonException as e: e.set_atom_url(url) await self.client.report_err(e) -- Muhammad Rizki