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 kanazawa.. (unknown [101.128.126.198]) by gnuweeb.org (Postfix) with ESMTPSA id 2634B7E245; Fri, 4 Nov 2022 18:10:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667585415; bh=XkVDmdiWlMXkzuTUbyZyLrUC0SQxK5gQU/TZWJbBdEg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rLk9Pf05jfNqaszHHan95RO3tzIfBbN+5pJsgC1zHJdWjfsOKjQ6VNVyHk6SNrMe7 Py9wQsISFKJv48Pk8vgfaUr5do1q98RvAKNNfPD/f0eYXwu/05BZDJz01CaLEj3LyY 2OC6+uAzm8dPvnAZkSfPUnjLmylH15vT5HYcxbZLksG5JHhel0LhIGkeDOptlOzqrW g56aWLnwC1zAlRWhWrfxXy4Rb/o7uwE7T1wddGHCc6qph/ge7VE1dyiERJ9Z0qOCYJ lLDxs+0doE7mb+KrO5/VGY8USErQ6McwQAJYbNu/TXRhZozRbtmqYoLLI6e/gGsV9I y6ltdGHLfA6mg== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 11/16] telegram: Implement the log message for catching errors Date: Sat, 5 Nov 2022 01:09:21 +0700 Message-Id: <20221104180931.3852-15-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221104180931.3852-1-kiizuha@gnuweeb.org> References: <20221104180931.3852-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: After created the BotLogger() class and initialized it to the DaemonClient() attribute, implement it in the telegram/mailer/listener.py to log it into the file if catching errors. v4: - Add logger v3: - Simplify try and except statement (comment from Muhammad Rizki). v2: * no changes * Signed-off-by: Muhammad Rizki --- daemon/telegram/mailer/listener.py | 44 ++++++++++++------- daemon/telegram/packages/client.py | 4 +- daemon/telegram/packages/decorator.py | 15 +++---- .../packages/plugins/commands/scrape.py | 10 ++++- 4 files changed, 45 insertions(+), 28 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 46ccf93..6fb2e44 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -13,7 +13,6 @@ from atom import utils from enums import Platform import asyncio import re -import traceback class BotMutexes(): @@ -29,6 +28,7 @@ class Bot(): self.scraper = scraper self.mutexes = mutexes self.db = client.db + self.logger = client.logger self.isRunnerFixed = False @@ -49,8 +49,8 @@ class Bot(): # TODO(ammarfaizi2): # Ideally, we also want to log and report this situation. # - print(f"Database error: {str(e)}") - print("Reconnecting to the database...") + 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. @@ -63,14 +63,17 @@ class Bot(): async def __run(self): - print("[__run]: Running...") + self.logger.info("Running...") 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: - print(traceback.format_exc()) + exc_str = utils.catch_err() + self.logger.warning(exc_str) + capt = "Unkown raw lore URL, see full details in the log file." + await self.client.send_log_file(capt) if not self.isRunnerFixed: self.isRunnerFixed = True @@ -86,8 +89,13 @@ class Bot(): async def __handle_atom_url(self, url): urls = await self.scraper.get_new_threads_urls(url) for url in urls: - mail = await self.scraper.get_email_from_url(url) - await self.__handle_mail(url, mail) + try: + mail = await self.scraper.get_email_from_url(url) + await self.__handle_mail(url, mail) + except: + exc_str = utils.catch_err() + self.logger.warning(exc_str) + await self.client.send_log_file(url) async def __handle_mail(self, url, mail): @@ -104,23 +112,27 @@ class Bot(): # @__must_hold(self.mutexes.send_to_tg) async def __send_mail(self, url, mail, tg_chat_id): email_msg_id = utils.get_email_msg_id(mail) + if not email_msg_id: - # - # It doesn't have a Message-Id. - # A malformed email. Skip! - # + md = "email_msg_id not detected, skipping malformed email" + self.logger.debug(md) return False email_id = self.__mail_id_from_db(email_msg_id, tg_chat_id) if not email_id: - # - # Email has already been sent to Telegram. - # Skip! - # + md = f"Skipping {email_id} because has already been sent to Telegram" + self.logger.debug(md) return False - text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) + try: + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) + except: + exc_str = utils.catch_err() + self.logger.warning(exc_str) + await self.client.send_log_file(url) + return + reply_to = self.get_reply(mail, tg_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py index 6c83a5a..58195b9 100644 --- a/daemon/telegram/packages/client.py +++ b/daemon/telegram/packages/client.py @@ -46,7 +46,7 @@ class DaemonClient(Client): url: str = None, parse_mode: ParseMode = ParseMode.HTML ) -> Message: - print("[send_text_email]") + self.logger.debug("[send_text_email]") return await self.send_message( chat_id=chat_id, text=text, @@ -71,7 +71,7 @@ class DaemonClient(Client): url: str = None, parse_mode: ParseMode = ParseMode.HTML ) -> Message: - print("[send_patch_email]") + self.logger.debug("[send_patch_email]") tmp, doc, caption, url = utils.prepare_patch( mail, text, url, Platform.TELEGRAM ) diff --git a/daemon/telegram/packages/decorator.py b/daemon/telegram/packages/decorator.py index c7a5f02..153fa95 100644 --- a/daemon/telegram/packages/decorator.py +++ b/daemon/telegram/packages/decorator.py @@ -24,20 +24,19 @@ def handle_flood(func: Callable[[T], T]) -> Callable[[T], T]: try: return await func(*args) except FloodWait as e: - # - # Aiee... we hit our limit. - # Let's slow down a bit. - # - _flood_exceptions(e) - print("[__handle_telegram_floodwait]: Woken up from flood wait...") + # Calling logger attr from the DaemonClient() class + logger = args[0].logger + + _flood_exceptions(e, logger) + logger.info("Woken up from flood wait...") return callback -async def _flood_exceptions(e): +async def _flood_exceptions(e, logger): x = re.search(r"A wait of (\d+) seconds is required", str(e)) if not x: raise e n = int(x.group(1)) - print(f"[____handle_telegram_floodwait]: Sleeping for {n} seconds due to Telegram limit") + logger.info(f"Sleeping for {n} seconds due to Telegram limit") await asyncio.sleep(n) diff --git a/daemon/telegram/packages/plugins/commands/scrape.py b/daemon/telegram/packages/plugins/commands/scrape.py index 29cc8ad..f89727d 100644 --- a/daemon/telegram/packages/plugins/commands/scrape.py +++ b/daemon/telegram/packages/plugins/commands/scrape.py @@ -36,8 +36,14 @@ async def scrap_email(c: DaemonClient, m: Message): return s = Scraper() - mail = await s.get_email_from_url(url) - text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) + + try: + mail = await s.get_email_from_url(url) + text, files, is_patch = utils.create_template(mail, Platform.TELEGRAM) + except: + exc_str = utils.catch_err() + c.logger.warning(exc_str) + await c.send_log_file(url) if is_patch: m = await c.send_patch_email( -- Muhammad Rizki