feat: 增强 upsert_video 方法,添加缓冲区长度和操作时间的调试信息

This commit is contained in:
晓丰 2025-05-21 01:04:52 +08:00
parent 1c4823d633
commit ed73a94a4f

18
DB.py
View File

@ -1,4 +1,6 @@
import json
from turtledemo.penrose import start
import redis
import pymysql
import time
@ -445,7 +447,7 @@ class DBSA:
push_record = staticmethod(lambda row: print("[退回Redis]", row["v_xid"]))
@classmethod
def upsert_video(cls, data: Dict):
def upsert_video(cls, data):
data = copy.deepcopy(data)
data.setdefault("a_id", 0)
data.setdefault("history_status", "")
@ -476,13 +478,17 @@ class DBSA:
with cls._lock:
cls._buf_op.append(op_row)
cls._buf_vid.append(vid_row)
if len(cls._buf_op) >= cls.FLUSH_EVERY_ROWS:
buf_len = len(cls._buf_vid)
print(f"DB缓冲 -> xid={data['v_xid']}, level={data['level']}, buffer={buf_len}")
if buf_len >= cls.FLUSH_EVERY_ROWS:
need_flush = True
flush_reason = "ROWS"
elif time.time() - cls._last_flush >= cls.FLUSH_INTERVAL:
need_flush = True
flush_reason = "TIME"
if need_flush:
print(f"DBSA 落 ({flush_reason}) ...")
cls.flush()
@classmethod
@ -519,11 +525,13 @@ class DBSA:
if not op_rows and not vid_rows:
return
start = time.time()
try:
cls._bulk_insert(op_rows)
cls._bulk_upsert(vid_rows)
elapsed = time.time() - start
print(f"[DBSA] 成 op={len(op_rows)} video={len(vid_rows)} time={elapsed:.3f}s")
except Exception as e:
print("[DBSA] 批写失败:", e)
print(f"[DBSA] flush FAIL: {e} op={len(op_rows)} video={len(vid_rows)}")
for row in vid_rows:
cls.push_record(row)