GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v3 02/11] telegram: Perform graceful exit when interrupted by a signal
Date: Sun,  8 Jan 2023 15:07:32 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

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 <[email protected]>
Signed-off-by: Muhammad Rizki <[email protected]>
---
 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 <[email protected]>
 #
 
+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


  parent reply	other threads:[~2023-01-08  8:08 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-08  8:07 [PATCH v3 00/11] Improvements, refactors, and features Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 01/11] discord: Remove unnecessary try/except block Muhammad Rizki
2023-01-08  8:07 ` Muhammad Rizki [this message]
2023-01-08  8:07 ` [PATCH v3 03/11] Add DaemonException() class Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 04/11] telegram: Refactor report_err() and move to the DaemonClient() class Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 05/11] telegram: Implement the DaemonException() and refactor report_err() Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 06/11] telegram: listener: Remove unnecessary try/except block Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 07/11] discord: add report_err() for the Discord bot Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 08/11] discord: Implement the report_err() and DaemonException() error class Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 09/11] discord: database: Add ping() method for reconnect MySQL Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 10/11] discord: listener: Add handle_db_error to handle the MySQL errors Muhammad Rizki
2023-01-08  8:07 ` [PATCH v3 11/11] telegram: Rename the Telegram bot custom client Muhammad Rizki
2023-01-08  8:34 ` [PATCH v3 00/11] Improvements, refactors, and features Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox