From f2f5e0e3d5a122952e31c0bf397582dcf57f86b4 Mon Sep 17 00:00:00 2001 From: liujianjiang Date: Mon, 14 Jul 2025 10:42:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=87=BA=E7=8E=B0uuid?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E6=97=B6=EF=BC=8C=E8=BF=9B=E8=A1=8C=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index ebf19a7..91473f8 100644 --- a/app.py +++ b/app.py @@ -60,12 +60,35 @@ def add_h5_user(): account_name = request.form['account_name'] countries = request.form['countries'] link_url = request.form['link_url'] + max_retries = 3 # 最大重试次数 + retry_count = 0 + success = False + while not success and retry_count < max_retries: + try: + account_id = uuid.uuid4().hex + sql_str = """INSERT INTO account_h5_info + (account_id, keep_rate, task_total, + account_name, countries, link_url) + VALUES (%s, %s, %s, %s, %s, %s)""" + db.execute_single(sql_str, (account_id, keep_rate, + task_total, account_name, + countries, link_url)) + success = True + return redirect(url_for('get_h5_account_info')) + except Exception as e: + if "Duplicate entry" in str(e) and "account_id" in str(e): + retry_count += 1 + app.logger.warning(f"UUID冲突,正在重试({retry_count}/{max_retries})") + continue + raise # 如果不是UUID冲突错误,直接抛出异常 + + if not success: + raise Exception("UUID生成冲突,已达到最大重试次数") - sql_str = "insert into account_h5_info(account_id,keep_rate,task_total,account_name,countries,link_url) values(%s,%s,%s,%s,%s,%s)" - db.execute_single(sql_str, (uuid.uuid1(), keep_rate, task_total, account_name, countries, link_url)) - return redirect(url_for('get_h5_account_info')) except Exception as e: - app.logger.info("{}".format(str(e))) + app.logger.error(f"添加H5用户失败: {str(e)}") + # 这里可以添加错误处理逻辑,比如返回错误页面或提示信息 + return render_template('add_h5_user.html')