feat(shipping-notice): 收敛阶段能力投影并修复库存 RPA 运行链路
下发 preparation capabilities 并与右卡展示同源授权; 库存 RPA 运行态请求内 memo;取消会话 Profile 本地 TTL; 并为 shipping-worker 挂载共享盘。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
f773237226
commit
a6edc4fe61
16 files changed
+433
-179
No files matched your search
+34
-33
@@ -77,8 +77,7 @@ Hxops 与货代系统之间通过 **目录 + JSON 触发文件** 解耦,RPA
|
||||
| `SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT` | `test` / `prod`,须与后端 `ENV_NAME` 一致 |
|
||||
| `SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_SECONDS` | 心跳间隔(默认 20s),test/prod 触发前检查依赖在线心跳 |
|
||||
| `SHIPPING_NOTICE_RPA_CREDENTIAL_KEY` | Profile 凭据 Fernet 主密钥;必须与对应环境后端一致,只能由 Secret 渠道或部署脚本安全注入 |
|
||||
| `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE` | 库存导出是否复用浏览器登录 Profile,默认 `true`;上传任务不受影响 |
|
||||
| `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS` | 持久 Profile 最长保留小时数,默认 `24`;超时后清空目录并重新登录 |
|
||||
| `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE` | 库存导出是否复用浏览器登录 Profile,默认 `true`;上传任务不受影响;站点会话失效时自动重登 |
|
||||
|
||||
|
||||
新部署只使用 `new_rpa_worker_secret.ps1` + `start_rpa_worker.ps1`:Secret 由 DPAPI 保护,环境变量只注入对应 Worker 子进程。
|
||||
@@ -88,9 +87,9 @@ Hxops 与货代系统之间通过 **目录 + JSON 触发文件** 解耦,RPA
|
||||
`SHIPPING_NOTICE_RPA_CREDENTIAL_KEY`。部署文档和启动脚本只使用上述规范命名。
|
||||
|
||||
DSV/Crane 库存导出默认复用浏览器 Profile(按 `connector + rpa_profile_code` 隔离目录,
|
||||
默认保留 24 小时)。同一 WorkerRoot 只跑一个 Worker,任务串行消费;会话过期或站点踢登
|
||||
仍回落现有登录流程。上传任务保持一次性 Profile。若需强制每次全新登录,启动时加
|
||||
`-NoSessionReuse`。旧参数 `-ReuseDsvSession` / `SHIPPING_NOTICE_RPA_WORKER_DSV_SESSION_REUSE`
|
||||
长期保留)。同一 WorkerRoot 只跑一个 Worker,任务串行消费;是否免登取决于货代站点会话
|
||||
是否仍有效,失效时回落现有登录流程。上传任务保持一次性 Profile。若需强制每次全新登录,
|
||||
启动时加 `-NoSessionReuse`。旧参数 `-ReuseDsvSession` / `SHIPPING_NOTICE_RPA_WORKER_DSV_SESSION_REUSE`
|
||||
仍兼容,但请改用 `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE`。
|
||||
|
||||
### DSV WMS Upload Tool
|
||||
@@ -521,20 +520,21 @@ SHIPPING_NOTICE_TOOL_API_KEY
|
||||
SHIPPING_NOTICE_RPA_CREDENTIAL_KEY
|
||||
```
|
||||
|
||||
如使用 Docker Compose,容器必须挂载对应目录:
|
||||
如使用 Docker Compose,**写 RPA trigger 的容器都必须挂载**对应目录:`hxops-backend`、`hxops-schedule-worker`(定时导出)、`hxops-shipping-worker`(按需库存准备 Gate Job)。只挂 backend 时,shipping-worker 会在容器本地假目录写 JSON,DB 记 `trigger_written`,但 Windows 共享盘无文件。
|
||||
|
||||
```yaml
|
||||
services:
|
||||
hxops-backend:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
# hxops-backend / hxops-schedule-worker / hxops-shipping-worker 均需:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
```
|
||||
|
||||
重建/启动后端容器后验证:
|
||||
重建相关容器后验证:
|
||||
|
||||
```bash
|
||||
docker compose --env-file .env.test up -d --force-recreate hxops-shipping-worker
|
||||
docker compose --env-file .env.test exec -T hxops-backend sh -lc 'ls -ld /mnt/hxops-rpa-test-data/inventory_triggers'
|
||||
docker compose --env-file .env.test exec -T hxops-shipping-worker sh -lc 'mountpoint -q /mnt/hxops-rpa-test-data && ls -ld /mnt/hxops-rpa-test-data/inventory_triggers'
|
||||
docker compose --env-file .env.prod exec -T hxops-backend sh -lc 'ls -ld /mnt/hxops-rpa-prod-data/inventory_triggers'
|
||||
```
|
||||
|
||||
@@ -1022,14 +1022,13 @@ SHIPPING_NOTICE_RPA_WORKER_ALERT_SEND_LOCK_SECONDS=120
|
||||
sudo systemctl restart hxops-backend
|
||||
```
|
||||
|
||||
如使用 Docker Compose,必须将宿主机挂载目录 bind mount 到容器内相同路径:
|
||||
如使用 Docker Compose,必须将宿主机挂载目录 bind mount 到**写 trigger 的容器**内相同路径(`hxops-backend`、`hxops-schedule-worker`、`hxops-shipping-worker`):
|
||||
|
||||
```yaml
|
||||
services:
|
||||
hxops-backend:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
# hxops-backend / hxops-schedule-worker / hxops-shipping-worker 均需:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
```
|
||||
|
||||
|
||||
@@ -1188,13 +1187,12 @@ sudo -u hxops touch /mnt/hxops-rpa-test-data/inventory_triggers/linux_write_test
|
||||
sudo -u hxops rm /mnt/hxops-rpa-test-data/inventory_triggers/linux_write_test.txt
|
||||
```
|
||||
|
||||
`.env.test` 必须指向 `/mnt/hxops-rpa-test-data`,scope 必须为 `hxops-rpa-test-data`。此外,`docker-compose.yml` 的 `hxops-backend` 必须将宿主机挂载目录 bind mount 到容器**同一路径**,否则容器内无法看到 RPA 文件:
|
||||
`.env.test` 必须指向 `/mnt/hxops-rpa-test-data`,scope 必须为 `hxops-rpa-test-data`。此外,`docker-compose.yml` 的 `hxops-backend`、`hxops-schedule-worker`、`hxops-shipping-worker` 都必须将宿主机挂载目录 bind mount 到容器**同一路径**(按需库存 trigger 由 shipping-worker 写入),否则容器内写到本地假目录、Windows 看不到:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
hxops-backend:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
# hxops-backend / hxops-schedule-worker / hxops-shipping-worker 均需:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||||
```
|
||||
|
||||
在测试服务器项目根目录启动并验证容器:
|
||||
@@ -1203,16 +1201,18 @@ services:
|
||||
export COMPOSE_ENV_BASENAME=.env.test
|
||||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||||
|
||||
docker compose --env-file .env.test up -d hxops-backend hxops-kb-worker
|
||||
docker compose --env-file .env.test up -d hxops-backend hxops-kb-worker hxops-shipping-worker hxops-schedule-worker
|
||||
docker compose --env-file .env.test ps
|
||||
docker compose --env-file .env.test exec -T hxops-backend \
|
||||
sh -lc 'test -d /mnt/hxops-rpa-test-data/inventory_triggers && ls -ld /mnt/hxops-rpa-test-data/inventory_triggers'
|
||||
docker compose --env-file .env.test exec -T hxops-shipping-worker \
|
||||
sh -lc 'mountpoint -q /mnt/hxops-rpa-test-data && ls -ld /mnt/hxops-rpa-test-data/inventory_triggers'
|
||||
```
|
||||
|
||||
若这是首次把 bind mount 加入 Compose,必须在变更后重新创建后端容器:
|
||||
若这是首次把 bind mount 加入 Compose,必须在变更后重新创建相关容器:
|
||||
|
||||
```bash
|
||||
docker compose --env-file .env.test up -d --force-recreate hxops-backend
|
||||
docker compose --env-file .env.test up -d --force-recreate hxops-backend hxops-shipping-worker hxops-schedule-worker
|
||||
```
|
||||
|
||||
|
||||
@@ -1485,13 +1485,12 @@ cd C:\HxopsRpaProd
|
||||
|
||||
#### 2. 启动正式 Linux 后端并验证共享
|
||||
|
||||
在正式服务器项目根目录执行。前提是 `/mnt/hxops-rpa-prod-data` 已经通过受控 SMB 凭据挂载,且 `docker-compose.yml` 已有如下 bind mount:
|
||||
在正式服务器项目根目录执行。前提是 `/mnt/hxops-rpa-prod-data` 已经通过受控 SMB 凭据挂载,且 `docker-compose.yml` 中 `hxops-backend` / `hxops-schedule-worker` / `hxops-shipping-worker` 均已 bind mount:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
hxops-backend:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
# 三个服务均需:
|
||||
volumes:
|
||||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||||
```
|
||||
|
||||
```bash
|
||||
@@ -1501,10 +1500,12 @@ test -d /mnt/hxops-rpa-prod-data/inventory_triggers
|
||||
export COMPOSE_ENV_BASENAME=.env.prod
|
||||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||||
|
||||
docker compose --env-file .env.prod up -d hxops-backend hxops-kb-worker
|
||||
docker compose --env-file .env.prod up -d hxops-backend hxops-kb-worker hxops-shipping-worker hxops-schedule-worker
|
||||
docker compose --env-file .env.prod ps
|
||||
docker compose --env-file .env.prod exec -T hxops-backend \
|
||||
sh -lc 'test -d /mnt/hxops-rpa-prod-data/inventory_triggers && ls -ld /mnt/hxops-rpa-prod-data/inventory_triggers'
|
||||
docker compose --env-file .env.prod exec -T hxops-shipping-worker \
|
||||
sh -lc 'mountpoint -q /mnt/hxops-rpa-prod-data && ls -ld /mnt/hxops-rpa-prod-data/inventory_triggers'
|
||||
cat /mnt/hxops-rpa-prod-data/inventory_triggers/.hxops_rpa_shared_mount
|
||||
```
|
||||
|
||||
@@ -1595,7 +1596,7 @@ $env:SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD = "<Crane密码>"
|
||||
python scripts/rpa/inventory/inventory_rpa_worker.py
|
||||
```
|
||||
|
||||
说明:`SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR` 是浏览器 Profile 根目录。库存导出默认开启会话复用:目录形如 `{connector}__{rpa_profile_code}_session`,跨任务保留登录态,默认 TTL 24 小时(`SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS`);关闭复用或上传任务仍使用 `run_*` 一次性子目录并在结束后清理。同一 WorkerRoot 不要并发启动第二个库存 Worker。
|
||||
说明:`SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR` 是浏览器 Profile 根目录。库存导出默认开启会话复用:目录形如 `{connector}__{rpa_profile_code}_session`,跨任务长期保留登录态,不以本地 TTL 强制清空;站点会话失效时由登录流程重登。关闭复用或上传任务仍使用 `run_*` 一次性子目录并在结束后清理。同一 WorkerRoot 不要并发启动第二个库存 Worker。
|
||||
|
||||
如果登录框默认识别不稳定,可覆盖登录 selector:
|
||||
|
||||
|
||||
@@ -20,7 +20,6 @@ Optional:
|
||||
SHIPPING_NOTICE_RPA_WORKER_SAMPLE_INVENTORY=path/to/sample.xlsx
|
||||
SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR=data/shipping_notice/freight_rpa/browser_profile
|
||||
SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE=true
|
||||
SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS=24
|
||||
# legacy alias: SHIPPING_NOTICE_RPA_WORKER_DSV_SESSION_REUSE
|
||||
SHIPPING_NOTICE_RPA_WORKER_HEADLESS=false
|
||||
SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL=msedge
|
||||
@@ -879,21 +878,6 @@ def _dsv_session_reuse_enabled() -> bool:
|
||||
return _session_reuse_enabled()
|
||||
|
||||
|
||||
def _session_reuse_ttl_seconds() -> int:
|
||||
"""
|
||||
返回持久 Profile 最长保留秒数。
|
||||
|
||||
核心逻辑:读取 `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS`,默认 24 小时;
|
||||
下限 1 小时,避免误配成 0 导致每次都重建。
|
||||
"""
|
||||
raw = _env("SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS", "24") or "24"
|
||||
try:
|
||||
hours = int(raw)
|
||||
except ValueError:
|
||||
hours = 24
|
||||
return max(1, hours) * 3600
|
||||
|
||||
|
||||
def _sanitize_profile_dir_segment(value: str) -> str:
|
||||
"""
|
||||
将 connector / RPA Profile 编码成可作目录名的安全片段。
|
||||
@@ -915,7 +899,7 @@ def _session_profile_dir(connector_type: str, profile_code: str = "") -> Path:
|
||||
|
||||
核心逻辑:目录形如 `{connector}__{profile_code}_session`,不同货代账号绝不共用;
|
||||
若仍存在历史 `dsv360_session` 且目标目录尚未创建、profile 为 default,则沿用旧目录
|
||||
以保留已有登录态,待 TTL 过期后自然重建到新命名。
|
||||
以保留已有登录态。本地不按时间强制清空;站点会话失效时由登录流程兜底重登。
|
||||
|
||||
Args:
|
||||
connector_type: Worker 连接器名(如 dsv360 / crane_footprint)。
|
||||
@@ -972,7 +956,6 @@ def _write_session_meta(
|
||||
"last_used_at": datetime.fromtimestamp(now, tz=UTC).isoformat(),
|
||||
"connector_type": connector_type,
|
||||
"profile_code": profile_code,
|
||||
"ttl_hours": max(1, _session_reuse_ttl_seconds() // 3600),
|
||||
}
|
||||
meta_path = profile_dir / _SESSION_META_FILENAME
|
||||
meta_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
@@ -985,10 +968,10 @@ def _prepare_reusable_session_profile(
|
||||
profile_code: str,
|
||||
) -> Path:
|
||||
"""
|
||||
准备可复用 Profile 目录:超 TTL 则清空重建,否则刷新 last_used。
|
||||
准备可复用 Profile 目录:确保存在并刷新 last_used,不按本地时间强制过期。
|
||||
|
||||
核心逻辑:以 meta 的 `created_at_unix` 起算;无 meta 时用目录 mtime。超过
|
||||
`SESSION_REUSE_TTL_HOURS`(默认 24h)则整目录删除后重建,迫使重新登录。
|
||||
核心逻辑:目录长期保留 Cookie/登录态;是否免登由货代站点会话是否仍有效决定,
|
||||
失效时各 connector 的 ensure_session / 登录流程会自动重登。
|
||||
|
||||
Args:
|
||||
profile_dir: 目标持久目录。
|
||||
@@ -996,47 +979,21 @@ def _prepare_reusable_session_profile(
|
||||
profile_code: RPA Profile 编码,写入 meta。
|
||||
|
||||
Returns:
|
||||
Path: 已确保存在且未过期的 profile 目录。
|
||||
Path: 已确保存在的 profile 目录。
|
||||
"""
|
||||
ttl_seconds = _session_reuse_ttl_seconds()
|
||||
now = time.time()
|
||||
if profile_dir.exists():
|
||||
meta = _read_session_meta(profile_dir)
|
||||
created_at = meta.get("created_at_unix")
|
||||
try:
|
||||
created_ts = float(created_at) if created_at is not None else profile_dir.stat().st_mtime
|
||||
except (TypeError, ValueError, OSError):
|
||||
created_ts = now
|
||||
age_seconds = max(0.0, now - created_ts)
|
||||
if age_seconds >= ttl_seconds:
|
||||
print(
|
||||
"[browser] session_reuse expired "
|
||||
f"ageHours={age_seconds / 3600:.1f} ttlHours={ttl_seconds / 3600:.0f} "
|
||||
f"dir={profile_dir.name!r}; recreating"
|
||||
)
|
||||
shutil.rmtree(profile_dir, ignore_errors=True)
|
||||
profile_dir.mkdir(parents=True, exist_ok=True)
|
||||
_write_session_meta(
|
||||
profile_dir,
|
||||
connector_type=connector_type,
|
||||
profile_code=profile_code,
|
||||
created_at_unix=now,
|
||||
)
|
||||
return profile_dir
|
||||
profile_dir.mkdir(parents=True, exist_ok=True)
|
||||
_write_session_meta(
|
||||
profile_dir,
|
||||
connector_type=connector_type,
|
||||
profile_code=profile_code,
|
||||
created_at_unix=created_ts,
|
||||
)
|
||||
return profile_dir
|
||||
profile_dir.mkdir(parents=True, exist_ok=True)
|
||||
meta = _read_session_meta(profile_dir)
|
||||
created_at = meta.get("created_at_unix")
|
||||
try:
|
||||
created_ts = float(created_at) if created_at is not None else now
|
||||
except (TypeError, ValueError):
|
||||
created_ts = now
|
||||
_write_session_meta(
|
||||
profile_dir,
|
||||
connector_type=connector_type,
|
||||
profile_code=profile_code,
|
||||
created_at_unix=now,
|
||||
created_at_unix=created_ts,
|
||||
)
|
||||
return profile_dir
|
||||
|
||||
@@ -1086,8 +1043,8 @@ def _open_browser(
|
||||
打开用于库存 / 上传 RPA 的浏览器上下文。
|
||||
|
||||
核心逻辑:库存导出在 `reuse_session=True` 且 `SESSION_REUSE` 开启时,使用
|
||||
`{connector}__{profile_code}_session` 持久目录并按 TTL 失效重建;关闭复用或上传
|
||||
任务则创建 `run_*` 一次性目录。会话失效仍由各 connector 登录流程兜底。
|
||||
`{connector}__{profile_code}_session` 持久目录长期保留登录态;关闭复用或上传
|
||||
任务则创建 `run_*` 一次性目录。站点会话失效时由各 connector 登录流程兜底。
|
||||
|
||||
Args:
|
||||
download_dir: 当前任务下载目录,浏览器 downloads_path 指向此处。
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
每个环境必须有独立 root、scope、共享目录、browser profile、日志和 DPAPI Secret 文件。
|
||||
DSV/Crane 登录地址、账号和密码来自 HXOPS trigger 的 credential_envelope,
|
||||
不从本机 Secret 文件读取。
|
||||
默认开启库存会话复用(SESSION_REUSE + 24h TTL);需要每次全新登录时传 -NoSessionReuse。
|
||||
默认开启库存会话复用(SESSION_REUSE,长期保留 Profile;站点会话失效才重登);
|
||||
需要每次全新登录时传 -NoSessionReuse。
|
||||
#>
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
@@ -65,7 +66,7 @@ foreach ($required in @("ToolApiKey", "CredentialKey")) {
|
||||
}
|
||||
}
|
||||
|
||||
# 库存 Profile 默认按 connector+rpa_profile 复用,TTL 24h;-NoSessionReuse 可关闭。
|
||||
# 库存 Profile 默认按 connector+rpa_profile 长期复用;-NoSessionReuse 可关闭。
|
||||
$sessionReuse = if ($NoSessionReuse) { "false" } else { "true" }
|
||||
if ($ReuseSession -or $ReuseDsvSession) {
|
||||
$sessionReuse = "true"
|
||||
@@ -82,7 +83,6 @@ $processVars = [ordered]@{
|
||||
SHIPPING_NOTICE_RPA_WORKER_MOCK = $(if ($Mock) { "true" } else { "false" })
|
||||
SHIPPING_NOTICE_RPA_WORKER_HEADLESS = $(if ($Headless) { "true" } else { "false" })
|
||||
SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE = $sessionReuse
|
||||
SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE_TTL_HOURS = "24"
|
||||
SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL = "msedge"
|
||||
SHIPPING_NOTICE_RPA_WORKER_POLL_SECONDS = "3"
|
||||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR = Join-Path $dataRoot "inventory_triggers"
|
||||
|
||||
Reference in new issue
Block a user