feat(shipping-notice): 完善订单解析、模板管理与库存 RPA 流程
- 优化订单文档解析、补充合并与坐标重建 - 完善订单模板校验、发布及前端管理界面 - 改进库存 RPA 下载回退与工作流处理 - 补充后端、前端及 RPA 自动化测试 - 更新 Shipping Notice 技术文档与四语言文案
This commit is contained in:
1 parent
c4a27324c8
commit
8f2f568f92
46 files changed
+1745
-265
No files matched your search
@@ -1,9 +1,9 @@
|
||||
"""
|
||||
scripts/rpa/download_artifact_fallback.py
|
||||
|
||||
用途:在浏览器实际已落盘但 Playwright 未返回 Download 对象时,按下载目录中的新文件构造兼容适配器。
|
||||
用途:在浏览器实际已落盘但 Playwright Download 句柄不可用时,按下载目录中的新文件构造兼容适配器。
|
||||
依赖:pathlib、shutil、time。
|
||||
逻辑:由 DSV 导出链路在 `expect_download` 超时后调用;不改变站点交互,只补文件系统兜底。
|
||||
逻辑:DSV/Crane 在 expect_download 超时后调用;库存 worker 在 Download.save_as 因浏览器关闭失败时同样调用。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -86,6 +86,9 @@ from dsv_report_export import NoInventoryDataError # noqa: E402
|
||||
import dsv_session # noqa: E402
|
||||
import rpa_heartbeat # noqa: E402
|
||||
import worker_disk_cleanup # noqa: E402
|
||||
from download_artifact_fallback import ( # noqa: E402
|
||||
wait_for_new_download_file as _wait_for_new_download_file,
|
||||
)
|
||||
|
||||
# 测试与 smoke 向后兼容 re-export
|
||||
_maybe_auto_login_dsv = dsv_session.maybe_auto_login_dsv
|
||||
@@ -693,11 +696,82 @@ def _validate_inventory_excel(path: Path) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _save_download(download: Any, drop_dir: Path, task_no: str, source_code: str) -> Path:
|
||||
def _download_dir_baseline_names(drop_dir: Path) -> set[str]:
|
||||
"""
|
||||
导出前拍摄下载目录已有文件名,供目录兜底识别“本次新文件”。
|
||||
核心逻辑:仅收集普通文件名;目录不可读时返回空集合。
|
||||
"""
|
||||
try:
|
||||
return {path.name for path in drop_dir.iterdir() if path.is_file()}
|
||||
except OSError:
|
||||
return set()
|
||||
|
||||
|
||||
def _is_playwright_target_closed_error(exc: BaseException) -> bool:
|
||||
"""
|
||||
判断是否为 Playwright 页面/上下文/浏览器已关闭导致的下载落盘失败。
|
||||
核心逻辑:匹配常见 TargetClosed 文案;用于决定是否启用 downloads_path 目录兜底。
|
||||
"""
|
||||
message = str(exc).lower()
|
||||
return "has been closed" in message or "target closed" in message or "targetclosed" in message
|
||||
|
||||
|
||||
def _persist_download_to_dest(
|
||||
download: Any,
|
||||
dest: Path,
|
||||
*,
|
||||
drop_dir: Path,
|
||||
baseline_names: set[str] | None = None,
|
||||
fallback_timeout_seconds: float = 60.0,
|
||||
) -> None:
|
||||
"""
|
||||
将 Playwright Download(或目录适配器)落到规范路径。
|
||||
核心逻辑:先 save_as;若浏览器已关闭则按 baseline 在 downloads_path 等待稳定新文件再搬迁。
|
||||
大报表场景下 expect_download 仅表示开始,收尾时 Chromium 偶发已死,目录才是完成条件。
|
||||
"""
|
||||
try:
|
||||
download.save_as(str(dest))
|
||||
return
|
||||
except Exception as exc:
|
||||
if not _is_playwright_target_closed_error(exc):
|
||||
raise
|
||||
print(
|
||||
"[inventory-artifact] stage=download_save status=fallback "
|
||||
f"reason=browser_closed path={dest.name!r}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
artifact = _wait_for_new_download_file(
|
||||
download_dir=drop_dir,
|
||||
baseline_names=set(baseline_names or ()),
|
||||
timeout_seconds=fallback_timeout_seconds,
|
||||
)
|
||||
if artifact is None:
|
||||
raise
|
||||
artifact.save_as(str(dest))
|
||||
|
||||
|
||||
def _save_download(
|
||||
download: Any,
|
||||
drop_dir: Path,
|
||||
task_no: str,
|
||||
source_code: str,
|
||||
*,
|
||||
baseline_names: set[str] | None = None,
|
||||
fallback_timeout_seconds: float = 60.0,
|
||||
) -> Path:
|
||||
"""
|
||||
单票库存下载落盘到 drop_dir,必要时在浏览器关闭后走目录兜底。
|
||||
"""
|
||||
suggested = str(download.suggested_filename or "")
|
||||
suffix = Path(suggested).suffix or ".xlsx"
|
||||
dest = drop_dir / _download_name(task_no, source_code, suffix=suffix)
|
||||
download.save_as(str(dest))
|
||||
_persist_download_to_dest(
|
||||
download,
|
||||
dest,
|
||||
drop_dir=drop_dir,
|
||||
baseline_names=baseline_names,
|
||||
fallback_timeout_seconds=fallback_timeout_seconds,
|
||||
)
|
||||
if suffix.lower() in {".xlsx", ".xlsm", ".xltx", ".xltm"}:
|
||||
_validate_inventory_excel(dest)
|
||||
return dest
|
||||
@@ -711,14 +785,31 @@ def _batch_download_name(run_no: str, source_code: str, warehouse_id: str, suffi
|
||||
return f"inventory_{safe_run}_{safe_source}_{safe_wh}_{stamp}{suffix}"
|
||||
|
||||
|
||||
def _save_batch_download(download: Any, drop_dir: Path, run_no: str, source: dict[str, Any]) -> Path:
|
||||
def _save_batch_download(
|
||||
download: Any,
|
||||
drop_dir: Path,
|
||||
run_no: str,
|
||||
source: dict[str, Any],
|
||||
*,
|
||||
baseline_names: set[str] | None = None,
|
||||
fallback_timeout_seconds: float = 60.0,
|
||||
) -> Path:
|
||||
"""
|
||||
批量库存下载落盘到 drop_dir;Playwright save_as 因浏览器关闭失败时按目录新文件兜底。
|
||||
"""
|
||||
suggested = str(download.suggested_filename or "")
|
||||
suffix = Path(suggested).suffix or ".xlsx"
|
||||
source_code = str(source.get("source_code") or "UNKNOWN")
|
||||
warehouse_id = str(source.get("warehouse_id") or "WH")
|
||||
dest = drop_dir / _batch_download_name(run_no, source_code, warehouse_id, suffix=suffix)
|
||||
save_started_at = time.monotonic()
|
||||
download.save_as(str(dest))
|
||||
_persist_download_to_dest(
|
||||
download,
|
||||
dest,
|
||||
drop_dir=drop_dir,
|
||||
baseline_names=baseline_names,
|
||||
fallback_timeout_seconds=fallback_timeout_seconds,
|
||||
)
|
||||
print(
|
||||
"[inventory-artifact] stage=download_save status=end "
|
||||
f"elapsedMs={int((time.monotonic() - save_started_at) * 1000)} path={dest.name!r}"
|
||||
@@ -1218,13 +1309,15 @@ def _download_inventory_with_browser(trigger: dict[str, Any], drop_dir: Path) ->
|
||||
profile_code=_source_rpa_profile_code(source),
|
||||
)
|
||||
with _profile_credentials(source):
|
||||
# [STEP] 导出前拍摄目录基线:save_as 若因浏览器关闭失败,可识别 downloads_path 新文件。
|
||||
baseline_names = _download_dir_baseline_names(drop_dir)
|
||||
if connector_type == "dsv360":
|
||||
download = _download_dsv360_inventory(page, trigger, source)
|
||||
elif connector_type == "crane_footprint":
|
||||
download = _download_crane_inventory(page, trigger, source)
|
||||
else:
|
||||
raise RuntimeError(f"unsupported inventory connector_type: {connector_type}")
|
||||
return _save_download(download, drop_dir, task_no, source_code)
|
||||
return _save_download(download, drop_dir, task_no, source_code, baseline_names=baseline_names)
|
||||
except Exception:
|
||||
failed = True
|
||||
raise
|
||||
@@ -1583,8 +1676,16 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
|
||||
_ensure_crane_session(page, entry_url)
|
||||
else:
|
||||
_reset_crane_export_entry(page, entry_url)
|
||||
# [STEP] 导出点击前拍摄目录基线,供 save_as 浏览器关闭时的目录兜底。
|
||||
baseline_names = _download_dir_baseline_names(drop_dir)
|
||||
download = export_fn(page, body, source, skip_navigation=skip_navigation) if site == "dsv360" else export_fn(page, body, source)
|
||||
out_file = _save_batch_download(download, drop_dir, run_no, source)
|
||||
out_file = _save_batch_download(
|
||||
download,
|
||||
drop_dir,
|
||||
run_no,
|
||||
source,
|
||||
baseline_names=baseline_names,
|
||||
)
|
||||
record_result({"source_code": code, "warehouse_id": wh, "status": "success", "success": True, "file_path": str(out_file), "task_no": task_no or source.get("task_no") or None, "validation_run_no": source.get("validation_run_no"), **{k: source.get(k) for k in ("attempt_no", "source_run_no", "lease_token", "expected_warehouse_id")}})
|
||||
print(f"[inventory-batch] {site} export done {code}/{wh} -> {out_file.name}")
|
||||
except NoInventoryDataError as exc:
|
||||
|
||||
Reference in new issue
Block a user