feat(rpa): 增加 Worker 本机磁盘保留清理

按保留天数定期清理归档 trigger/drop、日志备份与孤儿 run_* Profile,
并支持运行中按大小轮转日志,避免常驻 Worker 撑爆 RPA 主机盘符。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hugoandCursor committed 2026-09-07 15:04:01 +08:00
1 parent a6edc4fe61
commit f8368acb6f
6 files changed
+762 -5

No files matched your search

+149
View File
@@ -623,6 +623,155 @@ def test_rotate_log_file_deletes_current_when_backup_count_zero(monkeypatch, tmp
assert not log_path.exists()
def _load_worker_disk_cleanup():
"""加载 scripts/rpa/common/worker_disk_cleanup.py 供单测直接断言。"""
module_path = _repo_root() / "scripts" / "rpa" / "common" / "worker_disk_cleanup.py"
spec = importlib.util.spec_from_file_location("worker_disk_cleanup_under_test", module_path)
assert spec is not None
assert spec.loader is not None
module = importlib.util.module_from_spec(spec)
# dataclass 在装饰阶段会查 sys.modules;动态加载须先登记。
sys.modules[spec.name] = module
spec.loader.exec_module(module)
return module
def test_worker_disk_cleanup_deletes_expired_archives_and_keeps_active(monkeypatch, tmp_path):
"""
超过保留期的归档应删除;进行中的 trigger、会话 Profile、cleanup_queue 必须保留。
断言意图:验证磁盘清理只碰“归档/临时物”,不误删业务进行中文件。
"""
cleanup = _load_worker_disk_cleanup()
now = time.time()
old = now - (10 * 86400)
recent = now - 3600
inv_trigger = tmp_path / "inventory_triggers"
upload_trigger = tmp_path / "upload_triggers"
drop = tmp_path / "inventory_drop"
queue = tmp_path / "upload_queue"
profiles = tmp_path / "browser_profile"
logs = tmp_path / "logs"
for path in (
inv_trigger / "processed",
inv_trigger / "failed",
upload_trigger / "processed",
drop / "processed",
drop / ".hxops_quarantine",
drop / ".cleanup_queue",
drop / ".cleanup_failed",
queue,
profiles,
logs,
):
path.mkdir(parents=True)
active_trigger = inv_trigger / "inventory_export_SN-ACTIVE.json"
active_trigger.write_text("{}", encoding="utf-8")
os.utime(active_trigger, (old, old))
stale_processed = inv_trigger / "processed" / "inventory_export_SN-OLD.json"
stale_processed.write_text("{}", encoding="utf-8")
os.utime(stale_processed, (old, old))
fresh_processed = inv_trigger / "processed" / "inventory_export_SN-NEW.json"
fresh_processed.write_text("{}", encoding="utf-8")
os.utime(fresh_processed, (recent, recent))
stale_drop = drop / "processed" / "inventory_OLD.xlsx"
stale_drop.write_bytes(b"xlsx")
os.utime(stale_drop, (old, old))
stale_root_drop = drop / "inventory_STALE.xlsx"
stale_root_drop.write_bytes(b"xlsx")
os.utime(stale_root_drop, (old, old))
cleanup_job = drop / ".cleanup_queue" / "move_abc.json"
cleanup_job.write_text("{}", encoding="utf-8")
os.utime(cleanup_job, (old, old))
stale_queue = queue / "upload_OLD.xlsx"
stale_queue.write_bytes(b"xlsx")
os.utime(stale_queue, (old, old))
log_path = logs / "worker.log"
log_path.write_text("current", encoding="utf-8")
stale_backup = Path(f"{log_path}.1")
stale_backup.write_text("old-backup", encoding="utf-8")
os.utime(stale_backup, (old, old))
session_profile = profiles / "dsv360__DEFAULT_session"
session_profile.mkdir()
(session_profile / "Cookies").write_text("keep", encoding="utf-8")
os.utime(session_profile, (old, old))
orphan_run = profiles / "run_orphan"
orphan_run.mkdir()
(orphan_run / "Cookies").write_text("drop", encoding="utf-8")
os.utime(orphan_run, (now - (48 * 3600), now - (48 * 3600)))
monkeypatch.setenv("SHIPPING_NOTICE_RPA_WORKER_DISK_RETENTION_DAYS", "7")
monkeypatch.setenv("SHIPPING_NOTICE_RPA_WORKER_ORPHAN_PROFILE_MAX_AGE_HOURS", "24")
stats = cleanup.run_worker_disk_cleanup(
inventory_trigger_dir=inv_trigger,
upload_trigger_dir=upload_trigger,
drop_dir=drop,
upload_queue_dir=queue,
browser_profile_dir=profiles,
log_file=log_path,
now_unix=now,
)
assert not stale_processed.exists()
assert fresh_processed.exists()
assert active_trigger.exists()
assert not stale_drop.exists()
assert not stale_root_drop.exists()
assert cleanup_job.exists()
assert not stale_queue.exists()
assert log_path.exists()
assert not stale_backup.exists()
assert session_profile.exists()
assert not orphan_run.exists()
assert stats.deleted_files >= 4
assert stats.deleted_dirs >= 1
def test_worker_disk_cleanup_skips_when_retention_disabled(monkeypatch, tmp_path):
"""DISK_RETENTION_DAYS<=0 时不删归档;孤儿 Profile 仍可按独立配置清理。"""
cleanup = _load_worker_disk_cleanup()
now = time.time()
old = now - (10 * 86400)
inv_trigger = tmp_path / "inventory_triggers"
processed = inv_trigger / "processed"
profiles = tmp_path / "browser_profile"
processed.mkdir(parents=True)
profiles.mkdir(parents=True)
stale_processed = processed / "old.json"
stale_processed.write_text("{}", encoding="utf-8")
os.utime(stale_processed, (old, old))
orphan_run = profiles / "run_orphan"
orphan_run.mkdir()
os.utime(orphan_run, (now - (48 * 3600), now - (48 * 3600)))
monkeypatch.setenv("SHIPPING_NOTICE_RPA_WORKER_DISK_RETENTION_DAYS", "0")
monkeypatch.setenv("SHIPPING_NOTICE_RPA_WORKER_ORPHAN_PROFILE_MAX_AGE_HOURS", "24")
cleanup.run_worker_disk_cleanup(
inventory_trigger_dir=inv_trigger,
browser_profile_dir=profiles,
now_unix=now,
)
assert stale_processed.exists()
assert not orphan_run.exists()
def test_open_browser_uses_shipping_notice_profile_fallback(monkeypatch):
"""未配置 profile 根目录时,应回退到 shipping_notice/freight_rpa/browser_profile。"""
worker = _load_freight_rpa_worker()