feat: 重构数据库操作以改进错误处理和重试逻辑
This commit is contained in:
parent
dd90cc3c91
commit
bbb2cf1823
35
DB.py
35
DB.py
@ -23,7 +23,7 @@ class DBVidcon:
|
||||
|
||||
def __init__(self):
|
||||
self.list_key = "video_kw_queue"
|
||||
self.record_list_key = "record_kw_queue"
|
||||
self.record_list_key = "error_save_queue"
|
||||
self.urgent_list_key = "video_urgent_queue"
|
||||
self.conn = pymysql.connect(**self._MYSQL_CONF)
|
||||
self.cursor = self.conn.cursor()
|
||||
@ -110,7 +110,6 @@ class DBVidcon:
|
||||
self.redis.lpush(self.record_list_key, *raws)
|
||||
|
||||
def rollback_urgent(self, raws):
|
||||
"""写库失败或加急任务处理失败时,把原始 JSON 退回 urgent 列表"""
|
||||
if isinstance(raws, str):
|
||||
raws = [raws]
|
||||
try:
|
||||
@ -151,7 +150,12 @@ class DBVidcon:
|
||||
data.setdefault("is_repeat", 3)
|
||||
data["sort"] = data.get("index", 0)
|
||||
|
||||
max_retries = 1 # 除了第一次外,再重试一次
|
||||
attempt = 0
|
||||
|
||||
while True:
|
||||
try:
|
||||
# 1) 先读 is_repeat
|
||||
select_repeat = """
|
||||
SELECT is_repeat
|
||||
FROM sh_dm_video_v2
|
||||
@ -164,7 +168,7 @@ class DBVidcon:
|
||||
if row:
|
||||
data['is_repeat'] = row[0]
|
||||
|
||||
# 2. 插入到 op 表
|
||||
# 2) 插入到 op 表
|
||||
sql_op = """
|
||||
INSERT INTO sh_dm_video_op_v2 (
|
||||
v_id, v_xid, a_id, level, name_title,
|
||||
@ -178,7 +182,7 @@ class DBVidcon:
|
||||
"""
|
||||
self.cursor.execute(sql_op, data)
|
||||
|
||||
# 3. 删除旧表中的那行
|
||||
# 3) 删除旧表行
|
||||
sql_del = """
|
||||
DELETE FROM sh_dm_video_v2
|
||||
WHERE rn = %(rn)s
|
||||
@ -186,7 +190,7 @@ class DBVidcon:
|
||||
"""
|
||||
self.cursor.execute(sql_del, data)
|
||||
|
||||
# 4. 带上 is_repeat 再插入新数据
|
||||
# 4) 插入新表
|
||||
sql_ins = """
|
||||
INSERT INTO sh_dm_video_v2 (
|
||||
v_id, v_xid, rn, v_name, title, link,
|
||||
@ -207,11 +211,26 @@ class DBVidcon:
|
||||
)
|
||||
"""
|
||||
self.cursor.execute(sql_ins, data)
|
||||
break # 成功跳出重试循环
|
||||
|
||||
except Exception as e:
|
||||
# 打印错误并回滚
|
||||
# 回滚这次未提交的改动
|
||||
self.conn.rollback()
|
||||
print("[数据库写入异常]", str(e))
|
||||
print("[出错数据]", data)
|
||||
raise
|
||||
print("[出错数据]:", data)
|
||||
|
||||
if attempt < max_retries:
|
||||
attempt += 1
|
||||
print(f"第 {attempt + 1} 次重试…")
|
||||
continue
|
||||
else:
|
||||
# 重试过后依然失败,推入 Redis 备用
|
||||
print("重试失败,将数据写入 Redis 以便后续处理")
|
||||
self.push_record(data)
|
||||
print("[交由Redis处理]")
|
||||
break
|
||||
|
||||
|
||||
|
||||
def flush(self):
|
||||
"""批量执行完后手动提交。"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user