* [PATCH v1 0/2] A bug fix and allow custom MySQL port @ 2022-12-24 22:00 Ammar Faizi 2022-12-24 22:00 ` [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function Ammar Faizi 2022-12-24 22:00 ` [PATCH v1 2/2] Allow custom MySQL port from the env file Ammar Faizi 0 siblings, 2 replies; 6+ messages in thread From: Ammar Faizi @ 2022-12-24 22:00 UTC (permalink / raw) To: GNU/Weeb Mailing List Cc: Ammar Faizi, Muhammad Rizki, Alviro Iskandar Setiawan From: Ammar Faizi <[email protected]> Hi, There are two patches in this series. A bug fix due to recent commit: ce17f3e8016c ("telegram: Implement the log message for catching errors") Also, add a new environment variable DB_PORT for allowing custom MySQL server port. Signed-off-by: Ammar Faizi <[email protected]> --- Ammar Faizi (2): telegram: listener: Fix missing MySQL error recovery function Allow custom MySQL port from the env file daemon/dc.py | 7 +++++++ daemon/discord.env.example | 1 + daemon/telegram.env.example | 1 + daemon/telegram/mailer/listener.py | 21 ++++++++++----------- daemon/tg.py | 7 +++++++ 5 files changed, 26 insertions(+), 11 deletions(-) base-commit: f81cc5ddc5e577f01b8fc916ca43e4c1248f4276 -- Ammar Faizi ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function 2022-12-24 22:00 [PATCH v1 0/2] A bug fix and allow custom MySQL port Ammar Faizi @ 2022-12-24 22:00 ` Ammar Faizi 2022-12-24 22:59 ` Muhammad Rizki 2022-12-24 22:00 ` [PATCH v1 2/2] Allow custom MySQL port from the env file Ammar Faizi 1 sibling, 1 reply; 6+ messages in thread From: Ammar Faizi @ 2022-12-24 22:00 UTC (permalink / raw) To: GNU/Weeb Mailing List Cc: Ammar Faizi, Muhammad Rizki, Alviro Iskandar Setiawan From: Ammar Faizi <[email protected]> When I restarted the MySQL server, the program kept looping in the report function, reporting the same error: "Lost connection to MySQL server during query" This happened because commit ce17f3e8016c ("telegram: Implement the log message for catching errors") added an error logging using a 'try and except' statement. This statement effectively kills the MySQL recovery function that the caller ('__run' function) provides. Create a new function wrapper, report_err(). Also, make sure the MySQL error recovery function is called. Fixes: ce17f3e8016c ("telegram: Implement the log message for catching errors") Signed-off-by: Ammar Faizi <[email protected]> --- daemon/telegram/mailer/listener.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index 5a182d0..044de6a 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -61,19 +61,23 @@ class Bot(): self.db.ping(reconnect=True, attempts=reconnect_attempts, delay=delay_in_secs) + async def report_err(caption): + if not caption: + caption = "No lore URL" + exc_str = utils.catch_err() + self.logger.warning(exc_str) + await self.client.send_log_file(caption) async def __run(self): self.logger.info("Running...") + url = None 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: - exc_str = utils.catch_err() - self.logger.warning(exc_str) - capt = "Unknown raw lore URL, see full details in the log file." - await self.client.send_log_file(capt) + await self.report_err(url) if not self.isRunnerFixed: self.isRunnerFixed = True @@ -89,13 +93,8 @@ class Bot(): async def __handle_atom_url(self, url): urls = await self.scraper.get_new_threads_urls(url) for url in urls: - 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) + mail = await self.scraper.get_email_from_url(url) + await self.__handle_mail(url, mail) async def __handle_mail(self, url, mail): -- Ammar Faizi ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function 2022-12-24 22:00 ` [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function Ammar Faizi @ 2022-12-24 22:59 ` Muhammad Rizki 0 siblings, 0 replies; 6+ messages in thread From: Muhammad Rizki @ 2022-12-24 22:59 UTC (permalink / raw) To: Ammar Faizi, GNU/Weeb Mailing List; +Cc: Alviro Iskandar Setiawan On 25/12/2022 05.00, Ammar Faizi wrote: > From: Ammar Faizi <[email protected]> > > When I restarted the MySQL server, the program kept looping in the > report function, reporting the same error: > > "Lost connection to MySQL server during query" > > This happened because commit ce17f3e8016c ("telegram: Implement the log > message for catching errors") added an error logging using a 'try and > except' statement. This statement effectively kills the MySQL recovery > function that the caller ('__run' function) provides. > > Create a new function wrapper, report_err(). Also, make sure the MySQL > error recovery function is called. > > Fixes: ce17f3e8016c ("telegram: Implement the log message for catching errors") > Signed-off-by: Ammar Faizi <[email protected]> > --- I will test this later, thanks. ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v1 2/2] Allow custom MySQL port from the env file 2022-12-24 22:00 [PATCH v1 0/2] A bug fix and allow custom MySQL port Ammar Faizi 2022-12-24 22:00 ` [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function Ammar Faizi @ 2022-12-24 22:00 ` Ammar Faizi 2022-12-24 22:57 ` Muhammad Rizki 1 sibling, 1 reply; 6+ messages in thread From: Ammar Faizi @ 2022-12-24 22:00 UTC (permalink / raw) To: GNU/Weeb Mailing List Cc: Ammar Faizi, Muhammad Rizki, Alviro Iskandar Setiawan From: Ammar Faizi <[email protected]> I don't always use 3306 as the MySQL server port. Allow custom port to be configured from the env file! Signed-off-by: Ammar Faizi <[email protected]> --- daemon/dc.py | 7 +++++++ daemon/discord.env.example | 1 + daemon/telegram.env.example | 1 + daemon/tg.py | 7 +++++++ 4 files changed, 16 insertions(+) diff --git a/daemon/dc.py b/daemon/dc.py index f9b57e5..a9d5ebd 100644 --- a/daemon/dc.py +++ b/daemon/dc.py @@ -29,10 +29,17 @@ def main(): logger = BotLogger(Platform.DISCORD) logger.init() + port = os.getenv("DB_PORT") + if not port: + port = 3306 + else: + port = int(port) + client = GWClient( db_conn=connector.connect( host=os.getenv("DB_HOST"), user=os.getenv("DB_USER"), + port=port, password=os.getenv("DB_PASS"), database=os.getenv("DB_NAME") ), diff --git a/daemon/discord.env.example b/daemon/discord.env.example index 60fcfac..de7e9f4 100644 --- a/daemon/discord.env.example +++ b/daemon/discord.env.example @@ -4,6 +4,7 @@ DISCORD_TOKEN= # Input your MySQL connection below DB_HOST= DB_USER= +DB_PORT=3306 DB_PASS= DB_NAME= diff --git a/daemon/telegram.env.example b/daemon/telegram.env.example index a070dd0..a42d86c 100644 --- a/daemon/telegram.env.example +++ b/daemon/telegram.env.example @@ -9,6 +9,7 @@ BOT_TOKEN= # MySQL Config DB_HOST= DB_USER= +DB_PORT=3306 DB_PASS= DB_NAME= diff --git a/daemon/tg.py b/daemon/tg.py index 8382c29..73ff2b9 100644 --- a/daemon/tg.py +++ b/daemon/tg.py @@ -21,6 +21,12 @@ def main(): logger = BotLogger() logger.init() + port = os.getenv("DB_PORT") + if not port: + port = 3306 + else: + port = int(port) + client = DaemonClient( "telegram/storage/EmailScraper", api_id=int(os.getenv("API_ID")), @@ -30,6 +36,7 @@ def main(): conn=connector.connect( host=os.getenv("DB_HOST"), user=os.getenv("DB_USER"), + port=port, password=os.getenv("DB_PASS"), database=os.getenv("DB_NAME") ), -- Ammar Faizi ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v1 2/2] Allow custom MySQL port from the env file 2022-12-24 22:00 ` [PATCH v1 2/2] Allow custom MySQL port from the env file Ammar Faizi @ 2022-12-24 22:57 ` Muhammad Rizki 2022-12-24 22:59 ` Ammar Faizi 0 siblings, 1 reply; 6+ messages in thread From: Muhammad Rizki @ 2022-12-24 22:57 UTC (permalink / raw) To: Ammar Faizi, GNU/Weeb Mailing List; +Cc: Alviro Iskandar Setiawan On 25/12/2022 05.00, Ammar Faizi wrote: > From: Ammar Faizi <[email protected]> > > I don't always use 3306 as the MySQL server port. Allow custom port > to be configured from the env file! > > Signed-off-by: Ammar Faizi <[email protected]> > --- > > + port = os.getenv("DB_PORT") > + if not port: > + port = 3306 > + else: > + port = int(port) > + > client = GWClient( > db_conn=connector.connect( > host=os.getenv("DB_HOST"), > user=os.getenv("DB_USER"), > + port=port, > password=os.getenv("DB_PASS"), > database=os.getenv("DB_NAME") > ), This code can be shorten to: client = GWClient( db_conn=connector.connect( host=os.getenv("DB_HOST"), user=os.getenv("DB_USER"), port=int(os.environ.get("DB_PORT", 3306)), password=os.getenv("DB_PASS"), database=os.getenv("DB_NAME") ) `os.environ` here is a dict object, a dict object has a .get() function, the second argument of the .get() is a default return if the first argument (dict key) is not exist. Like, DB_PORT is not exist in dict keys, then it will return 3306. > > + port = os.getenv("DB_PORT") > + if not port: > + port = 3306 > + else: > + port = int(port) > + > client = DaemonClient( > "telegram/storage/EmailScraper", > api_id=int(os.getenv("API_ID")), > @@ -30,6 +36,7 @@ def main(): > conn=connector.connect( > host=os.getenv("DB_HOST"), > user=os.getenv("DB_USER"), > + port=port, > password=os.getenv("DB_PASS"), > database=os.getenv("DB_NAME") > ), Implement this too. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v1 2/2] Allow custom MySQL port from the env file 2022-12-24 22:57 ` Muhammad Rizki @ 2022-12-24 22:59 ` Ammar Faizi 0 siblings, 0 replies; 6+ messages in thread From: Ammar Faizi @ 2022-12-24 22:59 UTC (permalink / raw) To: Muhammad Rizki, GNU/Weeb Mailing List; +Cc: Alviro Iskandar Setiawan On 12/25/22 5:57 AM, Muhammad Rizki wrote: > This code can be shorten to: > client = GWClient( > db_conn=connector.connect( > host=os.getenv("DB_HOST"), > user=os.getenv("DB_USER"), > port=int(os.environ.get("DB_PORT", 3306)), > password=os.getenv("DB_PASS"), > database=os.getenv("DB_NAME") > ) > > `os.environ` here is a dict object, a dict object has a .get() function, > the second argument of the .get() is a default return if the first argument (dict key) is not exist. Like, DB_PORT is not exist in dict keys, then it will return 3306. LGTM, but I already applied my patches. Please do that in your patch series later. -- Ammar Faizi ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-12-24 22:59 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-12-24 22:00 [PATCH v1 0/2] A bug fix and allow custom MySQL port Ammar Faizi 2022-12-24 22:00 ` [PATCH v1 1/2] telegram: listener: Fix missing MySQL error recovery function Ammar Faizi 2022-12-24 22:59 ` Muhammad Rizki 2022-12-24 22:00 ` [PATCH v1 2/2] Allow custom MySQL port from the env file Ammar Faizi 2022-12-24 22:57 ` Muhammad Rizki 2022-12-24 22:59 ` Ammar Faizi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox