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 C44E87E375; Sun, 8 Jan 2023 08:08:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1673165283; bh=6f9LHw59ItjdQwRxJJliadS309ev12Y5BBJFk+0XMRA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SrgCusmt09UDtv/B5+l35nbqNVOIcr33x0BWSeQQRhkRvL6MYpmUI0Jm0K6Pqxc6N d1iFVUbTVnt2xqbdwwBsIeU1l7tMKoJ8570oT6cGLTTgeEGfIoN3C1JHxaOAzoT88f bkLYXOBKkan/CwV4FoZ71+1YYzXBdO6ZDoDut7R86PW/E1LO3YR1pDkgoFhxfmx415 8tsS+Vfb8OgjzjTh8HiYV2x6vB6ZMRpxZ8J9OL2ZZ5fkbxWrY45AwH89jDHAyXVFFU LkeVa8TRoSs15oAYrAQcA09En60CqUFaunyxT1oxN/iYDoOPTqI+pqyI+zn06PHrwY scOby5jZLqr4Q== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v3 02/11] telegram: Perform graceful exit when interrupted by a signal Date: Sun, 8 Jan 2023 15:07:32 +0700 Message-Id: <20230108080741.1914-3-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: 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. Acked-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