feat(shipping-notice): 增加 RPA Worker 心跳上报与状态查询功能
This commit is contained in:
1 parent
c3f05ba96b
commit
5a147c4d85
49 files changed
+4032
-1500
No files matched your search
@@ -23,6 +23,13 @@ server {
|
||||
location /api/ {
|
||||
# 转发到 Gunicorn (Gevent)
|
||||
proxy_pass http://127.0.0.1:5000;
|
||||
|
||||
# 请求体上限(主要约束上传;库存 ZIP 下载靠下方超时)
|
||||
# 按业务估算:几十个大 Excel 打包约 100–300MB,预留 200m;若单批更大可改 500m
|
||||
client_max_body_size 200m;
|
||||
# 大文件响应超时(库存 ZIP 下载关键项)
|
||||
proxy_send_timeout 300s;
|
||||
send_timeout 300s;
|
||||
|
||||
# 关键配置:关闭缓冲以支持 SSE 流式输出
|
||||
proxy_buffering off;
|
||||
|
||||
@@ -26,6 +26,11 @@ Hxops 与货代系统之间通过 **目录 + JSON 触发文件** 解耦,RPA
|
||||
| `FREIGHT_INVENTORY_DROP_DIR` | RPA 落盘库存 Excel(`.xlsx`),hxops / worker 回调 ingest |
|
||||
| `FREIGHT_RPA_UPLOAD_TRIGGER_DIR` | hxops 写入 `upload_{task_no}.json` |
|
||||
| `FREIGHT_UPLOAD_QUEUE_DIR` | hxops 复制待上传发货 Excel |
|
||||
| `FREIGHT_RPA_SHARED_SCOPE_ID` | 与后端 `SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` 一致;Worker 在 **inventory_triggers** 目录写哨兵 `.hxops_rpa_shared_mount` |
|
||||
| `FREIGHT_RPA_ENVIRONMENT` | `test` / `prod`,须与后端 `ENV_NAME` 一致 |
|
||||
| `FREIGHT_RPA_HEARTBEAT_SECONDS` | 心跳间隔(默认 20s),test/prod 触发前检查依赖在线心跳 |
|
||||
|
||||
Windows test/prod 部署脚本:`scripts/rpa/set_rpa_worker_env.ps1`(详见 `docs/features/shipping-notice/technical.md` §1.5.5a)。
|
||||
|
||||
## 开发环境 Mock
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ from dsv_session import (
|
||||
goto_dsv_entry_url as _goto_dsv_entry_url,
|
||||
has_dsv_portal_shell as _has_dsv_portal_shell,
|
||||
is_dsv_home_ready as _is_dsv_home_ready,
|
||||
is_dsv_in_report_navigation_context as _is_dsv_in_report_navigation_context,
|
||||
maybe_auto_login_dsv as _maybe_auto_login_dsv,
|
||||
wait_for_dsv_home_ready as _wait_for_dsv_home_ready,
|
||||
)
|
||||
@@ -1014,6 +1015,9 @@ def _ensure_dsv_session(page: Any, entry_url: str) -> None:
|
||||
"""
|
||||
if _is_dsv_session_active(page, entry_url):
|
||||
print("[dsv] session already active on entry site, skip login")
|
||||
if _is_dsv_in_report_navigation_context(page):
|
||||
print("[dsv] session active but still in report context, goto entry portal home")
|
||||
_goto_dsv_entry_url(page, entry_url)
|
||||
_wait_for_dsv_home_ready(page, timeout_ms=30_000)
|
||||
_wait_for_dsv_reports_nav_stable(page, timeout_ms=8_000)
|
||||
return
|
||||
@@ -1041,14 +1045,14 @@ def _ensure_dsv_session(page: Any, entry_url: str) -> None:
|
||||
def _reset_dsv_export_entry(page: Any, entry_url: str) -> None:
|
||||
"""
|
||||
批量导出中 navigation 变化时回到 portal 首页。
|
||||
核心逻辑:同 entry_url 且 session 仍有效时不重复登录,仅回首页待命;
|
||||
核心逻辑:同 entry_url 且 session 仍有效时不重复登录,但始终 goto entry 离开报表 iframe;
|
||||
同 navigation 连续导出请改用 _return_to_dsv_report_parameters。
|
||||
"""
|
||||
print("[dsv] reset export entry before next source")
|
||||
if _is_dsv_session_active(page, entry_url):
|
||||
print("[dsv] same entry_url session active, skip re-login")
|
||||
if not _is_dsv_home_ready(page):
|
||||
_goto_dsv_entry_url(page, entry_url)
|
||||
print("[dsv] goto entry portal home for navigation change")
|
||||
_goto_dsv_entry_url(page, entry_url)
|
||||
_wait_for_dsv_home_ready(page, timeout_ms=30_000)
|
||||
_wait_for_dsv_reports_nav_stable(page, timeout_ms=8_000)
|
||||
return
|
||||
@@ -1139,15 +1143,19 @@ def _navigate_dsv_to_report_form(page: Any, source: dict[str, Any]) -> str:
|
||||
_click_dsv_reports_menu(page)
|
||||
_wait_for_dsv_reports_open(page, timeout_ms=10_000)
|
||||
|
||||
program_group = str(query.get("program_group") or navigation.get("program_group") or "").strip()
|
||||
program_label = str(query.get("program_label") or navigation.get("program_label") or "").strip()
|
||||
report_group = str(query.get("report_group") or navigation.get("report_group") or "Inventory")
|
||||
|
||||
program_selector = _env("FREIGHT_RPA_DSV_PROGRAM_SELECTOR")
|
||||
program_label = str(query.get("program_label") or navigation.get("program_label") or "")
|
||||
if program_selector:
|
||||
page.locator(program_selector).first.click(timeout=_browser_timeout_ms())
|
||||
else:
|
||||
_click_dsv_menu_item(page, program_label, next_text=str(query.get("report_group") or navigation.get("report_group") or "Inventory"))
|
||||
if program_group and program_group != program_label:
|
||||
_click_dsv_menu_item(page, program_group, next_text=program_label)
|
||||
_click_dsv_menu_item(page, program_label, next_text=report_group)
|
||||
|
||||
report_group_selector = _env("FREIGHT_RPA_DSV_REPORT_GROUP_SELECTOR")
|
||||
report_group = str(query.get("report_group") or navigation.get("report_group") or "Inventory")
|
||||
if report_group_selector:
|
||||
page.locator(report_group_selector).first.click(timeout=_browser_timeout_ms())
|
||||
else:
|
||||
|
||||
@@ -62,6 +62,38 @@ def _has_dsv_report_nav_usable(page: Any) -> bool:
|
||||
pass
|
||||
return _has_dsv_report_nav_attached(page)
|
||||
|
||||
def _is_dsv_in_report_navigation_context(page: Any) -> bool:
|
||||
"""
|
||||
判断当前是否仍处于报表 iframe / SSRS 上下文,而非 portal 首页。
|
||||
|
||||
核心逻辑:报表 frame URL 与 portal 首页一样会渲染 Home/Favorites 侧栏与 Reports 顶栏;
|
||||
仅探测 Reports 导航不足以区分,必须结合顶层 URL 与 report-frame 是否仍挂载。
|
||||
"""
|
||||
try:
|
||||
current_url = str(page.url or "").lower()
|
||||
except Exception:
|
||||
current_url = ""
|
||||
if "/main/portal/frame/reports/" in current_url:
|
||||
return True
|
||||
if "/s360reports/" in current_url:
|
||||
return True
|
||||
for frame in getattr(page, "frames", []):
|
||||
try:
|
||||
frame_url = str(frame.url or "").lower()
|
||||
except Exception:
|
||||
frame_url = ""
|
||||
if "/s360reports/reports/default.aspx" in frame_url:
|
||||
return True
|
||||
if "/s360reports/ssrs/" in frame_url:
|
||||
return True
|
||||
try:
|
||||
if frame.locator("#ReportViewerMain").count() > 0:
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
return False
|
||||
|
||||
|
||||
def _is_dsv_on_portal_home(page: Any) -> bool:
|
||||
"""URL 已在 portal 且主页面已出现 Home/Favorites/Recent Reports 等内容。"""
|
||||
try:
|
||||
@@ -72,6 +104,8 @@ def _is_dsv_on_portal_home(page: Any) -> bool:
|
||||
return False
|
||||
if "s360.dbschenker.com" not in current_url:
|
||||
return False
|
||||
if _is_dsv_in_report_navigation_context(page):
|
||||
return False
|
||||
try:
|
||||
body = str(page.locator("body").inner_text(timeout=1_000) or "").lower()
|
||||
except Exception:
|
||||
@@ -219,11 +253,13 @@ def _is_dsv_home_ready(page: Any) -> bool:
|
||||
"""
|
||||
判断 DSV 是否已经稳定进入首页导航态。
|
||||
|
||||
核心逻辑:必须离开认证域名,且 Reports 导航已可用(跨 frame 探测);
|
||||
用户肉眼已在 portal 首页时,不再仅依赖顶层 #report-mega-legacy 计数。
|
||||
核心逻辑:必须离开认证域名与报表 iframe 上下文,且 Reports 导航已可用;
|
||||
停留在 `/main/portal/frame/reports/` 时即使顶栏可见,也不应视为 home-ready。
|
||||
"""
|
||||
if "auth.dbschenker.com" in page.url:
|
||||
return False
|
||||
if _is_dsv_in_report_navigation_context(page):
|
||||
return False
|
||||
return _has_dsv_report_nav_usable(page)
|
||||
|
||||
def _wait_for_dsv_home_ready(
|
||||
@@ -376,6 +412,7 @@ has_dsv_portal_shell = _has_dsv_portal_shell
|
||||
has_dsv_report_nav_attached = _has_dsv_report_nav_attached
|
||||
has_dsv_report_nav_usable = _has_dsv_report_nav_usable
|
||||
is_dsv_home_ready = _is_dsv_home_ready
|
||||
is_dsv_in_report_navigation_context = _is_dsv_in_report_navigation_context
|
||||
is_dsv_on_portal_home = _is_dsv_on_portal_home
|
||||
iter_dsv_report_nav_locators = _iter_dsv_report_nav_locators
|
||||
wait_for_dsv_app_after_login = _wait_for_dsv_app_after_login
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
"""
|
||||
scripts/rpa/freight_rpa_heartbeat.py
|
||||
|
||||
用途:货代 RPA Worker 向后端上报心跳(POST /api/shipping-notice/rpa/heartbeat)。
|
||||
依赖:rpa_common.env、urllib、socket。
|
||||
逻辑:由 freight_rpa_worker 主循环按 FREIGHT_RPA_HEARTBEAT_SECONDS 周期调用;同步写入共享目录哨兵文件。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from datetime import UTC, datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from rpa_common import env as _env
|
||||
|
||||
_RUNTIME_STATUS = "idle"
|
||||
_LAST_HEARTBEAT_MONO = 0.0
|
||||
_DEFAULT_SENTINEL_NAME = ".hxops_rpa_shared_mount"
|
||||
|
||||
|
||||
def set_worker_runtime_status(status: str) -> None:
|
||||
"""更新 worker 运行态(idle / busy),供心跳上报。"""
|
||||
global _RUNTIME_STATUS
|
||||
normalized = str(status or "idle").strip().lower()
|
||||
_RUNTIME_STATUS = normalized if normalized in {"idle", "busy"} else "idle"
|
||||
|
||||
|
||||
def get_worker_runtime_status() -> str:
|
||||
return _RUNTIME_STATUS
|
||||
|
||||
|
||||
def _resolve_worker_environment() -> str:
|
||||
raw = (
|
||||
_env("FREIGHT_RPA_ENVIRONMENT")
|
||||
or _env("ENV_NAME")
|
||||
or _env("HXOPS_ENV")
|
||||
or "dev"
|
||||
)
|
||||
value = str(raw or "dev").strip().lower()
|
||||
if value in {"development", ""}:
|
||||
return "dev"
|
||||
if value in {"testing"}:
|
||||
return "test"
|
||||
if value in {"production"}:
|
||||
return "prod"
|
||||
return value
|
||||
|
||||
|
||||
def _resolve_shared_scope_id() -> str:
|
||||
raw = (
|
||||
_env("FREIGHT_RPA_SHARED_SCOPE_ID")
|
||||
or _env("SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID")
|
||||
or ""
|
||||
)
|
||||
value = str(raw or "").strip()
|
||||
if value:
|
||||
return value
|
||||
trigger = str(_env("FREIGHT_RPA_INVENTORY_TRIGGER_DIR") or "").replace("\\", "/").lower()
|
||||
for token in ("hxops-rpa-test-data", "hxops-rpa-prod-data"):
|
||||
if token in trigger:
|
||||
return token
|
||||
return ""
|
||||
|
||||
|
||||
def _resolve_sentinel_name() -> str:
|
||||
return str(
|
||||
_env("FREIGHT_RPA_SHARED_SENTINEL_NAME")
|
||||
or _env("SHIPPING_NOTICE_RPA_SHARED_SENTINEL_NAME")
|
||||
or _DEFAULT_SENTINEL_NAME
|
||||
).strip() or _DEFAULT_SENTINEL_NAME
|
||||
|
||||
|
||||
def _resolve_worker_id(hostname: str) -> str:
|
||||
explicit = str(_env("FREIGHT_RPA_WORKER_ID") or "").strip()
|
||||
if explicit:
|
||||
return explicit
|
||||
pid = os.getpid()
|
||||
return f"{hostname}-{pid}"
|
||||
|
||||
|
||||
def write_shared_mount_sentinel(*, trigger_dir: str | Path, hostname: str | None = None) -> None:
|
||||
"""
|
||||
在 trigger 共享目录写入哨兵文件,供 Ubuntu 后端校验 CIFS 挂载与 scope 一致。
|
||||
核心逻辑:不创建 trigger 根目录,仅更新已存在目录下的哨兵文件。
|
||||
"""
|
||||
scope_id = _resolve_shared_scope_id()
|
||||
if not scope_id:
|
||||
return
|
||||
root = Path(trigger_dir)
|
||||
if not root.is_dir():
|
||||
return
|
||||
host = hostname or socket.gethostname()
|
||||
body = "\n".join([
|
||||
f"scope_id={scope_id}",
|
||||
f"updated_at={datetime.now(UTC).replace(tzinfo=None).isoformat()}",
|
||||
f"host={host}",
|
||||
f"worker_id={_resolve_worker_id(host)}",
|
||||
])
|
||||
sentinel_path = root / _resolve_sentinel_name()
|
||||
sentinel_path.write_text(body + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
def build_heartbeat_payload(
|
||||
*,
|
||||
trigger_dir: str | Path,
|
||||
drop_dir: str | Path,
|
||||
status: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
组装心跳 payload。
|
||||
核心逻辑:capabilities 固定包含 inventory_export;shared_scope_id 用于跨平台路径对齐。
|
||||
"""
|
||||
hostname = socket.gethostname()
|
||||
return {
|
||||
"worker_id": _resolve_worker_id(hostname),
|
||||
"hostname": hostname,
|
||||
"environment": _resolve_worker_environment(),
|
||||
"capabilities": ["inventory_export"],
|
||||
"status": status or get_worker_runtime_status(),
|
||||
"trigger_dir": str(trigger_dir),
|
||||
"drop_dir": str(drop_dir),
|
||||
"shared_scope_id": _resolve_shared_scope_id(),
|
||||
"version": str(_env("FREIGHT_RPA_WORKER_VERSION", "rpa-poc") or "rpa-poc"),
|
||||
"timestamp": datetime.now(UTC).replace(tzinfo=None).isoformat(),
|
||||
}
|
||||
|
||||
|
||||
def post_heartbeat(payload: dict[str, Any]) -> bool:
|
||||
"""
|
||||
向后端上报心跳。
|
||||
Returns:
|
||||
bool: HTTP 2xx 时 True,否则 False(不中断 worker 主循环)。
|
||||
"""
|
||||
base = str(_env("HXOPS_API_BASE", "http://127.0.0.1:5052") or "http://127.0.0.1:5052").rstrip("/")
|
||||
key = (
|
||||
_env("SHIPPING_NOTICE_TOOL_API_KEY")
|
||||
or _env("INTEGRATION_API_KEY")
|
||||
or _env("HXOPS_TOOLS_API_KEY")
|
||||
)
|
||||
if not key:
|
||||
print("[heartbeat] skipped: missing SHIPPING_NOTICE_TOOL_API_KEY", file=sys.stderr)
|
||||
return False
|
||||
|
||||
url = f"{base}/api/shipping-notice/rpa/heartbeat"
|
||||
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
url,
|
||||
data=data,
|
||||
headers={
|
||||
"Content-Type": "application/json",
|
||||
"X-Integration-Key": key,
|
||||
"X-Shipping-Tool-Key": key,
|
||||
},
|
||||
method="POST",
|
||||
)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=8) as resp:
|
||||
return 200 <= int(resp.status) < 300
|
||||
except urllib.error.HTTPError as exc:
|
||||
body = ""
|
||||
try:
|
||||
body = exc.read().decode("utf-8", errors="replace")[:500]
|
||||
except Exception:
|
||||
body = ""
|
||||
print(f"[heartbeat] HTTP {exc.code} {body}", file=sys.stderr)
|
||||
return False
|
||||
except Exception as exc:
|
||||
print(f"[heartbeat] failed: {exc}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def maybe_send_heartbeat(
|
||||
*,
|
||||
trigger_dir: str | Path,
|
||||
drop_dir: str | Path,
|
||||
last_sent_mono: float,
|
||||
force: bool = False,
|
||||
) -> float:
|
||||
"""
|
||||
按 FREIGHT_RPA_HEARTBEAT_SECONDS 节流发送心跳。
|
||||
Returns:
|
||||
float: 最近一次尝试后的 monotonic 时间戳(成功或失败均更新节流时钟)。
|
||||
"""
|
||||
global _LAST_HEARTBEAT_MONO
|
||||
interval = max(15, int(_env("FREIGHT_RPA_HEARTBEAT_SECONDS", "20") or "20"))
|
||||
now = time.monotonic()
|
||||
if not force and last_sent_mono and (now - last_sent_mono) < interval:
|
||||
return last_sent_mono
|
||||
write_shared_mount_sentinel(trigger_dir=trigger_dir)
|
||||
payload = build_heartbeat_payload(trigger_dir=trigger_dir, drop_dir=drop_dir)
|
||||
post_heartbeat(payload)
|
||||
_LAST_HEARTBEAT_MONO = now
|
||||
return now
|
||||
@@ -61,6 +61,7 @@ import dsv_navigation # noqa: E402
|
||||
import dsv_report_export # noqa: E402
|
||||
from dsv_report_export import NoInventoryDataError # noqa: E402
|
||||
import dsv_session # noqa: E402
|
||||
import freight_rpa_heartbeat # noqa: E402
|
||||
|
||||
# 测试与 smoke 向后兼容 re-export
|
||||
_maybe_auto_login_dsv = dsv_session.maybe_auto_login_dsv
|
||||
@@ -1101,10 +1102,13 @@ def _scan_triggers(trigger_dir: Path, handler, extra_args: tuple = ()) -> None:
|
||||
for path in sorted(trigger_dir.glob("*.json")):
|
||||
if not path.is_file():
|
||||
continue
|
||||
freight_rpa_heartbeat.set_worker_runtime_status("busy")
|
||||
try:
|
||||
handler(path, *extra_args)
|
||||
except Exception as exc:
|
||||
print(f"[error] {path.name}: {exc}", file=sys.stderr)
|
||||
finally:
|
||||
freight_rpa_heartbeat.set_worker_runtime_status("idle")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
@@ -1125,7 +1129,20 @@ def main() -> None:
|
||||
print(f" drop={drop_dir}")
|
||||
print(f" queue={upload_queue_dir}")
|
||||
|
||||
last_heartbeat_mono = 0.0
|
||||
last_heartbeat_mono = freight_rpa_heartbeat.maybe_send_heartbeat(
|
||||
trigger_dir=inv_trigger_dir,
|
||||
drop_dir=drop_dir,
|
||||
last_sent_mono=last_heartbeat_mono,
|
||||
force=True,
|
||||
)
|
||||
|
||||
while True:
|
||||
last_heartbeat_mono = freight_rpa_heartbeat.maybe_send_heartbeat(
|
||||
trigger_dir=inv_trigger_dir,
|
||||
drop_dir=drop_dir,
|
||||
last_sent_mono=last_heartbeat_mono,
|
||||
)
|
||||
_scan_triggers(inv_trigger_dir, _handle_inventory_trigger, (drop_dir, inv_processed))
|
||||
_scan_triggers(upload_trigger_dir, _handle_upload_trigger, (upload_processed,))
|
||||
time.sleep(poll)
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
<#
|
||||
.SYNOPSIS
|
||||
为货代库存 RPA Worker 写入 Windows 用户级永久环境变量(含共享 scope 与心跳)。
|
||||
|
||||
.DESCRIPTION
|
||||
test/prod 部署时必须配置 FREIGHT_RPA_SHARED_SCOPE_ID,与 Ubuntu 后端
|
||||
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID 一致,否则 Worker 不会写哨兵文件,
|
||||
后端就绪检查将报 shared_sentinel_missing。
|
||||
|
||||
用法(以管理员或 RPA 专用账号登录后执行一次):
|
||||
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment test -ApiBase "http://192.168.1.20:8890" -ToolApiKey "<密钥>"
|
||||
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment prod -ApiBase "http://生产地址:端口" -ToolApiKey "<密钥>"
|
||||
|
||||
执行后请关闭并重新打开 PowerShell,再运行 freight_rpa_worker.py。
|
||||
完整说明见 docs/features/shipping-notice/technical.md §1.5.5a / §1.5.10。
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[ValidateSet("test", "prod")]
|
||||
[string]$Environment,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$ApiBase,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$ToolApiKey,
|
||||
|
||||
[string]$DsvUsername = "",
|
||||
[string]$DsvPassword = "",
|
||||
[string]$CraneUsername = "",
|
||||
[string]$CranePassword = ""
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
if ($Environment -eq "test") {
|
||||
$Root = "C:\HxopsRpaTest"
|
||||
$ScopeId = "hxops-rpa-test-data"
|
||||
} else {
|
||||
$Root = "C:\HxopsRpaProd"
|
||||
$ScopeId = "hxops-rpa-prod-data"
|
||||
}
|
||||
|
||||
$DataRoot = Join-Path $Root "data"
|
||||
$Vars = [ordered]@{
|
||||
"HXOPS_API_BASE" = $ApiBase
|
||||
"SHIPPING_NOTICE_TOOL_API_KEY" = $ToolApiKey
|
||||
"FREIGHT_RPA_ENVIRONMENT" = $Environment
|
||||
"FREIGHT_RPA_SHARED_SCOPE_ID" = $ScopeId
|
||||
"FREIGHT_RPA_HEARTBEAT_SECONDS" = "20"
|
||||
"FREIGHT_RPA_MOCK" = "false"
|
||||
"FREIGHT_RPA_HEADLESS" = "false"
|
||||
"FREIGHT_RPA_BROWSER_CHANNEL" = "msedge"
|
||||
"FREIGHT_RPA_POLL_SECONDS" = "3"
|
||||
"FREIGHT_RPA_INVENTORY_TRIGGER_DIR" = Join-Path $DataRoot "inventory_triggers"
|
||||
"FREIGHT_INVENTORY_DROP_DIR" = Join-Path $DataRoot "inventory_drop"
|
||||
"FREIGHT_RPA_UPLOAD_TRIGGER_DIR" = Join-Path $DataRoot "upload_triggers"
|
||||
"FREIGHT_UPLOAD_QUEUE_DIR" = Join-Path $DataRoot "upload_queue"
|
||||
"FREIGHT_RPA_BROWSER_PROFILE_DIR" = Join-Path $DataRoot "browser_profile"
|
||||
"FREIGHT_RPA_LOG_FILE" = Join-Path $DataRoot "logs\worker.log"
|
||||
"FREIGHT_RPA_LOG_MAX_BYTES" = "10485760"
|
||||
"FREIGHT_RPA_LOG_BACKUP_COUNT" = "5"
|
||||
}
|
||||
|
||||
if ($DsvUsername) { $Vars["FREIGHT_RPA_DSV_USERNAME"] = $DsvUsername }
|
||||
if ($DsvPassword) { $Vars["FREIGHT_RPA_DSV_PASSWORD"] = $DsvPassword }
|
||||
if ($CraneUsername) { $Vars["FREIGHT_RPA_CRANE_USERNAME"] = $CraneUsername }
|
||||
if ($CranePassword) { $Vars["FREIGHT_RPA_CRANE_PASSWORD"] = $CranePassword }
|
||||
|
||||
$SensitiveKeys = @(
|
||||
"SHIPPING_NOTICE_TOOL_API_KEY",
|
||||
"FREIGHT_RPA_DSV_PASSWORD",
|
||||
"FREIGHT_RPA_CRANE_PASSWORD"
|
||||
)
|
||||
|
||||
$UsernameKeys = @(
|
||||
"FREIGHT_RPA_DSV_USERNAME",
|
||||
"FREIGHT_RPA_CRANE_USERNAME"
|
||||
)
|
||||
|
||||
function Get-MaskedUsername {
|
||||
param([string]$Value)
|
||||
if (-not $Value) { return $Value }
|
||||
if ($Value -match '^([^@]+)@(.+)$') {
|
||||
$local = $Matches[1]
|
||||
$domain = $Matches[2]
|
||||
if ($local.Length -le 3) {
|
||||
return "***@$domain"
|
||||
}
|
||||
return ($local.Substring(0, 3) + "***@" + $domain)
|
||||
}
|
||||
if ($Value.Length -le 3) {
|
||||
return "***"
|
||||
}
|
||||
return ($Value.Substring(0, 3) + "***")
|
||||
}
|
||||
|
||||
function Get-DisplayValue {
|
||||
param(
|
||||
[string]$Key,
|
||||
[string]$Value
|
||||
)
|
||||
if ($SensitiveKeys -contains $Key -and $Value) {
|
||||
return "******"
|
||||
}
|
||||
if ($UsernameKeys -contains $Key -and $Value) {
|
||||
return Get-MaskedUsername -Value $Value
|
||||
}
|
||||
return $Value
|
||||
}
|
||||
|
||||
foreach ($entry in $Vars.GetEnumerator()) {
|
||||
[Environment]::SetEnvironmentVariable($entry.Key, [string]$entry.Value, "User")
|
||||
$display = Get-DisplayValue -Key $entry.Key -Value ([string]$entry.Value)
|
||||
Write-Host ("[ok] {0}={1}" -f $entry.Key, $display)
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Environment=$Environment scope_id=$ScopeId root=$Root"
|
||||
Write-Host "请关闭并重新打开 PowerShell,然后验证:"
|
||||
Write-Host " echo `$env:FREIGHT_RPA_SHARED_SCOPE_ID"
|
||||
Write-Host " cd $Root"
|
||||
Write-Host " python scripts\rpa\freight_rpa_worker.py"
|
||||
Reference in new issue
Block a user