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 BA5EE813FF; Sun, 30 Oct 2022 05:49:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667108982; bh=D9zJvDP0ogMlPzJgwB6bu2Pg+eTmOu8tS8UTSKYkmxo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jHk6svRP4wi2QwU/0cOD+uD/XBr7UDrnNtFHKg1u/te/W5MsOpbMfeCuUEU1s1YAP bb/pb1DXjcPAh5Mn8JEMXLfUegMB/eR4GFMzvEhbWmnpjsGwbN/tK1aru0C8DpKIrb OZV2zAm51p35v6wI2gn6Fiyat0TXVQe0XKBAJ4AY38jo/PuxkIi01HpF4wRo0MDWhX omFl9jV5q/K1cK2Qs4x38TX+cDVmMseQ4bIqL03ZB4hTzbN9aIOQTbyDEE+x/Dw1/m kFJIIC+6pHeF3TYkeMMpWTZPB2OjeyKQqXXfimZpwzj+zzOua8qKSgfxoXzLXmVScq HFRAVYpCA17zg== From: Ammar Faizi To: GNU/Weeb Mailing List Cc: Ammar Faizi , Muhammad Rizki , Alviro Iskandar Setiawan Subject: [PATCH v2 1/2] daemon: telegram: db: Allow the caller to reconnect Date: Sun, 30 Oct 2022 12:49:29 +0700 Message-Id: <20221030054930.573374-2-ammarfaizi2@gnuweeb.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221030054930.573374-1-ammarfaizi2@gnuweeb.org> References: <20221030054930.573374-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 (comment from Muhammad Rizki): - Use conn.ping(reconnect=True) instead of creating a new MySQL connection object. 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