feat(rpa): 完善库存与吊车报表自动化运行配置
This commit is contained in:
1 parent
70dd0051b8
commit
7a9fac2f8a
8 files changed
+364
-55
No files matched your search
@@ -2,18 +2,42 @@
|
||||
scripts/rpa/crane_report_export.py
|
||||
|
||||
用途:Crane On Hand Details Query 异步报表导出(Run Report → Schedule Report → Scheduled Reports 下载)。
|
||||
依赖:Playwright Page;由 freight_rpa_worker._export_crane_inventory_source 委托调用。
|
||||
逻辑:必须先 Run Report 产出查询结果,再 Schedule 异步导出;轮询仅在 scheduled-reports 路由进行。
|
||||
依赖:Playwright Page、download_artifact_fallback;由 inventory_rpa_worker._export_crane_inventory_source 委托调用。
|
||||
逻辑:必须先 Run Report 产出查询结果,再 Schedule 异步导出;轮询仅在 scheduled-reports 路由进行;
|
||||
expect_download 超时时按下载目录新文件做短窗口兜底(与 DSV 同模块,窗口更短)。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sys as _sys
|
||||
from pathlib import Path as _Path
|
||||
|
||||
_RPA_ROOT = next(
|
||||
(
|
||||
_c
|
||||
for _c in _Path(__file__).resolve().parents
|
||||
if (_c / "connectors").is_dir()
|
||||
and (_c / "common").is_dir()
|
||||
and (_c / "inventory").is_dir()
|
||||
and (_c / "receipt").is_dir()
|
||||
),
|
||||
None,
|
||||
)
|
||||
if _RPA_ROOT is not None:
|
||||
for _rel in ("", "common", "connectors", "connectors/dsv", "connectors/crane", "inventory", "receipt"):
|
||||
_dir = str(_RPA_ROOT) if not _rel else str(_RPA_ROOT / _rel)
|
||||
if _dir not in _sys.path:
|
||||
_sys.path.insert(0, _dir)
|
||||
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from download_artifact_fallback import wait_for_new_download_file as _wait_for_new_download_file
|
||||
|
||||
# Schedule Report Description 默认取 UUID 前 12 位十六进制(无连字符)。
|
||||
# 48 bit ≈ 2.8e14 种组合,单租户 RPA 导出场景碰撞可忽略;页面示例多为 6~8 位人工填写。
|
||||
_CRANE_SCHEDULE_DESCRIPTION_HEX_LEN_DEFAULT = 12
|
||||
@@ -71,6 +95,8 @@ _CRANE_SCHEDULED_COL_DESCRIPTION = 2
|
||||
_CRANE_SCHEDULED_COL_DOWNLOAD = 3
|
||||
_CRANE_SCHEDULED_REPORT_NAME_DEFAULT = "OnHandDetails.xlsx"
|
||||
_CRANE_SCHEDULED_SCAN_MAX_ROWS_DEFAULT = 20
|
||||
# Playwright download 事件偶发丢失时,目录兜底窗口(人工下载约 2s;不必拉到 DSV 的 60s)。
|
||||
_CRANE_DOWNLOAD_DIR_FALLBACK_SECONDS = 15.0
|
||||
_CRANE_SESSION_MODULE: Any | None = None
|
||||
|
||||
|
||||
@@ -930,10 +956,24 @@ def _wait_and_click_scheduled_download(
|
||||
description: str = "",
|
||||
expected_report_name: str = _CRANE_SCHEDULED_REPORT_NAME_DEFAULT,
|
||||
entry_url: str = "",
|
||||
download_dir: str = "",
|
||||
) -> Any:
|
||||
"""
|
||||
轮询 Scheduled Reports 直至目标行 Download 列由 Submitted 变为 Download 并点击。
|
||||
核心逻辑:每 2s 读一次状态;每 10s goto 刷新列表;表格可见时不重登,避免闪回登录页。
|
||||
核心逻辑:每 2s 探测一次状态;每 10s goto 刷新列表;表格可见时不重登,避免闪回登录页;
|
||||
expect_download 超时且已配置 download_dir 时,短窗口扫描目录新文件兜底。
|
||||
|
||||
Args:
|
||||
page: Playwright 页面。
|
||||
env: 环境变量读取函数。
|
||||
browser_timeout_ms: 浏览器操作超时(亦用于 expect_download)。
|
||||
description: Schedule 时写入的 Description,用于定位行。
|
||||
expected_report_name: 期望的 Name 列文件名。
|
||||
entry_url: Crane 入口 URL,用于会话恢复。
|
||||
download_dir: worker 注入的浏览器 downloads_path;空则不做目录兜底。
|
||||
|
||||
Returns:
|
||||
Playwright Download 或 FilesystemDownloadArtifact。
|
||||
"""
|
||||
entry = entry_url or _default_crane_entry_url(None)
|
||||
wait_ms = int(env("SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORT_WAIT_MS", "300000") or "300000")
|
||||
@@ -950,6 +990,7 @@ def _wait_and_click_scheduled_download(
|
||||
last_list_refresh = time.monotonic()
|
||||
poll_index = 0
|
||||
last_status = "not_found"
|
||||
drop_dir = str(download_dir or "").strip()
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
poll_index += 1
|
||||
@@ -1005,15 +1046,44 @@ def _wait_and_click_scheduled_download(
|
||||
f"description={description!r} status={last_status!r} after~{elapsed}s polls={poll_index}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
with page.expect_download(timeout=click_timeout) as download_info:
|
||||
if not _click_locator(locator, browser_timeout_ms=click_timeout):
|
||||
raise RuntimeError(
|
||||
f"Crane scheduled report Download link click failed for description={description!r}"
|
||||
)
|
||||
print(
|
||||
f"[crane-export] download started name={expected_report_name!r} description={description!r}"
|
||||
)
|
||||
return download_info.value
|
||||
baseline_names: set[str] = set()
|
||||
if drop_dir:
|
||||
try:
|
||||
baseline_names = {path.name for path in Path(drop_dir).iterdir() if path.is_file()}
|
||||
except OSError:
|
||||
baseline_names = set()
|
||||
try:
|
||||
# [STEP] 主路径:Playwright download 事件
|
||||
with page.expect_download(timeout=click_timeout) as download_info:
|
||||
if not _click_locator(locator, browser_timeout_ms=click_timeout):
|
||||
raise RuntimeError(
|
||||
f"Crane scheduled report Download link click failed for description={description!r}"
|
||||
)
|
||||
print(
|
||||
f"[crane-export] download started name={expected_report_name!r} description={description!r}"
|
||||
)
|
||||
return download_info.value
|
||||
except RuntimeError:
|
||||
# 点击失败不走目录兜底,避免空等
|
||||
raise
|
||||
except Exception:
|
||||
# [STEP] 事件丢失/晚到:短窗口扫 downloads_path(与 DSV 同模块)
|
||||
if not drop_dir:
|
||||
raise
|
||||
fallback = _wait_for_new_download_file(
|
||||
download_dir=drop_dir,
|
||||
baseline_names=baseline_names,
|
||||
timeout_seconds=_CRANE_DOWNLOAD_DIR_FALLBACK_SECONDS,
|
||||
)
|
||||
if fallback is None:
|
||||
raise
|
||||
print(
|
||||
"[crane-download-fallback] "
|
||||
f"using downloaded file from directory because Playwright download event timed out: "
|
||||
f"{fallback.suggested_filename}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return fallback
|
||||
|
||||
loop_elapsed_ms = int((time.monotonic() - loop_started) * 1000)
|
||||
remaining = int(deadline - time.monotonic())
|
||||
@@ -1091,11 +1161,12 @@ def export_crane_inventory_report(
|
||||
) -> Any:
|
||||
"""
|
||||
导出 Crane 库存报表。
|
||||
核心逻辑:Run Report(等待结果)→ Schedule Report(填 Description)→ Scheduled Reports 按 Description 下载。
|
||||
核心逻辑:Run Report(等待结果)→ Schedule Report(填 Description)→ Scheduled Reports 按 Description 下载;
|
||||
下载时 expect_download 失败则按 source.__download_dir 短窗口目录兜底。
|
||||
|
||||
Args:
|
||||
page: Playwright 页面。
|
||||
source: rpa_inventory_source 配置。
|
||||
source: rpa_inventory_source 配置(可含 worker 注入的 __download_dir)。
|
||||
env: 环境变量读取函数。
|
||||
browser_timeout_ms: 浏览器操作超时。
|
||||
select_or_fill: worker 提供的下拉/输入填充函数。
|
||||
@@ -1103,7 +1174,7 @@ def export_crane_inventory_report(
|
||||
trigger: inventory_export / batch trigger,用于生成唯一 Description。
|
||||
|
||||
Returns:
|
||||
Playwright Download 对象。
|
||||
Playwright Download 或 FilesystemDownloadArtifact。
|
||||
"""
|
||||
download_mode = _resolve_download_mode(source, env=env)
|
||||
entry_url = _default_crane_entry_url(source)
|
||||
@@ -1150,7 +1221,6 @@ def export_crane_inventory_report(
|
||||
browser_timeout_ms=browser_timeout_ms,
|
||||
entry_url=entry_url,
|
||||
)
|
||||
wait_ms = int(env("SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORT_WAIT_MS", "300000") or "300000")
|
||||
return _wait_and_click_scheduled_download(
|
||||
page,
|
||||
env=env,
|
||||
@@ -1158,6 +1228,7 @@ def export_crane_inventory_report(
|
||||
description=schedule_description,
|
||||
expected_report_name=scheduled_report_name,
|
||||
entry_url=entry_url,
|
||||
download_dir=str(source.get("__download_dir") or ""),
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user