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 6140B7E427; Sun, 8 Jan 2023 08:08:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673165301; bh=nUovhduy9KCptJPeU46CgrZVtarQVdPLsf/juhYkZ0w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dYUnWVpfVNbZltbC6g+t4Mv7dNa/0aGJp6VJj+wUzZdtdGp9zbIK/9mtyTJp/arx+ VLG9QuwIRNUGpZp2ZOAij5UjtbqXtW8H18mxYr3hXSqmVDWyqf1uYwkt7jtvCTz3D2 DZxWD2v+ywNxW+9U8gG3Ksi36nSHFSjsENkTFQSbB1tPQdq0lIn7arWPYaJey7lIf5 fANb0iLbc84oMGcJVsa4ckvYiEjH9RYaJtB+OAuITqDevuj1D9QUvX2wRaK9ajyMuF 7HfWN6LsLVDJT9vRjBMlWW0cz6HO/t5Rme5bN863kCz9wzHdVkIzk3vewiz+1FAs5Z yXqb63xHa/m0A== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v3 10/11] discord: listener: Add handle_db_error to handle the MySQL errors Date: Sun, 8 Jan 2023 15:07:40 +0700 Message-Id: <20230108080741.1914-11-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20230108080741.1914-1-kiizuha@gnuweeb.org> References: <20230108080741.1914-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 aa44603..fc066b7 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