feat(shipping-notice): 增强订单抽取功能,添加进度事件和诊断运行管理,优化数据模型和访问控制
This commit is contained in:
1 parent
ce26d6d0c4
commit
c4095b08da
175 files changed
+13064
-1814
No files matched your search
+64
-15
@@ -112,6 +112,21 @@ def _is_dsv_on_portal_home(page: Any) -> bool:
|
||||
body = ""
|
||||
return any(marker in body for marker in ("home favorites", "recent reports", "favorites"))
|
||||
|
||||
|
||||
def _dsv_sso_return_timeout_ms() -> int:
|
||||
"""读取登录提交后 SSO 回跳的独立上限,避免混入首页菜单等待。"""
|
||||
return max(30_000, int(
|
||||
_env("SHIPPING_NOTICE_RPA_WORKER_DSV_SSO_RETURN_TIMEOUT_MS", "90000") or "90000"
|
||||
))
|
||||
|
||||
|
||||
def _dsv_sso_reload_after_ms() -> int:
|
||||
"""读取空白授权中转页的一次性刷新阈值,默认等待 12 秒。"""
|
||||
return max(5_000, int(
|
||||
_env("SHIPPING_NOTICE_RPA_WORKER_DSV_SSO_RELOAD_AFTER_MS", "12000") or "12000"
|
||||
))
|
||||
|
||||
|
||||
def _wait_for_dsv_app_after_login(
|
||||
page: Any,
|
||||
timeout_ms: int | None = None,
|
||||
@@ -126,7 +141,10 @@ def _wait_for_dsv_app_after_login(
|
||||
"""
|
||||
import dsv_auth
|
||||
|
||||
wait_ms = timeout_ms if timeout_ms is not None else _browser_timeout_ms()
|
||||
requested_wait_ms = timeout_ms if timeout_ms is not None else _browser_timeout_ms()
|
||||
sso_wait_ms = _dsv_sso_return_timeout_ms()
|
||||
# SSO 回跳和 Portal 菜单加载各有独立时间预算,不能互相吞掉。
|
||||
wait_ms = sso_wait_ms + requested_wait_ms
|
||||
deadline = time.monotonic() + (wait_ms / 1000)
|
||||
last_debug_at = 0.0
|
||||
started_at = time.monotonic()
|
||||
@@ -141,6 +159,9 @@ def _wait_for_dsv_app_after_login(
|
||||
_debug_dsv_stage(page, "wait-app-start", target_text="Reports")
|
||||
last_portal_home_log_at = 0.0
|
||||
portal_shell_callback_fired = False
|
||||
sso_started_at: float | None = None
|
||||
sso_reload_attempted = False
|
||||
last_sso_log_at = 0.0
|
||||
while time.monotonic() < deadline:
|
||||
now = time.monotonic()
|
||||
current_url = str(page.url or "").lower()
|
||||
@@ -165,6 +186,40 @@ def _wait_for_dsv_app_after_login(
|
||||
{"elapsedMs": int((time.monotonic() - started_at) * 1000), "url": page.url},
|
||||
)
|
||||
return True
|
||||
if auth_transition:
|
||||
if sso_started_at is None:
|
||||
sso_started_at = now
|
||||
print(f"[dsv-sso] authorization return started url={page.url!r}")
|
||||
sso_elapsed_ms = int((now - sso_started_at) * 1000)
|
||||
if now - last_sso_log_at >= 10.0:
|
||||
print(
|
||||
"[dsv-sso] waiting authorization return "
|
||||
f"elapsedMs={sso_elapsed_ms} reloaded={sso_reload_attempted} url={page.url!r}"
|
||||
)
|
||||
last_sso_log_at = now
|
||||
is_blank_transition = dsv_auth.is_dsv_blank_auth_transition(page)
|
||||
if (
|
||||
is_blank_transition
|
||||
and not sso_reload_attempted
|
||||
and sso_elapsed_ms >= _dsv_sso_reload_after_ms()
|
||||
):
|
||||
sso_reload_attempted = True
|
||||
print("[dsv-sso] blank authorization transition persisted; reload once")
|
||||
try:
|
||||
page.reload(wait_until="commit", timeout=15_000)
|
||||
except Exception as exc:
|
||||
print(f"[dsv-sso] authorization refresh failed: {exc}", file=sys.stderr)
|
||||
continue
|
||||
if sso_elapsed_ms >= sso_wait_ms:
|
||||
raise RuntimeError(
|
||||
"DSV SSO authorization return timed out "
|
||||
f"after {sso_elapsed_ms}ms; portal did not return after login"
|
||||
)
|
||||
if dsv_auth.has_dsv_password_upgrade_prompt(page, timeout_ms=250):
|
||||
dsv_auth.dismiss_dsv_password_upgrade_prompt(page)
|
||||
continue
|
||||
time.sleep(0.25)
|
||||
continue
|
||||
if not auth_transition and _wait_for_visible_selector(
|
||||
page,
|
||||
"",
|
||||
@@ -181,11 +236,18 @@ def _wait_for_dsv_app_after_login(
|
||||
{"elapsedMs": int((time.monotonic() - started_at) * 1000), "url": page.url},
|
||||
)
|
||||
return True
|
||||
on_portal_home = _is_dsv_on_portal_home(page)
|
||||
# Portal 壳层已出现后,只轮询 Reports 定位器,不再反复读取整页和 iframe 文本。
|
||||
on_portal_home = portal_shell_callback_fired or _is_dsv_on_portal_home(page)
|
||||
if on_portal_home and not auth_transition:
|
||||
if on_portal_shell_seen is not None and not portal_shell_callback_fired:
|
||||
on_portal_shell_seen()
|
||||
portal_shell_callback_fired = True
|
||||
if sso_started_at is not None:
|
||||
print(
|
||||
"[dsv-sso] authorization return completed "
|
||||
f"elapsedMs={int((now - sso_started_at) * 1000)}"
|
||||
)
|
||||
print("[dsv-portal] portal shell visible; waiting Reports navigation")
|
||||
if now - last_portal_home_log_at >= 2.0:
|
||||
print(
|
||||
"[dsv-home] portal home visible, waiting Reports nav "
|
||||
@@ -196,19 +258,6 @@ def _wait_for_dsv_app_after_login(
|
||||
elif _has_dsv_portal_shell(page) and on_portal_shell_seen is not None and not portal_shell_callback_fired:
|
||||
on_portal_shell_seen()
|
||||
portal_shell_callback_fired = True
|
||||
if auth_transition:
|
||||
if dsv_auth.has_dsv_password_upgrade_prompt(page, timeout_ms=250):
|
||||
dsv_auth.dismiss_dsv_password_upgrade_prompt(page)
|
||||
continue
|
||||
# [STEP] 合规/认证过渡页仅用轻量短超时探测 Reports 导航,避免与首页长轮询混淆。
|
||||
_wait_for_visible_selector(
|
||||
page,
|
||||
"",
|
||||
_DSV_REPORTS_NAV_SELECTORS,
|
||||
timeout_ms=400,
|
||||
)
|
||||
time.sleep(0.25)
|
||||
continue
|
||||
if "s360.dbschenker.com" in current_url and "auth.dbschenker.com" not in current_url:
|
||||
if not _has_dsv_report_nav_usable(page) and dsv_auth.has_dsv_password_upgrade_prompt(page, timeout_ms=250):
|
||||
dsv_auth.dismiss_dsv_password_upgrade_prompt(page)
|
||||
|
||||
@@ -582,6 +582,25 @@ def _group_batch_sources_by_profile(sources: list[Any]) -> dict[tuple[str, str],
|
||||
return grouped
|
||||
|
||||
|
||||
def _open_clean_batch_page(context: Any, current_page: Any | None) -> Any:
|
||||
"""在同一登录上下文中切换到干净页面,隔离上一站的 DSV 报表状态。
|
||||
|
||||
Args:
|
||||
context: 已建立登录态的 Playwright browser context,Cookie 会继续复用。
|
||||
current_page: 上一站使用的页面;关闭它避免报表 iframe 和下载菜单残留。
|
||||
|
||||
Returns:
|
||||
Any: 可重新进入库存报表的空白页面,不会触发新的浏览器登录。
|
||||
"""
|
||||
next_page = context.new_page()
|
||||
if current_page is not None:
|
||||
try:
|
||||
current_page.close()
|
||||
except Exception as exc:
|
||||
print(f"[inventory-batch] close previous page failed: {exc}", file=sys.stderr)
|
||||
return next_page
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _profile_credentials(source: dict[str, Any]):
|
||||
"""仅在单 Profile 执行范围内解密凭据并暂存环境变量,退出时恢复原值。"""
|
||||
@@ -1008,7 +1027,11 @@ def _handle_inventory_batch_trigger(trigger_path: Path, drop_dir: Path, processe
|
||||
_validate_inventory_source_for_rpa(source)
|
||||
skip_navigation = False
|
||||
if site == "dsv360":
|
||||
skip_navigation = _prepare_dsv_batch_export_step(page, entry_url, site_sources[index - 1] if index > 0 else None, source, index=index)
|
||||
# DSV 报表页会保留 iframe、导出菜单与下载等待状态;同一 tab
|
||||
# 串行切换站点会让下一站误用上一站页面。复用 context 登录态,但每站新开页面。
|
||||
if index > 0:
|
||||
page = _open_clean_batch_page(context, page)
|
||||
_prepare_dsv_batch_export_step(page, entry_url, None, source, index=0)
|
||||
elif index == 0:
|
||||
_ensure_crane_session(page, entry_url)
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user