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,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [182.253.183.140]) by gnuweeb.org (Postfix) with ESMTPSA id 0987781366; Thu, 27 Oct 2022 15:08:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666883313; bh=ooVB/lgW4QaLjRxPcYvSHfsub2w+H8SYZEiXQNaw/P4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X4g17EWUd6gcqC3YgqUAsFkECcayIKaOyU66aKTbqvG6sFObbAG6NqEmCTZ1XBr1h atTTAgoYYax+cRy7GmQPZFPpPV2S3LqFR1TDNqfI3euno/U8Oy7V713W1mgSjPfFpB /Ge4TLH1/pOnWKa+dD3OGVWJI0VH/Ol0QF9JVXg92Jb71yWyRYueh/4Tg/FIlGSTfJ w6H5Rasf3OBcGaj5/m34LnVls3qIJ0/L3eIS7g3Ozvt2srSywXFnoXMm9iJZaOPeoQ oWt7iP7xKvY+x2z75Dwk+XJwHrW7ZlvvFDSB79KIUg2Gz6QSfG8CFMAzorctt40JSM Uo7o6ph7C8p/g== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH v1 1/2] daemon: telegram: db: Allow the caller to reconnect Date: Thu, 27 Oct 2022 22:08:22 +0700 Message-Id: <20221027150823.601914-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221027150823.601914-1-ammarfaizi2@gnuweeb.org> References: <20221027150823.601914-1-ammarfaizi2@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 --- 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 # - +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