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')