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 [101.128.126.135]) by gnuweeb.org (Postfix) with ESMTPSA id B385D7E416; Tue, 3 Jan 2023 06:37:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1672727843; bh=2sFps+OPKFPl6CiuTO+yP2OWFnkUfyxRlEEChe5rES8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=srmKGb2a9r4rdtYQW/jiT9Vod9Oa159vXvPocDyb9oyx5JuUQNqoyMRfh2PKnXuA3 en8vx8UqYzhS/BmrwD4L4pueyLXFPa/krzEdSlmx3KkaQ1kO6hrzPYujqQs7d73K+t Ocw7OSx1CD9mA4j4QxKypzG0Oio0uzVwPqzzjYw4FXGJ++TWopPqYnR7GkJe19F1mP NFGA0JGqj8KjSN8zEsb3TLDilhbQ4CrvYcCxoRNzM1Iu8Pou3BwDO1x6XwjiWqF7jN ogTei2SFx9KXsgRcJA3JbDwcfOWQKp6fENEyiGGXDWhJggYu0fJraEoXgvYrNO48AH z35dF1uWHierQ== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 10/13] feat(discord): Add handle_db_error in the listener.py Date: Tue, 3 Jan 2023 13:36:38 +0700 Message-Id: <20230103063641.1680-11-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20230103063641.1680-1-kiizuha@gnuweeb.org> References: <20230103063641.1680-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: After added a ping() method in the database, we should handle the database error such as connection error. This function is to handle the connection error while the MySQL service has been restarted. Signed-off-by: Muhammad Rizki --- daemon/dscord/mailer/listener.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/daemon/dscord/mailer/listener.py b/daemon/dscord/mailer/listener.py index b45c9d0..ec58f6a 100644 --- a/daemon/dscord/mailer/listener.py +++ b/daemon/dscord/mailer/listener.py @@ -49,6 +49,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 -- Muhammad Rizki