504 lines
28 KiB
Markdown
504 lines
28 KiB
Markdown
# 货代 RPA PoC
|
||
|
||
Hxops 与货代系统之间通过 **目录 + JSON 触发文件** 解耦,RPA 脚本在 Windows 货代终端上运行(或由本 PoC worker 在开发环境模拟)。
|
||
|
||
## 模块结构
|
||
|
||
| 文件 | 职责 |
|
||
|------|------|
|
||
| `freight_rpa_worker.py` | 轮询 trigger、打开浏览器、按 `connector_type` 分发、HTTP 回调(~900 行) |
|
||
| `rpa_common.py` | 环境变量、通用 Playwright 点击/填充工具 |
|
||
| `dsv_driver.py` | DSV 对外入口:`download_dsv360_inventory` |
|
||
| `dsv_auth.py` | DSV PingID 分步登录(表单/探测辅助) |
|
||
| `dsv_auth_login_flow.py` | DSV 自动登录主编排 `maybe_auto_login_dsv` |
|
||
| `dsv_session.py` | DSV portal 壳层检测、首页 ready、entry_url |
|
||
| `dsv_navigation.py` | DSV Reports 菜单 → 报表参数页 |
|
||
| `dsv_report_export.py` | DSV 填参、View Report、Excel 导出、batch 复用 |
|
||
| `dsv_debug.py` | DSV 调试探针与链路日志 |
|
||
| `dsv_report_parameters.py` | SSRS 参数控件填充(已有) |
|
||
| `crane_session.py` / `crane_report_export.py` | Crane 登录与异步导出 |
|
||
|
||
## 目录约定
|
||
|
||
| 环境变量 | 用途 |
|
||
|---------|------|
|
||
| `SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR` | hxops 写入 `inventory_export_{task_no}.json` 或 `inventory_export_batch_{run_no}.json`,RPA 读取后导库存 |
|
||
| `SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR` | RPA 落盘库存 Excel(`.xlsx`),hxops / worker 回调 ingest |
|
||
| `SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR` | hxops 写入 `upload_{task_no}.json` |
|
||
| `SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR` | hxops 复制待上传发货 Excel |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_ENABLED` | 真实上传浏览器自动化总开关,默认 `false` |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_CONFIRM_SUBMIT` | 最终提交二次确认,默认 `false` |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_URL` | 非 DSV 站点的已审核上传页面 URL(仅由受控环境变量注入) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_UPLOAD_ENTRY_URL` | DSV Portal 入口;Worker 复用登录态,优先 `My Applications > Upload Tool`,再退回 `Apps > Upload Tool` |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_FILE_SELECTOR` | 文件输入框 CSS selector |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_SUBMIT_SELECTOR` | 提交按钮 CSS selector |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_SUCCESS_SELECTOR` | 成功状态 CSS selector;未出现不得回调成功 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_RECEIPT_SELECTOR` | 可选的货代回执号 CSS selector |
|
||
| `SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` | 与后端 `SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` 一致;Worker 在 **inventory_triggers** 目录写哨兵 `.hxops_rpa_shared_mount` |
|
||
| `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 渠道或部署脚本安全注入 |
|
||
|
||
Windows test/prod 部署脚本:`scripts/rpa/set_rpa_worker_env.ps1`(详见 `docs/features/shipping-notice/technical.md` §1.5.5a)。
|
||
|
||
统一命名规则:共享目录与 scope 使用 `SHIPPING_NOTICE_RPA_SHARED_*`,Windows Worker
|
||
运行参数使用 `SHIPPING_NOTICE_RPA_WORKER_*`,Profile 凭据主密钥使用
|
||
`SHIPPING_NOTICE_RPA_CREDENTIAL_KEY`。历史 `FREIGHT_RPA_*`、`SHIPPING_RPA_*`
|
||
仅在迁移期兼容读取,不得用于新部署。
|
||
|
||
### DSV WMS Upload Tool
|
||
|
||
已确认 DSV 上传页为 `WMS Upload Tool > Upload`,页面有 `Browse` 文件选择入口和
|
||
`History` 查询入口。`Browse` 的真实 DOM 是 `<input id="file2" type="file">`,因此
|
||
DSV Worker 默认使用 `#file2`,也可用 `SHIPPING_NOTICE_RPA_WORKER_UPLOAD_FILE_SELECTOR`
|
||
覆盖。选择文件后出现的真实提交控件为 `<button class="btn btn-primary upload-button">`
|
||
(文案 `Process File`),DSV Worker 默认使用
|
||
`button.upload-button:has-text("Process File")`,亦可通过
|
||
`SHIPPING_NOTICE_RPA_WORKER_UPLOAD_SUBMIT_SELECTOR` 覆盖。DSV 成功不依赖 toast:Worker
|
||
切换到 `History`,仅在同名文件行同时存在绿色
|
||
`em.fa.fa-circle.success-color.status-icon` 和非空 `Reference #` 时回调成功,Reference
|
||
作为货代回执号。CRANE 上传路径尚未确认,Worker 会显式失败,绝不会误用 DSV 页面。
|
||
|
||
## 部署、启动与停止手册
|
||
|
||
本节说明库存 RPA 测试导出的三种运行方式:
|
||
|
||
| 场景 | Linux 后端 | Windows Worker | 适用范围 |
|
||
|------|------------|----------------|----------|
|
||
| Windows 本地联调 | 当前项目本地后端 | 当前项目 `scripts/rpa/` | 开发、调试、一次性测试导出;不使用 SMB 和任务计划 |
|
||
| Linux 测试环境 | `.env.test` + `/mnt/hxops-rpa-test-data` | `C:\HxopsRpaTest` | 测试库存站点的配置测试导出和验收 |
|
||
| Linux 正式环境 | `.env.prod` + `/mnt/hxops-rpa-prod-data` | `C:\HxopsRpaProd` | 已审核库存站点的正式库存导出 |
|
||
|
||
### 运行架构与职责
|
||
|
||
```text
|
||
Linux HXOPS 后端 Windows RPA Worker
|
||
---------------- ------------------
|
||
写 inventory_triggers/*.json --SMB--> 轮询 trigger,登录 DBS/Crane
|
||
接收 /rpa/* 回调 <--HTTPS-- 下载 Excel、写 inventory_drop、回调
|
||
读取 inventory_drop/*.xlsx --SMB--> 共享目录中的导出结果
|
||
```
|
||
|
||
- Linux 后端负责生成 trigger、接收回调、校验库存文件结构并入库;不在 Linux 上执行浏览器自动化。
|
||
- Windows Worker 负责浏览器自动化、读取 trigger、写入库存文件和发送心跳;它不直接连接 HXOPS 数据库。
|
||
- test 与 prod 必须使用不同的 SMB 共享、Windows 根目录、Linux 挂载目录、`SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID`、API Base、Tool API Key 和 RPA 凭据主密钥。
|
||
- Worker 进程启动后会在库存 trigger 目录写入 `.hxops_rpa_shared_mount`,并每 15 至 30 秒向 `/api/shipping-notice/rpa/heartbeat` 发送心跳。HXOPS 在 test/prod 写 trigger 前会检查 Worker 心跳、环境和共享 scope。
|
||
|
||
### 部署前检查
|
||
|
||
所有环境均需确认以下条件:
|
||
|
||
1. Windows RPA 终端可手工访问 DBS/Crane,网络、代理、证书、SSO/MFA 均已准备好。
|
||
2. Python 使用 3.12;`python --version` 和 `python -m pip --version` 可执行。
|
||
3. Windows 到对应 HXOPS 后端 `HXOPS_API_BASE` 的 HTTPS/HTTP 网络可达;Linux 后端到 Windows SMB 共享可读写。
|
||
4. RPA Profile 凭据主密钥 `SHIPPING_NOTICE_RPA_CREDENTIAL_KEY` 已通过 Jenkins、Secret 管理器或受控运维渠道提供,且 Windows Worker 与**同一环境**后端值一致。
|
||
5. `SHIPPING_NOTICE_TOOL_API_KEY` 与对应后端一致。不得把 Key、Fernet 主密钥、DSV/Crane 密码写入本 README、`.env` 提交内容、命令历史、截图或 Worker 日志。
|
||
6. 正式环境还必须确认库存站点已启用,并已绑定 published 库存文件解析方案;配置测试导出只能对停用站点发起,不能把测试文件当作正式库存快照。
|
||
|
||
### Windows 本地:直接在当前项目目录测试
|
||
|
||
这是最简方式,适用于在当前项目根目录运行本地后端和 RPA Worker。无需创建 `C:\HxopsRpaTest`、SMB 共享或计划任务;关闭 PowerShell 或按 `Ctrl+C` 即停止 Worker。
|
||
|
||
#### 1. 启动本地后端
|
||
|
||
在第一个 PowerShell 中,从项目根目录启动后端:
|
||
|
||
```powershell
|
||
cd backend
|
||
.\venv\Scripts\Activate.ps1
|
||
python run.py
|
||
```
|
||
|
||
默认 API Base 为 `http://127.0.0.1:5052`。若本地端口不是 5052,后续 Worker 的 `HXOPS_API_BASE` 必须同步修改。
|
||
|
||
#### 2. 安装 Worker 依赖
|
||
|
||
在第二个 PowerShell 中回到项目根目录。首次执行时安装 Python 库和 Playwright 浏览器:
|
||
|
||
```powershell
|
||
python -m pip install -r backend\requirements.txt
|
||
python -m pip install playwright
|
||
python -m playwright install chromium
|
||
```
|
||
|
||
如使用本机 Edge,可设置 `SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL=msedge`,无需让 Worker 下载 Chromium;仍建议保留 Playwright Python 包。
|
||
|
||
#### 3. 配置当前 PowerShell 会话并启动 Worker
|
||
|
||
Worker 不会自动加载 `.env.dev`,以下命令明确把四个目录指向当前项目的 `backend/data/shipping_notice/freight_rpa`。密钥使用占位符表示,实际值仅从本机受控 Secret 渠道注入。
|
||
|
||
```powershell
|
||
cd <项目根目录>
|
||
|
||
$repoRoot = (Get-Location).Path
|
||
$rpaDataRoot = Join-Path $repoRoot "backend\data\shipping_notice\freight_rpa"
|
||
|
||
$env:HXOPS_API_BASE = "http://127.0.0.1:5052"
|
||
$env:SHIPPING_NOTICE_TOOL_API_KEY = "<本地 Tool API Key>"
|
||
$env:SHIPPING_NOTICE_RPA_CREDENTIAL_KEY = "<本地 RPA 凭据主密钥>"
|
||
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT = "dev"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_MOCK = "false"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_HEADLESS = "false"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL = "msedge"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_POLL_SECONDS = "3"
|
||
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR = Join-Path $rpaDataRoot "inventory_triggers"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR = Join-Path $rpaDataRoot "inventory_drop"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR = Join-Path $rpaDataRoot "upload_triggers"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR = Join-Path $rpaDataRoot "upload_queue"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR = Join-Path $rpaDataRoot "browser_profile"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_LOG_FILE = Join-Path $rpaDataRoot "logs\worker.log"
|
||
|
||
python scripts\rpa\freight_rpa_worker.py
|
||
```
|
||
|
||
若只验证 trigger 扫描、文件流转与回调而不访问真实货代网站,可把 `SHIPPING_NOTICE_RPA_WORKER_MOCK` 设为 `true`。真实库存导出保持 `false`,并在本机受控环境中提供 DSV/Crane 账号或使用平台下发的加密 Profile 凭据。
|
||
|
||
#### 4. 验证与停止
|
||
|
||
启动成功会输出 `[freight_rpa_worker] started`。另开一个 PowerShell 检查实时日志、trigger 和导出目录:
|
||
|
||
```powershell
|
||
Get-Content "$rpaDataRoot\logs\worker.log" -Tail 100 -Wait
|
||
Get-ChildItem "$rpaDataRoot\inventory_triggers"
|
||
Get-ChildItem "$rpaDataRoot\inventory_drop"
|
||
```
|
||
|
||
从管理端发起一次测试导出;成功后 trigger 会被移入 `inventory_triggers\processed`,导出文件位于 `inventory_drop`,后端会收到 RPA 回调。测试结束时,在运行 Worker 的窗口按 `Ctrl+C`。若临时设置过敏感变量,关闭该 PowerShell 即可清除其进程环境,不要把值保存为用户级环境变量。
|
||
|
||
### Linux 测试环境:Windows Test Worker + SMB 共享
|
||
|
||
Linux 测试服务器上的 HXOPS 后端使用 `/mnt/hxops-rpa-test-data`,Windows 测试 Worker 使用 `C:\HxopsRpaTest\data`;两者必须是同一 SMB 共享 `hxops-rpa-test-data`。完整 Windows 目录、共享和权限创建步骤见 `docs/features/shipping-notice/technical.md` 的 §1.5.2 至 §1.5.5。
|
||
|
||
#### 1. Windows 测试 Worker 前置配置
|
||
|
||
在 Windows RPA 主机:
|
||
|
||
1. 创建 `C:\HxopsRpaTest\data\inventory_triggers`、`inventory_drop`、`upload_triggers`、`upload_queue`、`browser_profile`、`logs`,并将 `C:\HxopsRpaTest\data` 共享为 `hxops-rpa-test-data`。
|
||
2. 从当前仓库复制整个 `scripts\rpa` 目录到 `C:\HxopsRpaTest\scripts\rpa`,保证 Worker 引用的 driver、session、heartbeat 和公共模块版本一致;不要复制 `.env*`、旧浏览器 profile、旧 trigger、`processed` 或 `failed` 目录。
|
||
3. 安装依赖和浏览器运行时:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
python -m pip install --upgrade pip
|
||
python -m pip install -r <项目仓库>\backend\requirements.txt playwright
|
||
python -m playwright install chromium
|
||
```
|
||
|
||
4. 使用受控方式设置 test 专属变量。推荐执行仓库提供的脚本,它会交互读取 Fernet 主密钥,避免在命令行中传入该密钥:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment test -ApiBase "http://<测试后端地址>:<端口>" -ToolApiKey "<测试 Tool API Key>"
|
||
```
|
||
|
||
重新打开 PowerShell 后,必须确认以下值:
|
||
|
||
```powershell
|
||
echo $env:SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT # test
|
||
echo $env:SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID # hxops-rpa-test-data
|
||
echo $env:SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR
|
||
echo $env:HXOPS_API_BASE
|
||
```
|
||
|
||
#### 2. Linux 测试服务器挂载和容器检查
|
||
|
||
首次部署需安装 `cifs-utils`,将 Windows 测试共享挂载到 `/mnt/hxops-rpa-test-data`,并通过 `/etc/fstab` 配置开机自动挂载。凭据文件权限必须为 `600`,后端服务用户必须有读写权限。示例(请替换主机、账号与 uid/gid):
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install -y cifs-utils smbclient
|
||
sudo mkdir -p /mnt/hxops-rpa-test-data /etc/smbcredentials
|
||
sudo chmod 700 /etc/smbcredentials
|
||
|
||
# /etc/smbcredentials/hxops-rpa-test 内容仅在服务器本地维护:
|
||
# username=<Windows共享账号>
|
||
# password=<Windows共享密码>
|
||
# domain=WORKGROUP
|
||
sudo chmod 600 /etc/smbcredentials/hxops-rpa-test
|
||
|
||
sudo mount -t cifs //<Windows-RPA-IP>/hxops-rpa-test-data /mnt/hxops-rpa-test-data \
|
||
-o credentials=/etc/smbcredentials/hxops-rpa-test,vers=3.0,iocharset=utf8,uid=<hxops-uid>,gid=<hxops-gid>,file_mode=0775,dir_mode=0775
|
||
|
||
mountpoint -q /mnt/hxops-rpa-test-data
|
||
sudo -u hxops touch /mnt/hxops-rpa-test-data/inventory_triggers/linux_write_test.txt
|
||
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 文件:
|
||
|
||
```yaml
|
||
services:
|
||
hxops-backend:
|
||
volumes:
|
||
- /mnt/hxops-rpa-test-data:/mnt/hxops-rpa-test-data
|
||
```
|
||
|
||
在测试服务器项目根目录启动并验证容器:
|
||
|
||
```bash
|
||
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 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'
|
||
```
|
||
|
||
若这是首次把 bind mount 加入 Compose,必须在变更后重新创建后端容器:
|
||
|
||
```bash
|
||
docker compose --env-file .env.test up -d --force-recreate hxops-backend
|
||
```
|
||
|
||
#### 3. 启动、验证与停止测试 Worker
|
||
|
||
在 Windows RPA 主机打开 PowerShell:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
python scripts\rpa\freight_rpa_worker.py
|
||
```
|
||
|
||
应同时验证以下四项后,再在管理端对**停用的库存站点**发起“测试导出”:
|
||
|
||
```powershell
|
||
# Windows:scope 和 Worker 日志
|
||
Get-Content C:\HxopsRpaTest\data\inventory_triggers\.hxops_rpa_shared_mount
|
||
Get-Content C:\HxopsRpaTest\data\logs\worker.log -Tail 100 -Wait
|
||
```
|
||
|
||
```bash
|
||
# Linux:同一哨兵必须可见;管理端还应显示 test Worker 在线
|
||
cat /mnt/hxops-rpa-test-data/inventory_triggers/.hxops_rpa_shared_mount
|
||
```
|
||
|
||
哨兵文件应包含 `scope_id=hxops-rpa-test-data`。若管理端显示离线,先检查 Worker 的 `HXOPS_API_BASE`、测试 Tool API Key、Linux 防火墙、Redis 和 scope;不要通过改成 prod scope 绕过检查。
|
||
|
||
停止测试导出服务时,先停止发起新的测试导出并处理正在执行的 trigger,再在 Windows Worker 窗口按 `Ctrl+C`。如果需要停止 HXOPS 测试后端:
|
||
|
||
```bash
|
||
export COMPOSE_ENV_BASENAME=.env.test
|
||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||
docker compose --env-file .env.test stop hxops-kb-worker hxops-backend
|
||
```
|
||
|
||
仅暂停 RPA 时,只停止 Windows test Worker,保持 SMB 挂载和 HXOPS 后端运行,方便查看回调与排障日志。
|
||
|
||
### Linux 正式环境:Windows Prod Worker + SMB 共享
|
||
|
||
正式环境的流程与测试环境相同,但资源必须全部切换为 prod。**禁止**复制 test 的 `.env`、Windows 环境变量、浏览器 profile 或共享目录后直接上线。
|
||
|
||
| 配置项 | 测试 | 正式 |
|
||
|--------|------|------|
|
||
| Windows 根目录 | `C:\HxopsRpaTest` | `C:\HxopsRpaProd` |
|
||
| SMB 共享 | `hxops-rpa-test-data` | `hxops-rpa-prod-data` |
|
||
| Linux 挂载目录 | `/mnt/hxops-rpa-test-data` | `/mnt/hxops-rpa-prod-data` |
|
||
| 后端环境文件 | `.env.test` | `.env.prod` |
|
||
| Worker environment | `test` | `prod` |
|
||
| shared scope | `hxops-rpa-test-data` | `hxops-rpa-prod-data` |
|
||
|
||
#### 1. 正式前置条件
|
||
|
||
1. 已在测试环境完成真实登录、文件下载、回调、结构校验和库存站点测试导出验收。
|
||
2. Windows `C:\HxopsRpaProd\data` 已作为 `hxops-rpa-prod-data` 共享,且 Linux 正式服务器仅挂载该共享。
|
||
3. `.env.prod` 使用正式 API Key、`SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID=hxops-rpa-prod-data` 和 `/mnt/hxops-rpa-prod-data/*` 四个共享目录变量。
|
||
4. Windows Worker 使用 `-Environment prod` 设置用户级变量;其 Tool API Key 与 Fernet 主密钥只能来自正式 Secret 渠道。
|
||
5. 正式库存站点已启用并绑定 published 解析方案;真实上传 Driver 尚未验收时必须保持 `SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED=false`。
|
||
|
||
在 Windows RPA 主机设置并启动 prod Worker:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaProd
|
||
.\scripts\rpa\set_rpa_worker_env.ps1 -Environment prod -ApiBase "http://<正式后端地址>:<端口>" -ToolApiKey "<正式 Tool API Key>"
|
||
|
||
# 关闭并重新打开 PowerShell 后执行
|
||
cd C:\HxopsRpaProd
|
||
python scripts\rpa\freight_rpa_worker.py
|
||
```
|
||
|
||
#### 2. 启动正式 Linux 后端并验证共享
|
||
|
||
在正式服务器项目根目录执行。前提是 `/mnt/hxops-rpa-prod-data` 已经通过受控 SMB 凭据挂载,且 `docker-compose.yml` 已有如下 bind mount:
|
||
|
||
```yaml
|
||
services:
|
||
hxops-backend:
|
||
volumes:
|
||
- /mnt/hxops-rpa-prod-data:/mnt/hxops-rpa-prod-data
|
||
```
|
||
|
||
```bash
|
||
mountpoint -q /mnt/hxops-rpa-prod-data
|
||
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 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'
|
||
cat /mnt/hxops-rpa-prod-data/inventory_triggers/.hxops_rpa_shared_mount
|
||
```
|
||
|
||
哨兵内容必须包含 `scope_id=hxops-rpa-prod-data`,管理端 RPA Worker 状态页必须显示 prod Worker 在线,才可启用/恢复正式库存 RPA 排程。
|
||
|
||
#### 3. 正式停止与恢复顺序
|
||
|
||
计划维护或紧急停止时:
|
||
|
||
1. 先暂停正式库存 RPA 排程,并确认没有新的 trigger 被写入。
|
||
2. 等待当前 Worker 完成,或记录待人工处理的 trigger,再停止 Windows prod Worker(前台运行时按 `Ctrl+C`;若已配置计划任务则停止该任务)。
|
||
3. 仅在需要维护 HXOPS 时停止 Linux 容器:
|
||
|
||
```bash
|
||
export COMPOSE_ENV_BASENAME=.env.prod
|
||
export COMPOSE_SHARED_ENV_BASENAME=.env.shared
|
||
docker compose --env-file .env.prod stop hxops-kb-worker hxops-backend
|
||
```
|
||
|
||
恢复时严格按以下顺序:确认 SMB 挂载和容器内 bind mount正常 → 启动 Linux 后端与知识库 Worker → 启动 Windows prod Worker → 等待一次心跳和哨兵更新 → 管理端确认在线 → 恢复排程。不要用 test Worker 或 test 共享目录验证正式环境。
|
||
|
||
### 统一故障定位顺序
|
||
|
||
| 现象 | 先检查 | 常见处理 |
|
||
|------|--------|----------|
|
||
| 后端触发即提示 Worker 离线 | 管理端 Worker 状态、Worker 控制台、`HXOPS_API_BASE`、Tool API Key | 启动正确环境的 Windows Worker;检查网络和 Key,等待一个心跳周期 |
|
||
| `shared_sentinel_missing` 或 scope 不匹配 | `.hxops_rpa_shared_mount` 两端内容、`SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` | Windows/Linux 使用相同环境的 scope;确认 Worker 有权写 trigger 目录 |
|
||
| Linux 宿主机可见共享但容器不可见 | `docker compose exec ... ls /mnt/hxops-rpa-*-data` | 为 `hxops-backend` 添加同路径 bind mount 并重建容器 |
|
||
| trigger 长期未处理 | Windows Worker 是否运行、实际 trigger 路径、`processed`/`failed`、Worker 日志 | 确认 test/prod 目录一致;不要手工把 JSON 复制到另一环境 |
|
||
| 登录失败 | Windows 手工登录、MFA/SSO、代理、证书、Worker 日志 | 先执行 DSV/Crane smoke,必要时人工完成认证后再重试 |
|
||
| 回调失败 | Worker 日志中的 HTTP 状态、后端日志、防火墙 | 核对对应环境 API Base/Key;确认 Windows 能访问 `/api/shipping-notice/rpa/*` |
|
||
| 导出文件被结构门禁阻断 | 管理端结构差异、库存站点绑定的 published 方案 | 修订并发布解析方案后重新测试;不得在生产运行期自动猜列 |
|
||
|
||
## 开发环境 Mock
|
||
|
||
1. 在 `.env.dev` 配置目录与 Tool Key(见 `docs/features/shipping-notice/technical.md` §1)。
|
||
2. 启动后端。
|
||
3. 另开终端运行 PoC worker:
|
||
|
||
```powershell
|
||
$env:HXOPS_API_BASE = "http://127.0.0.1:5052"
|
||
$env:SHIPPING_NOTICE_TOOL_API_KEY = "<与 backend 一致>"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_MOCK = "true"
|
||
# 四个 SHIPPING_NOTICE_RPA_SHARED_* 目录与 backend 相同
|
||
python scripts/rpa/freight_rpa_worker.py
|
||
```
|
||
|
||
4. 在 Chat 中走 `ensure_inventory` / `upload_to_freight`,worker 会自动回调 `/api/shipping-notice/rpa/*`。
|
||
|
||
## 生产 RPA
|
||
|
||
`SHIPPING_NOTICE_RPA_WORKER_MOCK=false` 时 worker 会尝试用 Playwright 打开货代网站并下载库存 Excel。首次使用需要在 RPA 终端安装浏览器驱动:
|
||
|
||
```powershell
|
||
pip install playwright
|
||
python -m playwright install chromium
|
||
```
|
||
|
||
可以使用自动登录方式:
|
||
|
||
1. 自动登录:RPA worker 从本机环境变量读取账号密码,打开库存源 URL 后如检测到登录页则自动填写。
|
||
|
||
账号密码不要写入 trigger JSON,也不要提交到代码仓库。建议配置为 Windows 用户级环境变量或企业凭据管理器注入的进程环境变量。
|
||
|
||
若使用数据库中的 RPA Profile 密文,Windows Worker 还必须配置
|
||
`SHIPPING_NOTICE_RPA_CREDENTIAL_KEY`,且其值必须与对应 test/prod 后端完全一致。
|
||
该值只能从 Jenkins/Secret 管理器或受控运维渠道取得,不要写入仓库、脚本参数或日志。
|
||
|
||
```powershell
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_MOCK = "false"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_HEADLESS = "false"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR = "backend/data/shipping_notice/freight_rpa/browser_profile"
|
||
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME = "<DSV账号>"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD = "<DSV密码>"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_CRANE_USERNAME = "<Crane账号>"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD = "<Crane密码>"
|
||
|
||
python scripts/rpa/freight_rpa_worker.py
|
||
```
|
||
|
||
说明:`SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR` 现在仅作为“运行时 profile 根目录”。每次库存导出都会在该目录下自动创建一个全新的临时子目录,并在任务结束后清理,不会复用任何已有 profile。
|
||
|
||
如果登录框默认识别不稳定,可覆盖登录 selector:
|
||
|
||
| 环境变量 | 用途 |
|
||
|---------|------|
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_USERNAME_SELECTOR` | DSV 用户名输入框 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_PASSWORD_SELECTOR` | DSV 密码输入框 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_LOGIN_BUTTON_SELECTOR` | DSV 登录按钮 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_USERNAME_SELECTOR` | Crane 用户名(默认 `input[placeholder="Enter user name..."]`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD_SELECTOR` | Crane 密码(默认 `input[placeholder="Enter password..."]`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_LOGIN_BUTTON_SELECTOR` | Crane 登录按钮(默认 `button.button.is-primary:has-text("Sign in")`;填写凭据前为 disabled) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_DASHBOARD_REPORT_SELECTOR` | Crane 首页 Queries 区 On Hand Details 卡片(默认 `a.box[href="/on-hand-details-query"]`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_NAV_QUERIES_SELECTOR` | Crane 顶部 Queries 下拉触发(默认 `a.navbar-link:has-text("Queries")`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_NAV_REPORT_SELECTOR` | Crane 顶部菜单 On Hand Details 项(默认 `a.navbar-item:has-text("On Hand Details")`) |
|
||
|
||
Crane 登录烟测(仅验证 User Name / Password / Sign in):
|
||
|
||
```powershell
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_CRANE_USERNAME = "<Crane账号>"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_CRANE_PASSWORD = "<Crane密码>"
|
||
backend\venv\Scripts\python.exe scripts\rpa\crane_login_smoke.py
|
||
```
|
||
|
||
若页面出现 MFA、验证码或 SSO 二次确认,worker 会失败并回调错误,需要人工处理后重试。
|
||
|
||
库存 trigger 需要包含 `rpa_inventory_source`,worker 会按 `connector_type` 分发:
|
||
|
||
| connector_type | Driver |
|
||
|-------------|--------|
|
||
| `dbs` | 打开 DSV 360,进入 Reports / Program / Inventory 报表并下载 |
|
||
| `crane` | 打开 On Hand Details Query → Run Report → Schedule Report → Scheduled Reports 下载 |
|
||
|
||
### 批量库存导出(`inventory_export_batch`)
|
||
|
||
定时排程或后台预热会写入 `inventory_export_batch_{run_no}.json`,字段示例:
|
||
|
||
```json
|
||
{
|
||
"action": "inventory_export_batch",
|
||
"run_no": "RUN20260707030000",
|
||
"trigger_type": "scheduled",
|
||
"inventory_sources": [{ "source_code": "DBS_DUB", "connector_type": "dbs", "worker_connector_type": "dsv360", "rpa_profile_code": "DBS_EU", "warehouse_id": "DUB" }]
|
||
}
|
||
```
|
||
|
||
Worker 按 `worker_connector_type + rpa_profile_code` 分组,确保同一 connector 的不同账号互不复用登录态。后端从数据库 Profile 写入 `login_url` 和密文凭据;报表导航、控件定位、下载方式与等待策略均由 DBS/Crane Driver 模板维护,不从 Profile 或 Source 传入。worker 仅在执行该 Profile 时解密。DBS 固定 Source 必须带 `warehouse_id`;Crane 动态 Source 不预配置 Warehouse,后端以库存 Excel 的 `reservation_id + part_number` 解析 Warehouse。完成后回调 `POST /api/shipping-notice/rpa/inventory-batch-ready`。
|
||
|
||
页面控件如果默认可见文本无法稳定点击,可通过环境变量覆盖 selector:
|
||
|
||
| 环境变量 | 用途 |
|
||
|---------|------|
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_REPORTS_SELECTOR` | DSV Reports 菜单 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_PROGRAM_SELECTOR` | DSV Program 项 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_REPORT_GROUP_SELECTOR` | DSV Inventory 分组 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_REPORT_NAME_SELECTOR` | DSV On Hand Inventory by Location |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_OWNER_SELECTOR` | DSV Owner 输入框 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_DSV_EXPORT_SELECTOR` | DSV 导出按钮 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_RUN_SELECTOR` | Crane Run Report 按钮(默认 `button.button.is-primary:has-text("Run Report")`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULE_REPORT_SELECTOR` | Crane Schedule Report 按钮(默认 `button:has-text("Schedule Report")`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULE_DESCRIPTION_SELECTOR` | Schedule Report 弹窗 Description 输入框 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULE_OK_SELECTOR` | Schedule Report 弹窗 Ok 按钮 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULE_DESCRIPTION` | 覆盖自动生成的 Description |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULE_DESCRIPTION_HEX_LEN` | Description 十六进制长度(默认 **12**,范围 8~32) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORTS_SELECTOR` | Crane Scheduled Reports 入口(默认 `a.has-link[href="#/scheduled-reports"]`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_DOWNLOAD_SELECTOR` | Scheduled Reports 列表中的下载按钮 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SUBMISSION_COMPLETE_WAIT_MS` | 等待 Report Submitted 提示消失的最长时间(默认 120000) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_TABLE_SELECTOR` | Scheduled Reports 表格行选择器 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORT_NAME` | Scheduled Reports 目标文件名(Name 列,默认 `OnHandDetails.xlsx`) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORT_WAIT_MS` | 等待列表行 Submitted→Download 的最长时间(默认 300000) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORT_POLL_SECONDS` | 轮询 Scheduled Reports 间隔秒数(默认 3) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_SCHEDULED_REPORTS_URL` | Scheduled Reports 页面 URL(默认由当前站点 hash 推导) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_OWNER_SELECTOR` | Crane Owner 下拉/输入(可选) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_PROJECT_SELECTOR` | Crane Project 下拉/输入(可选) |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_CRANE_EXPORT_SELECTOR` | Crane 直出下载按钮(仅 `download_mode: file_download` 时使用) |
|
||
|
||
发货 Excel 上传仍保留原 PoC 行为:`SHIPPING_NOTICE_RPA_WORKER_MOCK=false` 时不会自动上传,需后续实现上传 Driver 或接入 UiPath / Power Automate。
|
||
|
||
回调 Header:`X-Shipping-Tool-Key: <SHIPPING_NOTICE_TOOL_API_KEY>`。
|