From: Ammar Faizi <[email protected]>
To: GNU/Weeb Mailing List <[email protected]>
Cc: Ammar Faizi <[email protected]>,
Muhammad Rizki <[email protected]>,
Alviro Iskandar Setiawan <[email protected]>
Subject: [PATCH v1 1/2] daemon: telegram: db: Allow the caller to reconnect
Date: Thu, 27 Oct 2022 22:08:22 +0700 [thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
The daemon is totally unusable when the MySQL server is restarted. It's
spinning in a loop with the following error:
mysql.connector.errors.OperationalError: 2013 (HY000): Lost connection to MySQL server during query
When it happens, the only way to fix the situation is: restart the
daemon manually, which is obviously not productive. Create a mechanism
in the class DB to allow the caller to reconnect. This way, the caller
can automatically reconnect without having the user restart the daemon.
Signed-off-by: Ammar Faizi <[email protected]>
---
daemon/telegram/database/core.py | 31 +++++++++++++++++++++++++-----
daemon/telegram/packages/client.py | 8 ++++----
daemon/tg.py | 4 ++--
3 files changed, 32 insertions(+), 11 deletions(-)
diff --git a/daemon/telegram/database/core.py b/daemon/telegram/database/core.py
index c34d7a8..bc65b54 100644
--- a/daemon/telegram/database/core.py
+++ b/daemon/telegram/database/core.py
@@ -4,17 +4,38 @@
# Copyright (C) 2022 Ammar Faizi <[email protected]>
#
-
+from mysql import connector
from .methods import DBMethods
class DB(DBMethods):
- def __init__(self, conn):
- self.conn = conn
+ def __init__(self, host, user, password, database):
+ self.host = host
+ self.user = user
+ self.password = password
+ self.database = database
+ self.conn = None
+ self.connect()
+
+ #
+ # Allow the caller to reconnect.
+ #
+ def connect(self):
+ if self.conn:
+ self.close()
+
+ self.conn = connector.connect(
+ host=self.host,
+ user=self.user,
+ password=self.password,
+ database=self.database
+ )
self.conn.autocommit = True
self.cur = self.conn.cursor(buffered=True)
-
- def __del__(self):
+ def close(self):
self.cur.close()
self.conn.close()
+
+ def __del__(self):
+ self.close()
diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py
index c971ea1..2ee37f6 100644
--- a/daemon/telegram/packages/client.py
+++ b/daemon/telegram/packages/client.py
@@ -15,11 +15,11 @@ from .decorator import handle_flood
class DaemonClient(Client):
- def __init__(self, name: str, api_id: int,
- api_hash: str, conn, **kwargs):
+ def __init__(self, name: str, api_id: int, api_hash: str, db: DB,
+ **kwargs):
super().__init__(name, api_id,
- api_hash, **kwargs)
- self.db = DB(conn)
+ api_hash, **kwargs)
+ self.db = db
@handle_flood
diff --git a/daemon/tg.py b/daemon/tg.py
index 5f8c21e..e6d5d05 100644
--- a/daemon/tg.py
+++ b/daemon/tg.py
@@ -6,11 +6,11 @@
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
-from mysql import connector
from atom import Scraper
from telegram.packages import DaemonClient
from telegram.mailer import BotMutexes
from telegram.mailer import Bot
+from telegram.database import DB
import os
@@ -22,7 +22,7 @@ def main():
api_id=int(os.getenv("API_ID")),
api_hash=os.getenv("API_HASH"),
bot_token=os.getenv("BOT_TOKEN"),
- conn=connector.connect(
+ db=DB(
host=os.getenv("DB_HOST"),
user=os.getenv("DB_USER"),
password=os.getenv("DB_PASS"),
--
Ammar Faizi
next prev parent reply other threads:[~2022-10-27 15:08 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-27 15:08 [PATCH v1 0/2] Automatic recovery from a MySQL restart Ammar Faizi
2022-10-27 15:08 ` Ammar Faizi [this message]
2022-10-27 16:52 ` [PATCH v1 1/2] daemon: telegram: db: Allow the caller to reconnect Muhammad Rizki
2022-10-27 16:54 ` Muhammad Rizki
2022-10-28 5:48 ` Ammar Faizi
2022-10-27 15:08 ` [PATCH v1 2/2] daemon: telegram: Handle MySQL error Ammar Faizi
2022-10-28 9:21 ` Alviro Iskandar Setiawan
2022-10-28 9:28 ` Ammar Faizi
2022-10-28 9:24 ` Alviro Iskandar Setiawan
2022-10-28 9:32 ` Ammar Faizi
2022-10-28 9:40 ` Alviro Iskandar Setiawan
2022-10-28 9:43 ` Ammar Faizi
2022-10-28 16:29 ` Ammar Faizi
2022-10-28 16:46 ` Alviro Iskandar Setiawan
2022-10-28 16:54 ` Ammar Faizi
2022-10-28 18:10 ` Muhammad Rizki
2022-10-28 18:26 ` Alviro Iskandar Setiawan
2022-10-28 18:52 ` Muhammad Rizki
2022-10-28 19:08 ` Alviro Iskandar Setiawan
2022-10-28 19:15 ` Muhammad Rizki
2022-10-28 19:29 ` Alviro Iskandar Setiawan
2022-10-28 19:34 ` Ammar Faizi
2022-10-28 19:36 ` Ammar Faizi
2022-10-28 19:38 ` Ammar Faizi
2022-10-28 19:44 ` Muhammad Rizki
2022-10-28 19:39 ` Muhammad Rizki
2022-10-28 19:44 ` Alviro Iskandar Setiawan
2022-10-28 19:46 ` Muhammad Rizki
2022-10-28 19:53 ` Alviro Iskandar Setiawan
2022-10-28 19:16 ` Ammar Faizi
2022-10-28 19:19 ` Muhammad Rizki
2022-10-28 19:19 ` Ammar Faizi
2022-10-28 19:22 ` Muhammad Rizki
2022-10-28 19:33 ` Ammar Faizi
2022-10-28 19:38 ` Muhammad Rizki
2022-10-28 17:02 ` Muhammad Rizki
2022-10-28 17:19 ` Ammar Faizi
2022-10-28 18:15 ` Muhammad Rizki
2022-10-28 18:18 ` Muhammad Rizki
2022-10-28 19:49 ` Ammar Faizi
2022-10-28 19:55 ` Muhammad Rizki
2022-10-28 19:46 ` 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