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 0CBC77E581; Sun, 8 Jan 2023 06:16:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673158565; bh=+YBF9dDih7SDfN4+SzeZKVX90YQFsQXuagQsabbRTZ4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iW3afHVNJlzjOwSY9d/9n9f7rg76DYU1q6+3QXYDq33A3G9qSy6xY3M73UJxPgD/k 10Hd1hOCUrsH555zwehem810Xxc+tk+N3Phitzvlxg2Zrn0zVCBdzVhVdHEhAGKkvU B/FipxR5C8ENUrbtvZsRKJCEsJEbcGbj6d6h+5Pr90hupBw5dd5NKZS+zeLo7guokW 58kUfAIgy3J+FFJJo3C0EKRMthK1+wJZdvExe/fleATH1mE4TzGRxLQ52v0TVM1YDR dSguHxVm/yv0Ee+E49MzX1Gisk4LcmEsO7WOnHsb9KvXvGriMTBguJZbCJYOzTEFcf KuB2PPczl365w== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v2 02/11] telegram: Perform graceful exit when interrupted by a signal Date: Sun, 8 Jan 2023 13:15:34 +0700 Message-Id: <20230108061543.1780-3-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: We need to start the Telegram bot before starting the listener. This way, if an error occurs, especially while parsing the email message, the listener will be able to send a report message to the channel, since the Telegram bot will already be running. Also, move them to the run() function in the listener. Corrected-by: Alviro Iskandar Setiawan Signed-off-by: Muhammad Rizki --- daemon/telegram/mailer/listener.py | 7 +++++++ daemon/tg.py | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 044de6a..6482a66 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -4,6 +4,7 @@ # Copyright (C) 2022 Ammar Faizi # +from pyrogram import idle from pyrogram.types import Message from mysql.connector.errors import OperationalError, DatabaseError from apscheduler.schedulers.asyncio import AsyncIOScheduler @@ -37,12 +38,18 @@ class Bot(): # Execute __run() once to avoid high latency at # initilization. # + self.sched.start() + self.client.start() + self.runner = self.sched.add_job( func=self.__run, misfire_grace_time=None, max_instances=1 ) + idle() + self.client.stop() + async def handle_db_error(self, e): # diff --git a/daemon/tg.py b/daemon/tg.py index 73ff2b9..a676cf5 100644 --- a/daemon/tg.py +++ b/daemon/tg.py @@ -7,6 +7,7 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler from dotenv import load_dotenv from mysql import connector +from pyrogram import idle from atom import Scraper from telegram.packages import DaemonClient from telegram.mailer import BotMutexes @@ -58,10 +59,7 @@ def main(): scraper=Scraper(), mutexes=BotMutexes() ) - sched.start() bot.run() - client.run() - if __name__ == '__main__': main() -- Muhammad Rizki