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.90]) by gnuweeb.org (Postfix) with ESMTPSA id 077B381411; Mon, 31 Oct 2022 16:13:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667232786; bh=iE3mt7W1pBzoHrOOH9P0hEwfgthMl8ikHO4K8/oZzLk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PBtyKtlCbTQ0sbVTvDOtSNP4hN1TsIG2BPP8uk4gQKEHp+PnKrTPOpAgLUhPOQKRl JI/Ebs50ufpKDCfLY0kvIyBjuKw2OXLuuFGyi1omJb3/IkCuKanF0iV7ylNAcqxe1N +6PrIjz18UFL8E/nG5AeDN3qZ4dz2DGdORQTc8M/zjUI+zKEuDIbjHVFtkQFkBZ3k+ 9pCFwuQR5IMogfVp+abjb8gFH4mdd0sQ9RRXaWHv+eTGF15RbR4Uay7Xm3K8vF2L4P VARnDNVSVFCVHf90KwRLL0SmBux2D1k1umKnb/6I/qRt+GM2pNyJbsqbCbCaXmUpqD u7GlqrAtdR9jw== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH RESEND v3 1/2] daemon: telegram: db: Allow the caller to reconnect Date: Mon, 31 Oct 2022 23:12:55 +0700 Message-Id: <20221031161256.199149-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221031161256.199149-1-ammarfaizi2@gnuweeb.org> References: <20221031161256.199149-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. v2: - Use conn.ping(reconnect=True) instead of creating a new MySQL connection object (comment from Muhammad Rizki). Signed-off-by: Ammar Faizi --- daemon/telegram/database/core.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/daemon/telegram/database/core.py b/daemon/telegram/database/core.py index c34d7a8..26f671b 100644 --- a/daemon/telegram/database/core.py +++ b/daemon/telegram/database/core.py @@ -14,6 +14,11 @@ class DB(DBMethods): self.conn.autocommit = True self.cur = self.conn.cursor(buffered=True) + def ping(self, reconnect=True, attempts=3, delay=3): + self.conn.ping(reconnect=reconnect, attempts=attempts, + delay=delay) + self.cur = self.conn.cursor(buffered=True) + def __del__(self): self.cur.close() -- Ammar Faizi