fix(backend): 启动时清空上次调试图 - 重启即放弃旧图

配合运行时滚动清理(保留最近N个)形成两层:
- 启动 purge_debug_dirs_on_startup(): 清空三目录上次的所有图
  (debug_captcha_direct/auto_captured/ppocr_live_crops),保留_warmup.png
- 运行时 prune_dir(): 连挂几天不重启时滚动保留最近N个

实测本地 logs/ppocr_live_crops 已积累1470个文件,启动清理删1631个归零。
重启即放弃上次调试图(留着占盘没用),连挂几天的用户靠滚动兜底。
This commit is contained in:
OLmatter
2026-06-26 01:23:06 +08:00
parent 0213c2ff90
commit 55ea973095
+32
View File
@@ -59,6 +59,14 @@ PORT = BACKEND_CONFIG.port
# 这些目录是调试用途,对识别功能无影响,老图定期清掉即可。
DEBUG_KEEP_FILES = 60 # debug_captcha_direct 保留最近 60 张原图
AUTO_CAPTURE_KEEP_FILES = 120 # auto_captured 保留最近 120 张截图
# 运行时同样会滚动的目录:启动时直接清空(重启即放弃上次的调试图,留着没用)。
# 注意 _warmup.png 是预热用的兜底图,需保留。
STARTUP_PURGE_DIRS = [
ROOT / "dataset" / "debug_captcha_direct",
ROOT / "dataset" / "auto_captured",
ROOT / "logs" / "ppocr_live_crops",
]
STARTUP_PURGE_KEEP = {"_warmup.png"}
def prune_dir(directory: Path, keep: int, suffix: str = ".png") -> None:
"""保留目录里最近修改的 keep 个文件,删除更老的。静默失败。"""
@@ -81,6 +89,29 @@ def prune_dir(directory: Path, keep: int, suffix: str = ".png") -> None:
except Exception:
pass
def purge_debug_dirs_on_startup() -> None:
"""启动时清空调试目录里的旧图(重启即放弃上次的调试图,留着占盘没用)。
保留 STARTUP_PURGE_KEEP 里的文件(如 _warmup.png)。静默失败。"""
total = 0
for d in STARTUP_PURGE_DIRS:
try:
if not d.exists():
continue
for p in d.iterdir():
if not p.is_file():
continue
if p.name in STARTUP_PURGE_KEEP:
continue
try:
p.unlink()
total += 1
except Exception:
pass
except Exception:
pass
if total:
print(f"[启动] 清理上次调试图 {total} 个", flush=True)
class AppState:
def __init__(self):
@@ -589,6 +620,7 @@ class CaptchaHandler(BaseHTTPRequestHandler):
def run_gui():
if tk is None or ttk is None:
raise RuntimeError("Tk is unavailable; install Tk support or use --headless")
purge_debug_dirs_on_startup()
root = tk.Tk()
root.title("Captcha Server v2 - Auto Rush Mode")
root.geometry("620x380")