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=-1.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,NO_DNS_FOR_FROM, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from [192.168.1.2] (unknown [101.128.126.183]) by gnuweeb.org (Postfix) with ESMTPSA id A315E805A4; Mon, 31 Oct 2022 16:29:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1667233799; bh=P+1X81gB3bhm+P/wzPYlpIb67C77l7/mWJ9WAngRLpE=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=D1Gat//PkjFZobRIWTIWhCjvAqZX0MEzyZJSmsMbzY6FJBNjVh1w4WLuZou10PkU7 Ypv06jH7KPrUJQ6Qy7/bVJlZjYYodSmF/aqyttKkNNFJT7xSmwYQs/gtQVsMNcE0Wy 7GEcXfQVbWRXEdYXYA1TLCEI3/tlDkhZqMZjSW18609zk4C29avZjWUVfOUI4SauDF C8I0fzlf/16AyOhNWdCZPv0VWXeqldKH6w1ZgXTtWsvGoh1WmHNJ5q6L9Kw350bmwR cVGg4Ui4FUHBqHWlFaotkQ7YChs+1E2/PaHa8DVWG7eNf8jpGE2XYGcErOyvkyMSwB dBsTIYMnucNrA== Message-ID: Date: Mon, 31 Oct 2022 23:29:55 +0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: [PATCH RESEND v3 1/2] daemon: telegram: db: Allow the caller to reconnect Content-Language: en-US To: Ammar Faizi , GNU/Weeb Mailing List Cc: Alviro Iskandar Setiawan References: <20221031161256.199149-1-ammarfaizi2@gnuweeb.org> <20221031161256.199149-2-ammarfaizi2@gnuweeb.org> From: Muhammad Rizki In-Reply-To: <20221031161256.199149-2-ammarfaizi2@gnuweeb.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: On 31/10/22 23.12, Ammar Faizi wrote: > 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() Okay, this looks fine to me.