修改出现uuid重复时,进行重新写入

This commit is contained in:
liujianjiang 2025-07-14 10:42:43 +08:00
parent bd24b300f9
commit f2f5e0e3d5
1 changed files with 27 additions and 4 deletions

31
app.py
View File

@ -60,12 +60,35 @@ def add_h5_user():
account_name = request.form['account_name'] account_name = request.form['account_name']
countries = request.form['countries'] countries = request.form['countries']
link_url = request.form['link_url'] link_url = request.form['link_url']
max_retries = 3 # 最大重试次数
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)" retry_count = 0
db.execute_single(sql_str, (uuid.uuid1(), keep_rate, task_total, account_name, countries, link_url)) 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')) return redirect(url_for('get_h5_account_info'))
except Exception as e: except Exception as e:
app.logger.info("<UNK>{}".format(str(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生成冲突已达到最大重试次数")
except Exception as e:
app.logger.error(f"添加H5用户失败: {str(e)}")
# 这里可以添加错误处理逻辑,比如返回错误页面或提示信息
return render_template('add_h5_user.html') return render_template('add_h5_user.html')