feat(rpa): 更新RPA配置与目录结构,增强环境安全性

- 在.env.prod中添加RPA Profile Fernet主密钥的注释和示例值,强调安全性和凭据轮换的重要性。
- 在.env.shared中移除RPA凭据,确保敏感信息不被提交。
- 更新RPA相关目录结构,统一为data/freight_rpa/{inventory,receipt,common},确保与后端设置一致。
- 修改相关文档和脚本以反映新的目录布局,确保一致性和可维护性。
This commit is contained in:
hugo committed 2026-09-21 08:59:46 +08:00
1 parent e08f314047
commit ab57ab3b1f
10 files changed
+322 -244

No files matched your search

+20 -13
View File
@@ -5,7 +5,8 @@ scripts/freight_rpa/connectors/dsv/dsv_auth.py
依赖:rpa_common、dsv_debug;登录主编排见 dsv_auth_login_flow。
逻辑:由 dsv_session / dsv_report_export 在 ensure session 时调用 maybe_auto_login_dsv;
方法页点击须兼容英文 “Choose a sign in method” 标题+副标题行(2026-09-16);
英文页可经 #kc_locale 尝试切「中文简体」(失败不阻断)。
英文页可经 #lanNavSel(SIMS 页脚语言按钮,2026-09-20 改版后的实际开关)或旧版
#kc_locale 尝试切「中文简体」(失败不阻断)。
"""
from __future__ import annotations
@@ -118,8 +119,9 @@ def _maybe_switch_dsv_locale_to_zh_cn(page: Any) -> bool:
"""
若登录页语言为 English,尝试切换为「中文简体」。
核心逻辑:读 `#lanNavSel`;已是中文则跳过;否则点 `#kc_locale` 打开下拉,
再点 `a.language.dropdown-item` 文案「中文简体」。整段失败只打日志,不抛异常。
核心逻辑:读 `#lanNavSel`;已是中文则跳过;否则点 `#lanNavSel`(SIMS 页脚语言按钮)
或旧版 `#kc_locale` 打开下拉,再点 `a.language.dropdown-item` 文案「中文简体」。
整段失败只打日志,不抛异常。
Args:
page: Playwright Page(可含 iframe)。
@@ -142,19 +144,24 @@ def _maybe_switch_dsv_locale_to_zh_cn(page: Any) -> bool:
roots = [page, *getattr(page, "frames", [])]
try:
# [STEP 1] 打开语言下拉
# [STEP 1] 打开语言下拉:2026-09-20 页面改版后开关是 SIMS 页脚 `#lanNavSel`
#(截图 DOM:下拉项为 a.language.dropdown-item),旧 Keycloak 页才是 `#kc_locale`;
# 两者都试,先新后旧。
opened = False
for root in roots:
try:
toggle = root.locator("#kc_locale").first
toggle.wait_for(state="visible", timeout=1_000)
toggle.click(timeout=2_000, force=True)
opened = True
for toggle_selector in ("#lanNavSel", "#kc_locale"):
for root in roots:
try:
toggle = root.locator(toggle_selector).first
toggle.wait_for(state="visible", timeout=1_000)
toggle.click(timeout=2_000, force=True)
opened = True
break
except Exception:
continue
if opened:
break
except Exception:
continue
if not opened:
print("[dsv-login] locale switch skipped: #kc_locale not clickable")
print("[dsv-login] locale switch skipped: #lanNavSel/#kc_locale not clickable")
return False
# [STEP 2] 点击「中文简体」
@@ -75,6 +75,16 @@ class NoInventoryDataError(RuntimeError):
"""
class DsvReportProcessingError(RuntimeError):
"""
DSV SSRS 报表服务端处理失败(rsProcessingAborted / rsDataSetExecutionError 等)。
核心逻辑:SSRS 错误页正文永久包含 "report processing" 文案,若按普通加载等待
会一直命中 asyncWait 直到拖满超时;本异常表示已确认的服务端终态错误,
供 worker 快速失败并按精确错误码上报(AWSRTM 2026-09-20 生产暂态故障)。
"""
def _click_dsv_export_excel(page: Any) -> None:
"""
触发 DSV SSRS ReportViewer 的 Excel 导出。
@@ -384,8 +394,9 @@ def _get_dsv_report_result_state(frame: Any) -> dict[str, bool]:
读取 DSV SSRS 结果 frame 的当前状态。
核心逻辑:真实页面里“结果表头已出现”与“报表真正渲染完成”并不同步,
这里统一抽象为 `asyncWait / exportReady / excelReady / bodyReady / directExportReady`
五个布尔量,供等待逻辑和导出逻辑复用。
这里统一抽象为 `asyncWait / processingError / exportReady / excelReady / bodyReady / directExportReady`
布尔量,供等待逻辑和导出逻辑复用;SSRS 服务端错误页视为终态(processingError),
不再命中 asyncWait,避免错误文案里的 "processing" 让等待逻辑误判为加载中。
"""
try:
state = frame.evaluate(
@@ -432,7 +443,13 @@ def _get_dsv_report_result_state(frame: Any) -> dict[str, bool]:
];
const bodyScore = bodyMarkers.filter((token) => normalizedBody.includes(token)).length;
const bodyReady = bodyScore > 0;
const asyncWait = [
const processingError = [
'rsprocessingaborted',
'rsdatasetexecutionerror',
'rsreportexecutionerror',
'an error has occurred during report',
].some((token) => normalizedBody.includes(token));
const asyncWait = !processingError && [
'loading',
'please wait',
'generating',
@@ -441,7 +458,7 @@ def _get_dsv_report_result_state(frame: Any) -> dict[str, bool]:
].some((token) => normalizedBody.includes(token));
const directExportReady = typeof exportReport === 'function' ||
(window && typeof window.exportReport === 'function');
return { asyncWait, exportReady, excelReady, bodyReady, bodyScore, directExportReady };
return { asyncWait, processingError, exportReady, excelReady, bodyReady, bodyScore, directExportReady };
}
"""
)
@@ -449,6 +466,7 @@ def _get_dsv_report_result_state(frame: Any) -> dict[str, bool]:
state = {}
return {
"asyncWait": bool((state or {}).get("asyncWait")),
"processingError": bool((state or {}).get("processingError")),
"exportReady": bool((state or {}).get("exportReady")),
"excelReady": bool((state or {}).get("excelReady")),
"bodyReady": bool((state or {}).get("bodyReady")),
@@ -637,6 +655,8 @@ def _wait_for_dsv_report_result_ready(page: Any, timeout_ms: int) -> None:
顶层页面未必能立即看到 `Export` 文案;因此这里改为轮询读取 frame 状态。
只要“目标结果 frame 的异步加载已结束 + 报表正文已出现”就允许继续尝试导出,因为真实站点上
导出能力有时直到点击工具栏/调用原生导出函数时才真正显式出现。
轮询中一旦确认 SSRS 服务端处理错误(rsProcessingAborted 等)即快速失败抛
DsvReportProcessingError,不再空等超时(AWSRTM 2026-09-20 生产故障暴露)。
"""
deadline = time.monotonic() + max(timeout_ms / 1000, 1)
last_stage_second = -1
@@ -647,6 +667,21 @@ def _wait_for_dsv_report_result_ready(page: Any, timeout_ms: int) -> None:
selected = _find_dsv_report_result_frame(page)
if selected is not None:
frame_index, _frame, state = selected
if state.get("processingError"):
# [FAST-FAIL] SSRS 错误页是终态:立即上抛精确错误,由批次内重试重新导航恢复。
_emit_debug_event(
"pre-fix",
"H2",
"freight_rpa_worker.py:_wait_for_dsv_report_result_ready:processing-error",
"[DEBUG] ssrs report processing error detected",
{"frameIndex": frame_index, **state},
)
_debug_dsv_stage(page, "wait-report-processing-error", target_text="Export")
raise DsvReportProcessingError(
"DSV s360 report failed on server side "
"(rsProcessingAborted/rsDataSetExecutionError); "
f"frameIndex={frame_index}"
)
if state["bodyReady"] and not state["asyncWait"]:
# #region debug-point H2:wait-report-ready
_emit_debug_event(