feat: 自动隐藏被覆盖的打包历史
This commit is contained in:
@@ -6,7 +6,7 @@ import plistlib
|
||||
import shutil
|
||||
import subprocess
|
||||
import zipfile
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
@@ -214,6 +214,7 @@ def _cleanup_old_builds(db):
|
||||
|
||||
base = BUILD_BASE_DIR
|
||||
if not base.exists():
|
||||
_cleanup_superseded_tasks(db)
|
||||
return
|
||||
for d in base.iterdir():
|
||||
if d.is_dir() and d.name.startswith("build_readoor_"):
|
||||
@@ -222,6 +223,50 @@ def _cleanup_old_builds(db):
|
||||
shutil.rmtree(d)
|
||||
except Exception:
|
||||
pass
|
||||
_cleanup_superseded_tasks(db)
|
||||
|
||||
|
||||
def _cleanup_superseded_tasks(db):
|
||||
"""7 天后删除已被新 OSS 包覆盖的历史记录及仅本地保留的文件。"""
|
||||
from ..models import Task
|
||||
cutoff = datetime.utcnow() - timedelta(days=7)
|
||||
tasks = db.query(Task).filter(
|
||||
Task.superseded_at.isnot(None), Task.superseded_at < cutoff,
|
||||
).all()
|
||||
for task in tasks:
|
||||
for path_value in (task.build_dir, task.dsym_path, task.obfuscation_maps_path):
|
||||
if not path_value:
|
||||
continue
|
||||
try:
|
||||
path = Path(path_value)
|
||||
if path.is_dir():
|
||||
shutil.rmtree(path, ignore_errors=True)
|
||||
elif path.exists():
|
||||
path.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
log_path = Path(__file__).parent.parent / "logs" / f"{task.id}.log"
|
||||
try:
|
||||
log_path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
db.delete(task)
|
||||
if tasks:
|
||||
db.commit()
|
||||
|
||||
|
||||
def _mark_superseded_tasks(db, latest_task):
|
||||
"""标记同一 OSS 对象的旧成功记录;OSS 文件由最新任务覆盖。"""
|
||||
if not latest_task.oss_url:
|
||||
return
|
||||
from ..models import Task
|
||||
db.query(Task).filter(
|
||||
Task.id != latest_task.id,
|
||||
Task.status == "completed",
|
||||
Task.oss_url == latest_task.oss_url,
|
||||
Task.superseded_at.is_(None),
|
||||
).update({Task.superseded_at: datetime.utcnow()}, synchronize_session=False)
|
||||
db.commit()
|
||||
|
||||
|
||||
def _cleanup_old_logs():
|
||||
@@ -439,6 +484,7 @@ async def run_build_task(task_id: str):
|
||||
await asyncio.to_thread(_db_update, db, task,
|
||||
status="completed", completed_at=datetime.utcnow(),
|
||||
current_step=None)
|
||||
await asyncio.to_thread(_mark_superseded_tasks, db, task)
|
||||
|
||||
await log_streamer.emit_step(task_id, "打包完成")
|
||||
await log_streamer.emit(task_id, f"IPA: {ipa_path}")
|
||||
|
||||
Reference in New Issue
Block a user