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

@@ -81,7 +81,7 @@ import dsv_debug # noqa: E402
import dsv_driver # noqa: E402
import dsv_navigation # noqa: E402
import dsv_report_export # noqa: E402
from dsv_report_export import NoInventoryDataError # noqa: E402
from dsv_report_export import DsvReportProcessingError, NoInventoryDataError # noqa: E402
import dsv_session # noqa: E402
import rpa_heartbeat # noqa: E402
import worker_disk_cleanup # noqa: E402
@@ -1307,6 +1307,13 @@ def _emit_batch_progress(body: dict[str, Any], run_no: str, results: list[dict[s
print(f"[inventory-batch] progress callback failed {run_no}: {exc}", file=sys.stderr)
# 批内单源导出的暂态失败重试参数:DSV s360 服务端报表错误(rsProcessingAborted 等)
# 属暂态故障(AWSRTM 2026-09-20 生产实测),失败源重新导航后重试一次,间隔 30s 覆盖
# SSRS 端恢复窗口;不设 env 开关,保持行为确定。
_EXPORT_SOURCE_MAX_ATTEMPTS = 2
_EXPORT_SOURCE_RETRY_WAIT_SECONDS = 30
def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processed_dir: Path) -> None:
"""
消费 inventory_export_batch trigger:按 Profile 分组导出并回调后端。
@@ -1489,62 +1496,84 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
source["__download_dir"] = str(drop_dir)
code = str(source.get("source_code") or "")
wh = str(source.get("warehouse_id") or "")
try:
report_configuration_test_progress(source)
_validate_inventory_source_for_rpa(source)
skip_navigation = False
if site == "dsv360":
# [STEP 3] 批内同一 Profile 只登录一次:首源完整登录,后续源复用当前 tab
# 的登录态——同导航走 _return_to_dsv_report_parameters 复用参数页,
# 导航不同走 _reset_dsv_export_entry(goto entry + 会话检测,不重复 OAuth)。
# 曾因"每源新开空白页 + 写死 index=0"导致每源重复走 SSO 重定向链
# (每源 2-2.5 分钟,且偶发 OAuth 空白页单源失败),现已移除。
# 页面串站状态由整页/帧重载兜底,无需隔离页。
previous_source = site_sources[index - 1] if index > 0 else None
try:
# 返回 True 表示参数页已就绪:后续跳过菜单导航直接填参/导出。
skip_navigation = bool(
_prepare_dsv_batch_export_step(page, entry_url, previous_source, source, index=index)
)
except Exception as exc:
# 会话复用的安全网:登录/会话类偶发异常(SSO/OAuth 链、
# “密码升级”弹窗遮挡、字段未渲染)时,新开干净页面重试一次完整登录;
# 凭据校验类(invalid/verification)不重试,直接失败。
message = str(exc).lower()
login_related = any(token in message for token in ("login", "session", "oauth"))
credential_mismatch = any(token in message for token in ("invalid", "verification"))
if not login_related or credential_mismatch:
raise
# [RETRY] DSV s360 服务端报表处理错误(rsProcessingAborted 等)为暂态故障
# (AWSRTM 2026-09-20 生产实测):失败源在批次内重试一次而非整批等下次排程;
# 重试会重跑会话/导航准备(同导航重载参数页,否则回首页重选菜单进报表),
# 绝不在错误页上直接再点 View Report。NoInventoryDataError 是业务结果,不重试。
for attempt in range(1, _EXPORT_SOURCE_MAX_ATTEMPTS + 1):
try:
if attempt == 1:
report_configuration_test_progress(source)
_validate_inventory_source_for_rpa(source)
skip_navigation = False
if site == "dsv360":
# [STEP 3] 批内同一 Profile 只登录一次:首源完整登录,后续源复用当前 tab
# 的登录态——同导航走 _return_to_dsv_report_parameters 复用参数页,
# 导航不同走 _reset_dsv_export_entry(goto entry + 会话检测,不重复 OAuth)。
# 曾因"每源新开空白页 + 写死 index=0"导致每源重复走 SSO 重定向链
# (每源 2-2.5 分钟,且偶发 OAuth 空白页单源失败),现已移除。
# 页面串站状态由整页/帧重载兜底,无需隔离页。
previous_source = site_sources[index - 1] if index > 0 else None
try:
# 返回 True 表示参数页已就绪:后续跳过菜单导航直接填参/导出。
skip_navigation = bool(
_prepare_dsv_batch_export_step(page, entry_url, previous_source, source, index=index)
)
except Exception as exc:
# 会话复用的安全网:登录/会话类偶发异常(SSO/OAuth 链、
# “密码升级”弹窗遮挡、字段未渲染)时,新开干净页面重试一次完整登录;
# 凭据校验类(invalid/verification)不重试,直接失败。
message = str(exc).lower()
login_related = any(token in message for token in ("login", "session", "oauth"))
credential_mismatch = any(token in message for token in ("invalid", "verification"))
if not login_related or credential_mismatch:
raise
print(
f"[inventory-batch] dsv360 session prepare failed for {code}, retry with clean page: {exc}",
file=sys.stderr,
)
page = _open_clean_batch_page(context, page)
skip_navigation = bool(
_prepare_dsv_batch_export_step(page, entry_url, None, source, index=0)
)
elif index == 0:
_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,
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, "worker_attempt": attempt, "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}")
break
except NoInventoryDataError as exc:
record_result({"source_code": code, "warehouse_id": wh, "status": "skipped", "success": False, "skipped": True, "skip_reason": "no_inventory_data", "message": str(exc), "worker_attempt": attempt, "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export skipped {code}/{wh}: {exc}")
break
except Exception as exc:
error_code = (
"dsv_report_processing_error"
if isinstance(exc, DsvReportProcessingError)
else "rpa_export_failed"
)
if attempt < _EXPORT_SOURCE_MAX_ATTEMPTS:
print(
f"[inventory-batch] dsv360 session prepare failed for {code}, retry with clean page: {exc}",
f"[inventory-batch] {site} export failed {code}/{wh} "
f"(attempt {attempt}/{_EXPORT_SOURCE_MAX_ATTEMPTS}), "
f"retry in {_EXPORT_SOURCE_RETRY_WAIT_SECONDS}s: {exc}",
file=sys.stderr,
)
page = _open_clean_batch_page(context, page)
skip_navigation = bool(
_prepare_dsv_batch_export_step(page, entry_url, None, source, index=0)
)
elif index == 0:
_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,
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:
record_result({"source_code": code, "warehouse_id": wh, "status": "skipped", "success": False, "skipped": True, "skip_reason": "no_inventory_data", "message": str(exc), "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export skipped {code}/{wh}: {exc}")
except Exception as exc:
record_result({"source_code": code, "warehouse_id": wh, "status": "failed", "success": False, "error_code": "rpa_export_failed", "error_message": str(exc), "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export failed {code}/{wh}: {exc}", file=sys.stderr)
time.sleep(_EXPORT_SOURCE_RETRY_WAIT_SECONDS)
continue
record_result({"source_code": code, "warehouse_id": wh, "status": "failed", "success": False, "error_code": error_code, "error_message": str(exc), "worker_attempt": attempt, "validation_run_no": source.get("validation_run_no")})
print(f"[inventory-batch] {site} export failed {code}/{wh}: {exc}", file=sys.stderr)
except Exception as exc:
for source in site_sources:
record_result({"source_code": source.get("source_code"), "warehouse_id": source.get("warehouse_id"), "status": "failed", "success": False, "error_code": "rpa_profile_failed", "error_message": str(exc), "validation_run_no": source.get("validation_run_no")})
@@ -50,8 +50,8 @@ if ($Environment -eq "test") {
} else {
$scopeId = "hxops-rpa-prod-data"
}
# freight_rpa 三层布局根:$WorkerRoot\freight_rpa\{inventory,receipt,common}(与后端 settings/.env 默认值一致)。
$rpaRoot = Join-Path $WorkerRoot "freight_rpa"
# freight_rpa 三层布局根:$WorkerRoot\data\freight_rpa\{inventory,receipt,common}(data 即 SMB 共享 hxops-rpa-*-data 的根,代码包 scripts 不进共享)。
$rpaRoot = Join-Path $WorkerRoot "data\freight_rpa"
function ConvertTo-PlainText {
param([Parameter(Mandatory = $true)][System.Security.SecureString]$Value)