下发 preparation capabilities 并与右卡展示同源授权; 库存 RPA 运行态请求内 memo;取消会话 Profile 本地 TTL; 并为 shipping-worker 挂载共享盘。 Co-authored-by: Cursor <cursoragent@cursor.com>
1690 lines
86 KiB
Markdown
1690 lines
86 KiB
Markdown
# 货代 RPA PoC
|
||
|
||
Hxops 与货代系统之间通过 **目录 + JSON 触发文件** 解耦,RPA 脚本在 Windows 货代终端上运行(或由本 PoC worker 在开发环境模拟)。
|
||
|
||
## 收料 RPA(独立 Worker)
|
||
|
||
收料使用 `freight_receipt_rpa_worker.py` 与 `start_receipt_rpa_worker.ps1`,仅消费 `SHIPPING_NOTICE_RPA_SHARED_RECEIPT_TRIGGER_DIR`,仅写 `SHIPPING_NOTICE_RPA_SHARED_RECEIPT_DROP_DIR`,只回调 `/api/shipping-notice/rpa/receipt-batch-ready`。它可以与库存 Worker 同时运行,但四类目录(trigger、drop、browser profile、日志)绝不能共享。
|
||
|
||
启动示例:
|
||
|
||
```powershell
|
||
.\scripts\rpa\receipt\start_receipt_worker.ps1 -Environment test -ApiBase "http://<backend>" -WorkerRoot "C:\HxopsRpaTest" -SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
收料 Worker 在同一排程内按 Profile 登录一次后串行处理单元;DBS 每个 Warehouse 导出一个文件且不传报表日期,Crane 无论 Profile 登录地址保存为门户首页还是其他页面,都会强制进入 `#/inbound-details-query`,不填写 Warehouse 或查询条件,直接使用页面默认条件生成/等待唯一 Description 对应的 `Inbound Details` 文件。不要把收料 trigger 手工复制到库存目录或反向复制。
|
||
|
||
## 先看这里:文档阅读路径
|
||
|
||
请按实际场景只走一条路径,不要把“首次部署”和“部署后日常运维”重复执行:
|
||
|
||
|
||
| 你现在要做什么 | 阅读章节 | 说明 |
|
||
| ---------------------------- | ------------------ | --------------------------------------------- |
|
||
| 第一次部署 RPA 主机 | **一、首次完整部署主线** | 按 Windows → SMB → Ubuntu → 后端 → 后台任务 → 验收顺序执行 |
|
||
| 已部署,只是启动/停止/查日志 | **二、部署后日常运维** | 不要重新创建共享和重新写环境变量 |
|
||
| 只做开发机联调 | **三、开发/模拟** | 不使用 test/prod SMB、Key 或计划任务 |
|
||
| 调整 DSV/Crane 页面控件或批量 trigger | **四、Driver 与业务参考** | 只改参数或按场景联调 |
|
||
|
||
|
||
**唯一部署主线**是后文“首次完整部署”章节;后面的 test/prod/local 章节是已完成首次部署后的速查和联调参考,不是另一套部署流程。
|
||
|
||
## 模块结构
|
||
|
||
|
||
| 目录 / 文件 | 职责 |
|
||
| ----------------------------------- | ------------------------------------------------------ |
|
||
| `common/` | **跨域通用**(与流程、连接器无关) |
|
||
| `common/rpa_common.py` | 环境变量、通用 Playwright 点击/填充工具 |
|
||
| `common/download_artifact_fallback.py` | 浏览器已落盘但 Playwright 未返回 Download 时的下载适配器 |
|
||
| `common/rpa_heartbeat.py` | 向后端上报心跳 + 共享目录哨兵(原 `freight_rpa_heartbeat.py`) |
|
||
| `common/new_rpa_worker_secret.ps1` | 生成当前用户可解密的 DPAPI Secret 文件 |
|
||
| `connectors/` | **登录网站 / 报表系统自动化**(库存与收料共用) |
|
||
| `connectors/dsv/` | DSV 360 连接器:`dsv_driver` / `dsv_auth` / `dsv_auth_login_flow` / `dsv_session` / `dsv_navigation` / `dsv_report_export` / `dsv_debug` / `dsv_report_parameters` / `dsv_login_smoke` / `dsv_export_timeout_diagnostics` |
|
||
| `connectors/crane/` | Crane FootPrint 连接器:`crane_session` / `crane_navigation` / `crane_report_export` / `crane_login_smoke` |
|
||
| `inventory/` | **库存(发货通知)RPA 流程编排** |
|
||
| `inventory/inventory_rpa_worker.py` | 库存 Worker:轮询 trigger、按 `connector_type` 分发、HTTP 回调(原 `freight_rpa_worker.py`) |
|
||
| `inventory/run_inventory_batch_trigger.py` | 手工执行一条数据库生成的 inventory batch trigger |
|
||
| `inventory/start_inventory_worker.ps1` | 启动库存 Worker 的 PowerShell 启动脚本(原 `start_rpa_worker.ps1`) |
|
||
| `inventory/register_inventory_worker_tasks.ps1` | 注册 test/prod 库存 Worker 后台计划任务(headless + hidden) |
|
||
| `receipt/` | **收料 RPA 流程编排** |
|
||
| `receipt/receipt_rpa_worker.py` | 独立收料 Worker:仅消费 receipt trigger、专用目录与回调(原 `freight_receipt_rpa_worker.py`) |
|
||
| `receipt/receipt_crane_export.py` | Crane 收料报表导出适配器(强制 Inbound Details) |
|
||
| `receipt/receipt_dbs_export.py` | DBS 收料报表导出适配器(按 Warehouse) |
|
||
| `receipt/start_receipt_worker.ps1` | 启动收料 Worker 的 PowerShell 启动脚本(原 `start_receipt_rpa_worker.ps1`) |
|
||
|
||
|
||
|
||
|
||
## 目录约定
|
||
|
||
|
||
| 环境变量 | 用途 |
|
||
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ |
|
||
| `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 渠道或部署脚本安全注入 |
|
||
| `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE` | 库存导出是否复用浏览器登录 Profile,默认 `true`;上传任务不受影响;站点会话失效时自动重登 |
|
||
|
||
|
||
新部署只使用 `new_rpa_worker_secret.ps1` + `start_rpa_worker.ps1`:Secret 由 DPAPI 保护,环境变量只注入对应 Worker 子进程。
|
||
|
||
统一命名规则:共享目录与 scope 使用 `SHIPPING_NOTICE_RPA_SHARED_*`,Windows Worker
|
||
运行参数使用 `SHIPPING_NOTICE_RPA_WORKER_*`,Profile 凭据主密钥使用
|
||
`SHIPPING_NOTICE_RPA_CREDENTIAL_KEY`。部署文档和启动脚本只使用上述规范命名。
|
||
|
||
DSV/Crane 库存导出默认复用浏览器 Profile(按 `connector + rpa_profile_code` 隔离目录,
|
||
长期保留)。同一 WorkerRoot 只跑一个 Worker,任务串行消费;是否免登取决于货代站点会话
|
||
是否仍有效,失效时回落现有登录流程。上传任务保持一次性 Profile。若需强制每次全新登录,
|
||
启动时加 `-NoSessionReuse`。旧参数 `-ReuseDsvSession` / `SHIPPING_NOTICE_RPA_WORKER_DSV_SESSION_REUSE`
|
||
仍兼容,但请改用 `SHIPPING_NOTICE_RPA_WORKER_SESSION_REUSE`。
|
||
|
||
### 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`,并由**独立守护心跳线程**按 `SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_SECONDS`(默认 20s)周期向 `/api/shipping-notice/rpa/heartbeat` 发送心跳——心跳与批量导出解耦,长任务执行期间不会停发、后端不会误判离线。HXOPS 在 test/prod 写 trigger 前会检查 Worker 心跳、环境和共享 scope。
|
||
- 定时排程批量导出(`trigger_type=scheduled`)每完成一个站点,Worker 会调用 `/api/shipping-notice/rpa/inventory-batch-progress` 上报累计成功/跳过/失败笔数,管理端"库存RPA·执行记录"可实时看到当前 run 的进度;最终笔数仍以 `inventory-batch-ready` 回调为准。
|
||
|
||
|
||
|
||
### 部署前检查
|
||
|
||
所有环境均需确认以下条件:
|
||
|
||
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 与**同一环境**后端值一致。DSV/Crane 账号密码只在 HXOPS RPA Profile 配置,不在 Windows 部署时重复录入。
|
||
5. `SHIPPING_NOTICE_TOOL_API_KEY` 与对应后端一致。不得把 Key、Fernet 主密钥、DSV/Crane 密码写入本 README、`.env` 提交内容、命令历史、截图或 Worker 日志。
|
||
6. 正式环境还必须确认库存站点已启用,并已绑定 published 库存文件解析方案;配置测试导出只能对停用站点发起,不能把测试文件当作正式库存快照。
|
||
|
||
|
||
|
||
## 小白部署指南:一台 Windows 主机同时运行 test/prod
|
||
|
||
> 这是首次部署的**唯一执行入口**。按步骤 0 到步骤 12 顺序执行,不要跳步。
|
||
>
|
||
> 当前方案是:一台 Windows 主机、一个 Windows 用户、一个交互式桌面,test/prod 各一个 Worker。两个 Worker 的环境变量只在各自进程内生效。
|
||
|
||
|
||
|
||
### 步骤 0:先准备这些信息
|
||
|
||
不要把密钥或密码发到群里、写进 README 或保存到 PowerShell 脚本。请先从运维/Secret 管理器获得:
|
||
|
||
|
||
| 信息 | test | prod |
|
||
| ------------------------------------ | --------------------------- | --------------------------- |
|
||
| HXOPS 后端地址 | `http://<test地址>:<端口>` | `http://<prod地址>:<端口>` |
|
||
| `SHIPPING_NOTICE_TOOL_API_KEY` | test 专用 | prod 专用 |
|
||
| `SHIPPING_NOTICE_RPA_CREDENTIAL_KEY` | 必须与 test 后端一致 | 必须与 prod 后端一致 |
|
||
| DSV/Crane 登录凭据 | 在 HXOPS 管理端配置对应 RPA Profile | 在 HXOPS 管理端配置对应 RPA Profile |
|
||
| Ubuntu 后端 IP | 实际 test IP | 实际 prod IP |
|
||
| Windows RPA IP | 同一台 Windows 主机 IP | 同左 |
|
||
|
||
|
||
以下示例默认:
|
||
|
||
```text
|
||
Windows test 根目录:C:\HxopsRpaTest
|
||
Windows prod 根目录:C:\HxopsRpaProd
|
||
Secret 目录:C:\HxopsRpaSecrets
|
||
test 共享名:hxops-rpa-test-data
|
||
prod 共享名:hxops-rpa-prod-data
|
||
Ubuntu test 挂载点:/mnt/hxops-rpa-test-data
|
||
Ubuntu prod 挂载点:/mnt/hxops-rpa-prod-data
|
||
```
|
||
|
||
|
||
|
||
### 步骤 1:用 RPA 专用账号登录 Windows
|
||
|
||
1. 使用已创建的 RPA Windows 账号登录。
|
||
2. 后续的 Secret 文件和任务计划都必须在这个账号下创建。
|
||
3. Worker 使用可见浏览器,该账号登录后才会运行 test/prod 任务。
|
||
|
||
|
||
|
||
### 步骤 2:安装 Python 3.12
|
||
|
||
1. 在 Windows 安装 Python 3.12。
|
||
2. 安装时勾选 `Add Python to PATH`。
|
||
3. 打开 PowerShell,执行:
|
||
|
||
```powershell
|
||
python --version
|
||
python -m pip --version
|
||
where.exe python
|
||
```
|
||
|
||
预期:
|
||
|
||
- `python --version` 显示 `Python 3.12.x`。
|
||
- `where.exe python` 返回 Python 绝对路径。把第一个真实 Python 3.12 路径记下,后面创建任务时会用到。
|
||
|
||
|
||
|
||
### 步骤 3:创建 test/prod 目录
|
||
|
||
在 Windows PowerShell 中整段复制执行:
|
||
|
||
```powershell
|
||
foreach ($root in @("C:\HxopsRpaTest", "C:\HxopsRpaProd")) {
|
||
New-Item -ItemType Directory -Force "$root\scripts\rpa" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\inventory_triggers" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\inventory_drop" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\upload_triggers" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\upload_queue" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\browser_profile" | Out-Null
|
||
New-Item -ItemType Directory -Force "$root\data\logs" | Out-Null
|
||
}
|
||
New-Item -ItemType Directory -Force "C:\HxopsRpaSecrets" | Out-Null
|
||
```
|
||
|
||
验证:
|
||
|
||
```powershell
|
||
Get-ChildItem C:\HxopsRpaTest\data
|
||
Get-ChildItem C:\HxopsRpaProd\data
|
||
```
|
||
|
||
应看到 `inventory_triggers`、`inventory_drop`、`upload_triggers`、`upload_queue`、`browser_profile`、`logs`。
|
||
|
||
### 步骤 4:复制 RPA 程序
|
||
|
||
从已审核、准备部署的 HXOPS 仓库版本中,把**整个** `scripts\rpa` 目录复制两份:
|
||
|
||
```text
|
||
<HXOPS仓库>\scripts\rpa -> C:\HxopsRpaTest\scripts\rpa
|
||
<HXOPS仓库>\scripts\rpa -> C:\HxopsRpaProd\scripts\rpa
|
||
```
|
||
|
||
验证关键文件:
|
||
|
||
```powershell
|
||
Test-Path C:\HxopsRpaTest\scripts\rpa\inventory\inventory_rpa_worker.py
|
||
Test-Path C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1
|
||
Test-Path C:\HxopsRpaTest\scripts\rpa\common\new_rpa_worker_secret.ps1
|
||
Test-Path C:\HxopsRpaProd\scripts\rpa\inventory\inventory_rpa_worker.py
|
||
Test-Path C:\HxopsRpaProd\scripts\rpa\inventory\start_inventory_worker.ps1
|
||
```
|
||
|
||
每一行都应返回 `True`。不要复制 `.env*`、旧 trigger、旧 `processed/failed`、旧浏览器 profile 和真实密钥文件。
|
||
|
||
**重要:两个** `.ps1` **文件必须保持 UTF-8 BOM 编码。** Windows PowerShell 5.1 如果将无 BOM 的 UTF-8 脚本按本地编码读取,会出现“字符串缺少终止符”、中文变乱等错误。请从仓库原样复制脚本,不要用不支持 BOM 的编辑器另存为 ANSI。
|
||
|
||
Windows PowerShell 5.1 如仍已经报中文变乱,请在仓库根目录用 PowerShell 重新保存为 UTF-8 BOM:
|
||
|
||
```powershell
|
||
$utf8Bom = New-Object System.Text.UTF8Encoding($true)
|
||
foreach ($path in @(
|
||
"C:\HxopsRpaTest\scripts\rpa\common\new_rpa_worker_secret.ps1",
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1",
|
||
"C:\HxopsRpaProd\scripts\rpa\common\new_rpa_worker_secret.ps1",
|
||
"C:\HxopsRpaProd\scripts\rpa\inventory\start_inventory_worker.ps1"
|
||
)) {
|
||
$text = [System.IO.File]::ReadAllText($path, [System.Text.UTF8Encoding]::new($false))
|
||
[System.IO.File]::WriteAllText($path, $text, $utf8Bom)
|
||
}
|
||
```
|
||
|
||
然后重新执行本节的 `powershell.exe -NoProfile -ExecutionPolicy Bypass -File ...` 命令。
|
||
|
||
### 步骤 5:安装 Worker 依赖
|
||
|
||
在 PowerShell 中执行:
|
||
|
||
```powershell
|
||
python -m pip install --upgrade pip
|
||
python -m pip install playwright openpyxl pyyaml cryptography
|
||
python -m playwright install chromium
|
||
```
|
||
|
||
验证:
|
||
|
||
```powershell
|
||
python -c "import playwright, openpyxl, yaml, cryptography; print('RPA dependencies OK')"
|
||
```
|
||
|
||
应输出 `RPA dependencies OK`。
|
||
|
||
### 步骤 6:创建 Windows SMB 共享
|
||
|
||
如果 Ubuntu 后端需要通过 SMB 读写 Windows 目录,建议创建专用共享账号:
|
||
|
||
```powershell
|
||
net user hxops_share "<请换成强密码>" /add
|
||
```
|
||
|
||
然后使用 Windows 图形界面配置:
|
||
|
||
1. 右键 `C:\HxopsRpaTest\data` → “属性”。
|
||
2. 点“共享” → “高级共享”。
|
||
3. 勾选“共享此文件夹”。
|
||
4. 共享名填写 `hxops-rpa-test-data`。
|
||
5. “权限”中给 `hxops_share` 读取、更改、完全控制。
|
||
6. 回到属性的“安全”页,给 `hxops_share` 读取、写入、修改权限。
|
||
7. 对 `C:\HxopsRpaProd\data` 重复上述操作,共享名填 `hxops-rpa-prod-data`。
|
||
|
||
记下 Windows RPA 主机 IP,Ubuntu 将访问:
|
||
|
||
```text
|
||
//<Windows-RPA-IP>/hxops-rpa-test-data
|
||
//<Windows-RPA-IP>/hxops-rpa-prod-data
|
||
```
|
||
|
||
|
||
|
||
### 步骤 7:在 Windows 创建 test/prod Secret
|
||
|
||
Secret 必须用最终运行任务的同一 Windows 账号创建。
|
||
|
||
#### 如果提示“在此系统上禁止运行脚本”
|
||
|
||
这是 Windows PowerShell 执行策略拦截 `.ps1` 文件,不是 RPA 脚本业务错误。推荐仅对当前这一次运行临时放行,不修改系统或用户的永久执行策略:
|
||
|
||
```powershell
|
||
powershell.exe `
|
||
-NoProfile `
|
||
-ExecutionPolicy Bypass `
|
||
-File "C:\HxopsTest\scripts\rpa\common\new_rpa_worker_secret.ps1" `
|
||
-Environment test
|
||
```
|
||
|
||
正式环境将路径和环境替换为:
|
||
|
||
```powershell
|
||
powershell.exe `
|
||
-NoProfile `
|
||
-ExecutionPolicy Bypass `
|
||
-File "C:\HxopsRpaProd\scripts\rpa\common\new_rpa_worker_secret.ps1" `
|
||
-Environment prod
|
||
```
|
||
|
||
如果仍然提示文件被阻止,先解除从网络或压缩包复制来的文件标记,再重试:
|
||
|
||
```powershell
|
||
Unblock-File "C:\HxopsRpaTest\scripts\rpa\common\new_rpa_worker_secret.ps1"
|
||
Unblock-File "C:\HxopsRpaProd\scripts\rpa\common\new_rpa_worker_secret.ps1"
|
||
```
|
||
|
||
也可以只对当前 PowerShell 窗口临时放行:
|
||
|
||
```powershell
|
||
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
|
||
```
|
||
|
||
然后再执行:
|
||
|
||
```powershell
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment test
|
||
```
|
||
|
||
不要执行 `Set-ExecutionPolicy Unrestricted` 或 `Set-ExecutionPolicy RemoteSigned -Scope LocalMachine` 等永久放宽系统策略的命令。后台任务自身已使用 `-ExecutionPolicy Bypass`,不需要修改系统策略。
|
||
|
||
创建 test Secret:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment test
|
||
```
|
||
|
||
按提示输入两项:
|
||
|
||
1. test `SHIPPING_NOTICE_TOOL_API_KEY`。
|
||
2. test `SHIPPING_NOTICE_RPA_CREDENTIAL_KEY`。
|
||
|
||
DSV/Crane 登录地址、账号和密码不在 Windows 部署时输入:它们必须先在 HXOPS 管理端的 RPA Profile 中配置并保存。后端会将加密 `credential_envelope` 放入 trigger,Worker 只使用 `SHIPPING_NOTICE_RPA_CREDENTIAL_KEY` 解密。
|
||
|
||
创建 prod Secret:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaProd
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment prod
|
||
```
|
||
|
||
这次必须输入 prod 的 Tool API Key 和 RPA Credential Key,不要复制 test 值。DSV/Crane 凭据仍在 HXOPS prod RPA Profile 中配置。
|
||
|
||
验证文件存在:
|
||
|
||
```powershell
|
||
Test-Path C:\HxopsRpaSecrets\test.xml
|
||
Test-Path C:\HxopsRpaSecrets\prod.xml
|
||
```
|
||
|
||
应都返回 `True`。这两个 XML 由当前 Windows 用户 DPAPI 保护,不得复制到 Git、SMB 共享或其他账号。
|
||
|
||
### 步骤 8:在 Ubuntu 挂载两个 SMB 共享
|
||
|
||
以下命令在 Ubuntu HXOPS 主机执行。
|
||
|
||
1. 安装工具:
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install -y cifs-utils smbclient
|
||
```
|
||
|
||
1. 确认能看到 Windows 共享:
|
||
|
||
```bash
|
||
smbclient -L //<Windows-RPA-IP> -U hxops_share
|
||
```
|
||
|
||
输入 Windows 共享账号密码后,应看到 `hxops-rpa-test-data` 和 `hxops-rpa-prod-data`。
|
||
|
||
1. 创建挂载点和凭据目录:
|
||
|
||
```bash
|
||
sudo mkdir -p /mnt/hxops-rpa-test-data /mnt/hxops-rpa-prod-data /etc/smbcredentials
|
||
sudo chmod 700 /etc/smbcredentials
|
||
sudo nano /etc/smbcredentials/hxops-rpa
|
||
```
|
||
|
||
1. 在凭据文件中填写:
|
||
|
||
```ini
|
||
username=hxops_share
|
||
password=<Windows共享强密码>
|
||
domain=WORKGROUP
|
||
```
|
||
|
||
保存后执行:
|
||
|
||
```bash
|
||
sudo chmod 600 /etc/smbcredentials/hxops-rpa
|
||
id hxops
|
||
```
|
||
|
||
1. 记下 `id hxops` 返回的 uid/gid。下面以 `1001/1001` 为例,必须换成实际值:
|
||
|
||
```bash
|
||
sudo mount -t cifs //<Windows-RPA-IP>/hxops-rpa-test-data /mnt/hxops-rpa-test-data \
|
||
-o credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775
|
||
|
||
sudo mount -t cifs //<Windows-RPA-IP>/hxops-rpa-prod-data /mnt/hxops-rpa-prod-data \
|
||
-o credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775
|
||
```
|
||
|
||
1. 验证 Ubuntu 能写,Windows 能看到:
|
||
|
||
```bash
|
||
sudo -u hxops touch /mnt/hxops-rpa-test-data/inventory_triggers/from_ubuntu_test.txt
|
||
sudo -u hxops touch /mnt/hxops-rpa-prod-data/inventory_triggers/from_ubuntu_prod.txt
|
||
```
|
||
|
||
在 Windows 检查:
|
||
|
||
```powershell
|
||
Test-Path C:\HxopsRpaTest\data\inventory_triggers\from_ubuntu_test.txt
|
||
Test-Path C:\HxopsRpaProd\data\inventory_triggers\from_ubuntu_prod.txt
|
||
```
|
||
|
||
都应返回 `True`。验证完成后在 Ubuntu 删除测试文件:
|
||
|
||
```bash
|
||
sudo -u hxops rm /mnt/hxops-rpa-test-data/inventory_triggers/from_ubuntu_test.txt
|
||
sudo -u hxops rm /mnt/hxops-rpa-prod-data/inventory_triggers/from_ubuntu_prod.txt
|
||
```
|
||
|
||
1. 将两条挂载写入 `/etc/fstab`,确保 Ubuntu 重启后仍自动挂载:
|
||
|
||
```fstab
|
||
//<Windows-RPA-IP>/hxops-rpa-test-data /mnt/hxops-rpa-test-data cifs credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775,nofail,_netdev 0 0
|
||
//<Windows-RPA-IP>/hxops-rpa-prod-data /mnt/hxops-rpa-prod-data cifs credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775,nofail,_netdev 0 0
|
||
```
|
||
|
||
|
||
|
||
### 步骤 9:配置 Ubuntu 后端
|
||
|
||
test 后端使用:
|
||
|
||
```env
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR=/mnt/hxops-rpa-test-data/inventory_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR=/mnt/hxops-rpa-test-data/inventory_drop
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR=/mnt/hxops-rpa-test-data/upload_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR=/mnt/hxops-rpa-test-data/upload_queue
|
||
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID=hxops-rpa-test-data
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_TTL_SECONDS=90
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_REQUIRE_REDIS=true
|
||
```
|
||
|
||
prod 后端使用:
|
||
|
||
```env
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR=/mnt/hxops-rpa-prod-data/inventory_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR=/mnt/hxops-rpa-prod-data/inventory_drop
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR=/mnt/hxops-rpa-prod-data/upload_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR=/mnt/hxops-rpa-prod-data/upload_queue
|
||
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID=hxops-rpa-prod-data
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_TTL_SECONDS=90
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_REQUIRE_REDIS=true
|
||
SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED=false
|
||
```
|
||
|
||
同时确认 test/prod 后端各自配置与 Windows Secret 一致的:
|
||
|
||
```text
|
||
SHIPPING_NOTICE_TOOL_API_KEY
|
||
SHIPPING_NOTICE_RPA_CREDENTIAL_KEY
|
||
```
|
||
|
||
如使用 Docker Compose,**写 RPA trigger 的容器都必须挂载**对应目录:`hxops-backend`、`hxops-schedule-worker`(定时导出)、`hxops-shipping-worker`(按需库存准备 Gate Job)。只挂 backend 时,shipping-worker 会在容器本地假目录写 JSON,DB 记 `trigger_written`,但 Windows 共享盘无文件。
|
||
|
||
```yaml
|
||
# 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'
|
||
```
|
||
|
||
|
||
|
||
### 步骤 10:先在 Windows 手工启动两个 Worker
|
||
|
||
这一步先不创建任务计划,目的是确认脚本和 Secret 都正常。
|
||
|
||
打开第一个 PowerShell 窗口,启动 test:
|
||
|
||
如果 PowerShell 提示“在此系统上禁止运行脚本”,这是本机执行策略拦截 `.ps1`,不是 Worker 参数或后端业务错误。推荐使用 `-ExecutionPolicy Bypass` 仅绕过本次进程,不修改系统策略:
|
||
|
||
```powershell
|
||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File `
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1" `
|
||
-Environment test `
|
||
-ApiBase "http://192.168.100.192:8890" `
|
||
-WorkerRoot "C:\HxopsRpaTest" `
|
||
-SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
先确认脚本确实已复制到 Worker 根目录:
|
||
|
||
```powershell
|
||
Test-Path "C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1"
|
||
```
|
||
|
||
若脚本来自网络或压缩包且仍被标记为阻止,可只解除该文件的阻止标记:
|
||
|
||
```powershell
|
||
Unblock-File "C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1"
|
||
```
|
||
|
||
`-ApiBase` 必须填 Worker 实际可达的 HXOPS API 入口,判据是 `{base}/api/shipping-notice/rpa/heartbeat` 能访问到后端。Docker Compose 部署下后端容器内部监听 `5000`(`backend/deploy/gunicorn_config.py` 绑定 `0.0.0.0:5000`),而 `docker-compose.yml` 把宿主 `5052` 映射到容器内一个无监听端口,因此 `http://<host>:5052` 直连不可用;前端 nginx 网关(宿主 `8890`)把 `/api/` 反代到 `hxops-backend:5000`(见 `frontend/nginx.conf`),所以 Worker 应填网关地址,如 `http://192.168.100.192:8890`。该地址与 `HXOPS_TOOLS_API_BASE` 指向同一对外网关(都经 `/api/` 反代),实际以“`{base}/api/...` 可达”为准,不必拘泥于 Tool API / 业务后端的命名区分。启动前先验证网关端口连通:
|
||
|
||
```powershell
|
||
Test-NetConnection 192.168.100.192 -Port 8890
|
||
```
|
||
|
||
注意 `<网关>:8890/health` 会命中前端 SPA、不会透传到后端,不能作为后端连通性判据;是否可达以后端为准(Worker 心跳 `[heartbeat]` 上报成功或管理端显示 Worker 在线)。若 `TcpTestSucceeded` 为 `False`,应先核对测试服务器网关端口映射、nginx `/api/` 反代与防火墙。
|
||
|
||
确认通过后,也可以在当前窗口执行普通命令:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
.\scripts\rpa\inventory\start_inventory_worker.ps1 `
|
||
-Environment test `
|
||
-ApiBase "http://<test后端地址>:<端口>" `
|
||
-WorkerRoot "C:\HxopsRpaTest" `
|
||
-SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
打开第二个 PowerShell 窗口,启动 prod:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaProd
|
||
.\scripts\rpa\inventory\start_inventory_worker.ps1 `
|
||
-Environment prod `
|
||
-ApiBase "http://<prod后端地址>:<端口>" `
|
||
-WorkerRoot "C:\HxopsRpaProd" `
|
||
-SecretFile "C:\HxopsRpaSecrets\prod.xml"
|
||
```
|
||
|
||
两个窗口都应看到:
|
||
|
||
```text
|
||
Starting RPA Worker: environment=...
|
||
[freight_rpa_worker] started
|
||
```
|
||
|
||
再检查哨兵:
|
||
|
||
```powershell
|
||
Get-Content C:\HxopsRpaTest\data\inventory_triggers\.hxops_rpa_shared_mount
|
||
Get-Content C:\HxopsRpaProd\data\inventory_triggers\.hxops_rpa_shared_mount
|
||
```
|
||
|
||
test 必须包含 `scope_id=hxops-rpa-test-data`,prod 必须包含 `scope_id=hxops-rpa-prod-data`。管理端也必须同时显示 test/prod Worker 在线。
|
||
|
||
手工验证完成后,在两个 Worker 窗口分别按 `Ctrl+C` 停止,然后进入下一步创建后台任务。
|
||
|
||
### 步骤 11:创建 test/prod 后台任务
|
||
|
||
使用 RPA Windows 账号打开 PowerShell。先把 `$PythonExe`、test/prod API Base 替换成真实值,然后整段执行。test/prod 的 `-ApiBase` 应填实际可达的后端入口(同步骤 10:Docker Compose 部署填网关地址如 `http://192.168.100.192:8890`):
|
||
|
||
```powershell
|
||
$PythonExe = "C:\Users\<RPA用户>\AppData\Local\Programs\Python\Python312\python.exe"
|
||
$TestApiBase = "http://<test后端地址>:<端口>"
|
||
$ProdApiBase = "http://<prod后端地址>:<端口>"
|
||
$RunAsUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||
$PowerShellExe = "$env:SystemRoot\System32\WindowsPowerShell\v1.0\powershell.exe"
|
||
|
||
if (-not (Test-Path -LiteralPath $PythonExe -PathType Leaf)) {
|
||
throw "Python 路径不存在: $PythonExe"
|
||
}
|
||
|
||
$Settings = New-ScheduledTaskSettingsSet `
|
||
-StartWhenAvailable `
|
||
-RestartCount 10 `
|
||
-RestartInterval (New-TimeSpan -Minutes 1) `
|
||
-ExecutionTimeLimit ([TimeSpan]::Zero) `
|
||
-MultipleInstances IgnoreNew
|
||
|
||
$Principal = New-ScheduledTaskPrincipal `
|
||
-UserId $RunAsUser `
|
||
-LogonType Interactive `
|
||
-RunLevel Limited
|
||
|
||
$TestAction = New-ScheduledTaskAction `
|
||
-Execute $PowerShellExe `
|
||
-Argument "-NoProfile -ExecutionPolicy Bypass -File `"C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1`" -Environment test -ApiBase `"$TestApiBase`" -WorkerRoot `"C:\HxopsRpaTest`" -SecretFile `"C:\HxopsRpaSecrets\test.xml`" -PythonExe `"$PythonExe`"" `
|
||
-WorkingDirectory "C:\HxopsRpaTest"
|
||
|
||
$ProdAction = New-ScheduledTaskAction `
|
||
-Execute $PowerShellExe `
|
||
-Argument "-NoProfile -ExecutionPolicy Bypass -File `"C:\HxopsRpaProd\scripts\rpa\inventory\start_inventory_worker.ps1`" -Environment prod -ApiBase `"$ProdApiBase`" -WorkerRoot `"C:\HxopsRpaProd`" -SecretFile `"C:\HxopsRpaSecrets\prod.xml`" -PythonExe `"$PythonExe`"" `
|
||
-WorkingDirectory "C:\HxopsRpaProd"
|
||
|
||
$Trigger = New-ScheduledTaskTrigger -AtLogOn -User $RunAsUser
|
||
|
||
Register-ScheduledTask `
|
||
-TaskName "HXOPS Inventory RPA Worker TEST" `
|
||
-Action $TestAction `
|
||
-Trigger $Trigger `
|
||
-Principal $Principal `
|
||
-Settings $Settings `
|
||
-Description "HXOPS RPA TEST Worker"
|
||
|
||
Register-ScheduledTask `
|
||
-TaskName "HXOPS Inventory RPA Worker PROD" `
|
||
-Action $ProdAction `
|
||
-Trigger $Trigger `
|
||
-Principal $Principal `
|
||
-Settings $Settings `
|
||
-Description "HXOPS RPA PROD Worker"
|
||
```
|
||
|
||
这两个任务是 `AtLogOn + Interactive`:RPA 账号登录 Windows 后自动启动;不要改成 `SYSTEM` 或 `AtStartup`。
|
||
|
||
立即启动两个任务:
|
||
|
||
```powershell
|
||
Start-ScheduledTask -TaskName "HXOPS Inventory RPA Worker TEST"
|
||
Start-ScheduledTask -TaskName "HXOPS Inventory RPA Worker PROD"
|
||
Start-Sleep -Seconds 10
|
||
Get-ScheduledTaskInfo -TaskName "HXOPS Inventory RPA Worker TEST"
|
||
Get-ScheduledTaskInfo -TaskName "HXOPS Inventory RPA Worker PROD"
|
||
```
|
||
|
||
`LastTaskResult=267009` / `0x41301` 表示任务正在运行,不是错误。
|
||
|
||
### 步骤 12:最终验收
|
||
|
||
一项一项确认:
|
||
|
||
1. `Get-ScheduledTask` 显示 test/prod 两个任务。
|
||
2. test/prod 日志都在持续更新:
|
||
|
||
```powershell
|
||
Get-Content C:\HxopsRpaTest\data\logs\worker.log -Tail 100
|
||
Get-Content C:\HxopsRpaProd\data\logs\worker.log -Tail 100
|
||
```
|
||
|
||
1. test/prod 哨兵 scope 各自正确。
|
||
2. HXOPS 管理端显示 test/prod Worker 在线。
|
||
3. 先在 test 的**停用库存站点**发起一次测试导出。
|
||
4. trigger 处理后进入 test `inventory_triggers\processed`。
|
||
5. Excel 进入 test `inventory_drop`,后端收到回调并完成结构校验。
|
||
6. test 真实登录、下载、落盘、回调和结构校验全部通过后,再在受控窗口做一笔 prod 导出验收。
|
||
7. 上传 Driver 没有完成生产授权验收时,必须保持 `SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED=false`。
|
||
|
||
部署完成后的日常命令:
|
||
|
||
```powershell
|
||
# 启动
|
||
Start-ScheduledTask -TaskName "HXOPS Inventory RPA Worker TEST"
|
||
Start-ScheduledTask -TaskName "HXOPS Inventory RPA Worker PROD"
|
||
|
||
# 停止:先停止新 trigger,等当前任务完成后再执行
|
||
Stop-ScheduledTask -TaskName "HXOPS Inventory RPA Worker TEST"
|
||
Stop-ScheduledTask -TaskName "HXOPS Inventory RPA Worker PROD"
|
||
|
||
# 查状态
|
||
Get-ScheduledTask -TaskName "HXOPS Inventory RPA Worker TEST"
|
||
Get-ScheduledTask -TaskName "HXOPS Inventory RPA Worker PROD"
|
||
|
||
# 看日志
|
||
Get-Content C:\HxopsRpaTest\data\logs\worker.log -Tail 100 -Wait
|
||
Get-Content C:\HxopsRpaProd\data\logs\worker.log -Tail 100 -Wait
|
||
```
|
||
|
||
|
||
|
||
## 一、首次完整部署主线:Windows RPA + Ubuntu HXOPS
|
||
|
||
本节按实际首次部署顺序编排:规划路径 → 准备 Windows Worker → 创建 SMB 共享 → 配置心跳和环境变量 → Ubuntu 挂载 → 配置 HXOPS 后端 → 端到端验收。示例 IP、账号、密码和 Key 都是占位符,必须在各环境本地替换,不得提交真实值。
|
||
|
||
### 1.1 路径和环境规划
|
||
|
||
```text
|
||
Ubuntu HXOPS 后端主机 IP:192.168.1.20
|
||
Windows RPA 主机 IP:192.168.100.165
|
||
Windows 测试环境共享名:hxops-rpa-test-data
|
||
Windows 正式环境共享名:hxops-rpa-prod-data
|
||
Windows 测试环境本地目录:C:\HxopsRpaTest
|
||
Windows 正式环境本地目录:C:\HxopsRpaProd
|
||
Ubuntu 测试环境挂载目录:/mnt/hxops-rpa-test-data
|
||
Ubuntu 正式环境挂载目录:/mnt/hxops-rpa-prod-data
|
||
HXOPS 后端服务用户:hxops
|
||
```
|
||
|
||
路径必须一一对应:
|
||
|
||
```text
|
||
测试环境:
|
||
C:\HxopsRpaTest\data\inventory_triggers <-> /mnt/hxops-rpa-test-data/inventory_triggers
|
||
C:\HxopsRpaTest\data\inventory_drop <-> /mnt/hxops-rpa-test-data/inventory_drop
|
||
|
||
正式环境:
|
||
C:\HxopsRpaProd\data\inventory_triggers <-> /mnt/hxops-rpa-prod-data/inventory_triggers
|
||
C:\HxopsRpaProd\data\inventory_drop <-> /mnt/hxops-rpa-prod-data/inventory_drop
|
||
```
|
||
|
||
Ubuntu 后端写 Linux 挂载路径,Windows Worker 读写 Windows 本地路径,两者实际指向同一 SMB 共享。test/prod 必须使用不同目录、共享、挂载点、scope、Key 和 Worker 任务名。
|
||
|
||
### 1.2 Windows RPA 主机创建目录和部署文件
|
||
|
||
在 Windows RPA 主机的 PowerShell 中执行:
|
||
|
||
```powershell
|
||
foreach ($root in @("C:\HxopsRpaTest", "C:\HxopsRpaProd")) {
|
||
New-Item -ItemType Directory -Force $root
|
||
New-Item -ItemType Directory -Force "$root\scripts\rpa"
|
||
New-Item -ItemType Directory -Force "$root\backend\config"
|
||
New-Item -ItemType Directory -Force "$root\data\inventory_triggers"
|
||
New-Item -ItemType Directory -Force "$root\data\inventory_drop"
|
||
New-Item -ItemType Directory -Force "$root\data\upload_triggers"
|
||
New-Item -ItemType Directory -Force "$root\data\upload_queue"
|
||
New-Item -ItemType Directory -Force "$root\data\browser_profile"
|
||
New-Item -ItemType Directory -Force "$root\data\logs"
|
||
}
|
||
```
|
||
|
||
从待部署的已审核仓库版本复制整个 `scripts\rpa` 目录到 test/prod 的 `scripts\rpa`,保证 Worker、driver、session、heartbeat、调试工具和公共模块来自同一版本。其中至少包含:
|
||
|
||
```text
|
||
scripts\rpa\inventory\inventory_rpa_worker.py
|
||
scripts\rpa\common\rpa_common.py
|
||
scripts\rpa\common\rpa_heartbeat.py
|
||
scripts\rpa\connectors\dsv\dsv_driver.py
|
||
scripts\rpa\connectors\dsv\dsv_login_smoke.py
|
||
scripts\rpa\connectors\dsv\dsv_report_parameters.py
|
||
scripts\rpa\inventory\run_inventory_batch_trigger.py
|
||
scripts\rpa\connectors\crane\crane_login_smoke.py
|
||
scripts\rpa\common\new_rpa_worker_secret.ps1
|
||
scripts\rpa\inventory\start_inventory_worker.ps1
|
||
```
|
||
|
||
不得复制 `.env*`、密码文件、`backend\data` 旧运行数据、旧浏览器 profile、旧 trigger 以及 `processed` / `failed` 文件。
|
||
|
||
### 1.3 Windows RPA 主机创建 SMB 共享
|
||
|
||
建议创建独立共享账号,密码只在主机本地或 Secret 管理机制中维护:
|
||
|
||
```powershell
|
||
net user hxops_share "请替换为强密码" /add
|
||
```
|
||
|
||
Windows 图形界面配置:
|
||
|
||
1. 右键 `C:\HxopsRpaTest\data` → 属性 → 共享 → 高级共享。
|
||
2. 共享名填写 `hxops-rpa-test-data`,共享权限给 `hxops_share` 读取、更改和完全控制。
|
||
3. 在“安全”选项卡同时给 `hxops_share` NTFS 读取、写入和修改权限。
|
||
4. 对 `C:\HxopsRpaProd\data` 重复以上操作,共享名填写 `hxops-rpa-prod-data`。
|
||
|
||
Ubuntu 访问地址:
|
||
|
||
```text
|
||
//192.168.100.165/hxops-rpa-test-data
|
||
//192.168.100.165/hxops-rpa-prod-data
|
||
```
|
||
|
||
|
||
|
||
### 1.4 Windows RPA 主机安装 Python 和依赖
|
||
|
||
安装 Python 3.12 并勾选 `Add Python to PATH`,然后执行:
|
||
|
||
```powershell
|
||
python --version
|
||
python -m pip --version
|
||
python -m pip install --upgrade pip
|
||
python -m pip install playwright openpyxl pyyaml
|
||
python -m playwright install chromium
|
||
```
|
||
|
||
若 Worker 复用项目完整依赖,也可执行 `python -m pip install -r <项目仓库>\backend\requirements.txt playwright`。使用本机 Edge 时设置 `SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL=msedge`。
|
||
|
||
### 1.5 Windows RPA 临时变量、登录测试与心跳
|
||
|
||
先在当前 PowerShell 会话中验证 test 链路;密钥从受控 Secret 渠道注入,不得将真实值保存到脚本或命令历史:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
|
||
$env:HXOPS_API_BASE = "http://测试后端地址:端口"
|
||
$env:SHIPPING_NOTICE_TOOL_API_KEY = "请替换为测试环境Key"
|
||
# SHIPPING_NOTICE_RPA_CREDENTIAL_KEY 由受控 Secret 工具注入当前进程。
|
||
|
||
$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_WORKER_HEARTBEAT_SECONDS = "20"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT = "test"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID = "hxops-rpa-test-data"
|
||
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR = "C:\HxopsRpaTest\data\inventory_triggers"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR = "C:\HxopsRpaTest\data\inventory_drop"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR = "C:\HxopsRpaTest\data\upload_triggers"
|
||
$env:SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR = "C:\HxopsRpaTest\data\upload_queue"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR = "C:\HxopsRpaTest\data\browser_profile"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_LOG_FILE = "C:\HxopsRpaTest\data\logs\worker.log"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_LOG_MAX_BYTES = "10485760"
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_LOG_BACKUP_COUNT = "5"
|
||
|
||
$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密码"
|
||
```
|
||
|
||
正式环境将 `C:\HxopsRpaTest` 替换为 `C:\HxopsRpaProd`,Worker environment 替换为 `prod`,scope 替换为 `hxops-rpa-prod-data`,同时使用正式 API Base、Tool API Key 和 Fernet 主密钥。
|
||
|
||
先执行登录烟测:
|
||
|
||
```powershell
|
||
python scripts\rpa\connectors\dsv\dsv_login_smoke.py
|
||
python scripts\rpa\connectors\crane\crane_login_smoke.py
|
||
```
|
||
|
||
DSV 成功时会输出 `SUCCESS — DSV session ready`。如需人工联调已由平台生成的 trigger,执行:
|
||
|
||
```powershell
|
||
python scripts\rpa\inventory\run_inventory_batch_trigger.py <trigger.json>
|
||
```
|
||
|
||
该入口不从 YAML 或本地文件读取账号密码;Profile 密文凭据和页面操作由平台 trigger 提供。
|
||
|
||
Worker 启动后会周期调用 `POST /api/shipping-notice/rpa/heartbeat`,并在 inventory trigger 目录写入 `.hxops_rpa_shared_mount`。部署参数如下:
|
||
|
||
`SHIPPING_NOTICE_RPA_SHARED_SENTINEL_NAME` 可覆盖哨兵文件名;未设置时使用 `.hxops_rpa_shared_mount`。
|
||
|
||
|
||
| 侧 | 变量 | 测试示例 | 正式示例 |
|
||
| ----------- | ---------------------------------------------------- | --------------------- | --------------------- |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` | `hxops-rpa-test-data` | `hxops-rpa-prod-data` |
|
||
| Windows RPA | `SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID` | `hxops-rpa-test-data` | `hxops-rpa-prod-data` |
|
||
| Windows RPA | `SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT` | `test` | `prod` |
|
||
| Windows RPA | `SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_SECONDS` | `15`~`30`(默认 20) | 同左 |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_TTL_SECONDS` | `90` | `90` |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_WORKER_ALERT_ENABLED` | `false` | 需要时改为 `true` |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_WORKER_ALERT_USERNAMES` | 逗号分隔 HXOPS 用户名 | 部署前建议显式填写 |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_WORKER_ALERT_FALLBACK_ADMIN` | `true` | 建议 `true` |
|
||
| Ubuntu 后端 | `SHIPPING_NOTICE_RPA_WORKER_ALERT_SEND_LOCK_SECONDS` | `120` | `120` |
|
||
|
||
|
||
`SHIPPING_NOTICE_RPA_WORKER_ALERT_USERNAMES` 留空时,只有存在 `role=admin` 且已绑定 `wecom_userid` 的用户才会收到企微通知;否则只写 warning。告警冷却状态以 `app_settings` 为权威源,Redis 只作缓存同步;DB 不可读写时才回退 Redis/进程内 claim。
|
||
|
||
test/prod 必须显式配置 scope;Windows 标准路径不含共享名,无法靠路径稳定推导。漏配时 Worker 不写哨兵,后端就绪检查会返回 `shared_sentinel_missing`。
|
||
|
||
```powershell
|
||
Get-Content C:\HxopsRpaTest\data\inventory_triggers\.hxops_rpa_shared_mount
|
||
```
|
||
|
||
```bash
|
||
cat /mnt/hxops-rpa-test-data/inventory_triggers/.hxops_rpa_shared_mount
|
||
```
|
||
|
||
哨兵应包含 `scope_id=hxops-rpa-test-data`。管理端 `GET /api/admin/rpa-workers/status` 可查看最后心跳;Worker 离线时正式触发会 fail-fast。test/prod 默认要求 Redis;Redis 不可用时返回 `RPA.HEARTBEAT_STORAGE_UNAVAILABLE`。
|
||
|
||
### 1.6 Ubuntu 安装 SMB 工具并探测共享
|
||
|
||
```bash
|
||
sudo apt update
|
||
sudo apt install -y cifs-utils smbclient
|
||
smbclient -L //192.168.100.165 -U hxops_share
|
||
smbclient //192.168.100.165/hxops-rpa-test-data -U hxops_share
|
||
smbclient //192.168.100.165/hxops-rpa-prod-data -U hxops_share
|
||
```
|
||
|
||
在 `smbclient` 中执行 `ls`,应看到 `inventory_triggers`、`inventory_drop`、`upload_triggers`、`upload_queue`、`browser_profile` 和 `logs`。
|
||
|
||
### 1.7 Ubuntu 挂载 SMB 共享
|
||
|
||
```bash
|
||
sudo mkdir -p /mnt/hxops-rpa-test-data /mnt/hxops-rpa-prod-data /etc/smbcredentials
|
||
sudo chmod 700 /etc/smbcredentials
|
||
sudo nano /etc/smbcredentials/hxops-rpa
|
||
```
|
||
|
||
凭据文件只在 Ubuntu 本地维护:
|
||
|
||
```ini
|
||
username=hxops_share
|
||
password=请替换为Windows共享密码
|
||
domain=WORKGROUP
|
||
```
|
||
|
||
域账号需将 `domain` 替换为实际域名。
|
||
|
||
```bash
|
||
sudo chmod 600 /etc/smbcredentials/hxops-rpa
|
||
id hxops
|
||
```
|
||
|
||
以 `uid=1001,gid=1001` 为示例:
|
||
|
||
```bash
|
||
sudo mount -t cifs //192.168.100.165/hxops-rpa-test-data /mnt/hxops-rpa-test-data \
|
||
-o credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775
|
||
|
||
sudo mount -t cifs //192.168.100.165/hxops-rpa-prod-data /mnt/hxops-rpa-prod-data \
|
||
-o credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775
|
||
|
||
ls -la /mnt/hxops-rpa-test-data
|
||
ls -la /mnt/hxops-rpa-prod-data
|
||
sudo -u hxops touch /mnt/hxops-rpa-test-data/inventory_triggers/ubuntu_write_test.txt
|
||
sudo -u hxops rm /mnt/hxops-rpa-test-data/inventory_triggers/ubuntu_write_test.txt
|
||
sudo -u hxops touch /mnt/hxops-rpa-prod-data/inventory_triggers/ubuntu_write_test.txt
|
||
sudo -u hxops rm /mnt/hxops-rpa-prod-data/inventory_triggers/ubuntu_write_test.txt
|
||
```
|
||
|
||
|
||
|
||
### 1.8 Ubuntu 配置开机自动挂载
|
||
|
||
编辑 `/etc/fstab`,追加以下两行,并替换实际 IP、uid 和 gid:
|
||
|
||
```fstab
|
||
//192.168.100.165/hxops-rpa-test-data /mnt/hxops-rpa-test-data cifs credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775,nofail,_netdev 0 0
|
||
//192.168.100.165/hxops-rpa-prod-data /mnt/hxops-rpa-prod-data cifs credentials=/etc/smbcredentials/hxops-rpa,vers=3.0,iocharset=utf8,uid=1001,gid=1001,file_mode=0775,dir_mode=0775,nofail,_netdev 0 0
|
||
```
|
||
|
||
```bash
|
||
sudo umount /mnt/hxops-rpa-test-data
|
||
sudo umount /mnt/hxops-rpa-prod-data
|
||
sudo mount -a
|
||
ls -la /mnt/hxops-rpa-test-data
|
||
ls -la /mnt/hxops-rpa-prod-data
|
||
```
|
||
|
||
|
||
|
||
### 1.9 Ubuntu HXOPS 后端环境变量和容器挂载
|
||
|
||
Ubuntu 后端必须使用 Linux 挂载路径,不得写 Windows 本地路径或 UNC 路径。测试环境示例:
|
||
|
||
```env
|
||
SHIPPING_NOTICE_TOOL_API_KEY=请替换为强随机ASCII密钥
|
||
|
||
SHIPPING_NOTICE_AUTO_RPA_INVENTORY_ENABLED=true
|
||
SHIPPING_NOTICE_INVENTORY_RPA_BATCH_ENABLED=true
|
||
SHIPPING_NOTICE_RPA_INVENTORY_TIMEOUT_SECONDS=900
|
||
SHIPPING_NOTICE_INVENTORY_SNAPSHOT_TTL_HOURS=24
|
||
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR=/mnt/hxops-rpa-test-data/inventory_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR=/mnt/hxops-rpa-test-data/inventory_drop
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR=/mnt/hxops-rpa-test-data/upload_triggers
|
||
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR=/mnt/hxops-rpa-test-data/upload_queue
|
||
|
||
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID=hxops-rpa-test-data
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_TTL_SECONDS=90
|
||
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_REQUIRE_REDIS=true
|
||
SHIPPING_NOTICE_RPA_WORKER_ALERT_ENABLED=false
|
||
SHIPPING_NOTICE_RPA_WORKER_ALERT_USERNAMES=
|
||
SHIPPING_NOTICE_RPA_WORKER_ALERT_FALLBACK_ADMIN=true
|
||
SHIPPING_NOTICE_RPA_WORKER_ALERT_SEND_LOCK_SECONDS=120
|
||
```
|
||
|
||
正式环境使用 `/mnt/hxops-rpa-prod-data/...`、`hxops-rpa-prod-data` 和正式 Tool API Key。如使用 systemd:
|
||
|
||
```bash
|
||
sudo systemctl restart hxops-backend
|
||
```
|
||
|
||
如使用 Docker Compose,必须将宿主机挂载目录 bind mount 到**写 trigger 的容器**内相同路径(`hxops-backend`、`hxops-schedule-worker`、`hxops-shipping-worker`):
|
||
|
||
```yaml
|
||
# 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
|
||
```
|
||
|
||
|
||
|
||
### 1.10 端到端验收
|
||
|
||
1. Windows 手工打开 Edge,确认能访问 DSV/Crane。
|
||
2. Windows 执行 `python scripts\rpa\connectors\dsv\dsv_login_smoke.py` 和必要的 Crane smoke。
|
||
3. 在管理端创建库存 RPA trigger,确认其包含 Source、Profile 和密文凭据。
|
||
4. Ubuntu 执行 `sudo -u hxops touch /mnt/hxops-rpa-test-data/inventory_triggers/test_from_ubuntu.txt`,Windows 确认对应文件可见,然后删除测试文件。
|
||
5. Windows 按“步骤 10”使用 `start_rpa_worker.ps1` 启动对应环境 Worker。
|
||
6. 确认 `.hxops_rpa_shared_mount` 含正确 `scope_id`。
|
||
7. HXOPS 管理端或 `GET /api/admin/rpa-workers/status` 显示 Worker 在线。
|
||
8. HXOPS 页面或排程触发库存 RPA 导出。
|
||
9. Ubuntu 执行 `ls -lt /mnt/hxops-rpa-test-data/inventory_triggers` 查看 trigger。
|
||
10. Windows 查看 Worker 输出;成功后 trigger 进入 `inventory_triggers\processed`。
|
||
11. Excel 进入 `inventory_drop`,并经后端回调入库为库存快照。
|
||
12. HXOPS 任务进入库存字段映射/库存匹配;定时排程 run 从 `awaiting_rpa_callback` 终态化为 `success` / `failed` / `partial_failed`。
|
||
|
||
|
||
|
||
## 二、部署后日常运维
|
||
|
||
以下章节是对上述首次完整部署的运行速查,不替代 Windows 目录/共享、Ubuntu 挂载、心跳、环境变量、后台任务和端到端验收步骤。
|
||
|
||
### 2.1 Windows 本地联调(不连接 test/prod)
|
||
|
||
这是最简方式,适用于在当前项目根目录运行本地后端和 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"
|
||
|
||
\.\scripts\rpa\inventory\start_inventory_worker.ps1 -Environment test -ApiBase "http://<test后端地址>:<端口>" -WorkerRoot "C:\HxopsRpaTest" -SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
若只验证 trigger 扫描、文件流转与回调而不访问真实货代网站,可把 `SHIPPING_NOTICE_RPA_WORKER_MOCK` 设为 `true`。真实库存导出保持 `false`,并使用 HXOPS RPA Profile 随 trigger 下发的加密凭据。
|
||
|
||
#### 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 即可清除其进程环境,不要把值保存为用户级环境变量。
|
||
|
||
### 2.2 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
|
||
```
|
||
|
||
1. 使用受控方式设置 test 专属变量。推荐执行仓库提供的脚本,它会交互读取 Fernet 主密钥,避免在命令行中传入该密钥:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
|
||
```
|
||
|
||
重新打开 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`、`hxops-schedule-worker`、`hxops-shipping-worker` 都必须将宿主机挂载目录 bind mount 到容器**同一路径**(按需库存 trigger 由 shipping-worker 写入),否则容器内写到本地假目录、Windows 看不到:
|
||
|
||
```yaml
|
||
# hxops-backend / hxops-schedule-worker / hxops-shipping-worker 均需:
|
||
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 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,必须在变更后重新创建相关容器:
|
||
|
||
```bash
|
||
docker compose --env-file .env.test up -d --force-recreate hxops-backend hxops-shipping-worker hxops-schedule-worker
|
||
```
|
||
|
||
|
||
|
||
#### 3. 启动、验证与停止测试 Worker
|
||
|
||
在 Windows RPA 主机打开 PowerShell:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
\.\scripts\rpa\inventory\start_inventory_worker.ps1 -Environment test -ApiBase "http://<test后端地址>:<端口>" -WorkerRoot "C:\HxopsRpaTest" -SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
应同时验证以下四项后,再在管理端对**停用的库存站点**发起“测试导出”:
|
||
|
||
```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 后端运行,方便查看回调与排障日志。
|
||
|
||
### 2.3 Windows RPA 主机后台常驻与任务计划(同一用户双环境)
|
||
|
||
手工启动和端到端测试通过后,使用 Windows 任务计划让 Worker 在同一个 RPA 用户的交互式会话中同时运行。两个 Worker 不再使用用户级永久环境变量;每个任务调用独立的 `start_rpa_worker.ps1`,只在子进程中设置环境。`SHIPPING_NOTICE_RPA_WORKER_HEADLESS=false` 时仍需要交互式用户会话,因此不得改成 `AtStartup` 、`SYSTEM` 账号或 Windows Session 0 服务。
|
||
|
||
创建任务前确认:
|
||
|
||
1. 使用实际运行 Worker 的 RPA 专用 Windows 账号登录。
|
||
2. 分别运行 `new_rpa_worker_secret.ps1 -Environment test` 与 `-Environment prod` 创建 DPAPI Secret 文件。
|
||
3. 已手工运行两个 `start_rpa_worker.ps1`,并完成心跳、哨兵、登录、下载、落盘和回调验证。
|
||
4. 每个任务使用独立的 WorkerRoot、SecretFile、scope、profile 和日志;不共用 trigger/drop 目录。
|
||
|
||
以 test 环境为例,在 RPA 专用账号的 PowerShell 中执行:
|
||
|
||
```powershell
|
||
$TaskName = "HXOPS Inventory RPA Worker TEST"
|
||
$WorkerRoot = "C:\HxopsRpaTest"
|
||
$Launcher = "C:\HxopsRpaTest\scripts\rpa\inventory\start_inventory_worker.ps1"
|
||
$SecretFile = "C:\HxopsRpaSecrets\test.xml"
|
||
$ApiBase = "http://<测试后端地址>:<端口>"
|
||
$RunAsUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
|
||
|
||
if (-not (Test-Path -LiteralPath $Launcher -PathType Leaf)) {
|
||
throw "启动脚本不存在: $Launcher"
|
||
}
|
||
|
||
$Action = New-ScheduledTaskAction `
|
||
-Execute "powershell.exe" `
|
||
-Argument "-NoProfile -ExecutionPolicy Bypass -File `"$Launcher`" -Environment test -ApiBase `"$ApiBase`" -WorkerRoot `"$WorkerRoot`" -SecretFile `"$SecretFile`"" `
|
||
-WorkingDirectory $WorkerRoot
|
||
|
||
$Trigger = New-ScheduledTaskTrigger -AtLogOn -User $RunAsUser
|
||
$Principal = New-ScheduledTaskPrincipal `
|
||
-UserId $RunAsUser `
|
||
-LogonType Interactive `
|
||
-RunLevel Limited
|
||
$Settings = New-ScheduledTaskSettingsSet `
|
||
-StartWhenAvailable `
|
||
-RestartCount 10 `
|
||
-RestartInterval (New-TimeSpan -Minutes 1) `
|
||
-ExecutionTimeLimit ([TimeSpan]::Zero) `
|
||
-MultipleInstances IgnoreNew
|
||
|
||
Register-ScheduledTask `
|
||
-TaskName $TaskName `
|
||
-Action $Action `
|
||
-Trigger $Trigger `
|
||
-Principal $Principal `
|
||
-Settings $Settings `
|
||
-Description "HXOPS inventory export RPA worker TEST (process-scoped environment)"
|
||
```
|
||
|
||
正式环境将上述变量替换为:
|
||
|
||
```powershell
|
||
$TaskName = "HXOPS Inventory RPA Worker PROD"
|
||
$WorkerRoot = "C:\HxopsRpaProd"
|
||
$Launcher = "C:\HxopsRpaProd\scripts\rpa\inventory\start_inventory_worker.ps1"
|
||
$SecretFile = "C:\HxopsRpaSecrets\prod.xml"
|
||
$ApiBase = "http://<正式后端地址>:<端口>"
|
||
```
|
||
|
||
prod 任务的 `-Argument` 中将 `-Environment test` 改为 `-Environment prod`,并使用 prod 的 `$Launcher` / `$SecretFile` / `$ApiBase`。
|
||
|
||
test 和 prod 可在同一台主机、同一个交互式用户会话中并行,但不得共用用户级环境变量、浏览器 profile、触发/落盘目录或日志。创建 Secret 文件:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment test
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment prod
|
||
```
|
||
|
||
Secret 文件默认位于 `C:\HxopsRpaSecrets\test.xml` 和 `prod.xml`,使用当前 Windows 用户 DPAPI 保护;不得复制到仓库、共享目录或其他账号。
|
||
|
||
创建后手工启动并检查:
|
||
|
||
```powershell
|
||
Start-ScheduledTask -TaskName $TaskName
|
||
Start-Sleep -Seconds 5
|
||
Get-ScheduledTask -TaskName $TaskName
|
||
Get-ScheduledTaskInfo -TaskName $TaskName
|
||
Get-Content "$WorkerRoot\data\logs\worker.log" -Tail 100 -Wait
|
||
```
|
||
|
||
`LastTaskResult=267009` (`0x41301`) 表示任务正在运行,不是执行失败。还必须确认管理端显示对应环境 Worker 在线,且共享哨兵中的 `scope_id` 正确。
|
||
|
||
日常启停与排障:
|
||
|
||
```powershell
|
||
# 查询任务与最近执行结果
|
||
Get-ScheduledTask -TaskName $TaskName
|
||
Get-ScheduledTaskInfo -TaskName $TaskName
|
||
|
||
# 手工启动
|
||
Start-ScheduledTask -TaskName $TaskName
|
||
|
||
# 先停止新的 RPA 触发并等待当前 trigger 结束,再停止 Worker
|
||
Stop-ScheduledTask -TaskName $TaskName
|
||
|
||
# 查看轮转日志
|
||
Get-ChildItem "$WorkerRoot\data\logs\worker.log*"
|
||
Get-Content "$WorkerRoot\data\logs\worker.log" -Tail 200
|
||
```
|
||
|
||
仅在确认任务名和环境正确后删除任务;此操作不删除 Worker 文件、共享数据或日志:
|
||
|
||
```powershell
|
||
Stop-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
||
```
|
||
|
||
计划维护时,必须按“暂停新触发 → 处理/记录运行中 trigger → 停止任务”的顺序执行。恢复时按“确认 SMB 挂载 → 确认 HXOPS 后端 → 启动 Worker → 等待心跳/哨兵 → 恢复排程”的顺序执行。
|
||
|
||
#### 2.3.1 headless 后台模式(无可见 Edge,完全后台)
|
||
|
||
> 适用:希望彻底摆脱「可见 Edge + 交互式桌面」,让 Worker 无控制台、无浏览器窗口地后台常驻。
|
||
> 交付脚本:`scripts/rpa/inventory/register_inventory_worker_tasks.ps1`(test/prod 双任务,headless + 隐藏控制台,仍为 RPA 账号的 `AtLogOn + Interactive` 任务)。
|
||
|
||
**切换前必须先完成 headless 冒烟预检(风险最高,不要跳过):**
|
||
|
||
```powershell
|
||
$env:SHIPPING_NOTICE_RPA_WORKER_HEADLESS = "true"
|
||
# 在 RPA 主机验证真实登录/导出可通:
|
||
python scripts\rpa\connectors\dsv\dsv_login_smoke.py
|
||
python scripts\rpa\connectors\crane\crane_login_smoke.py
|
||
# 若触及上传,再验证一次 headless 下的上传流程
|
||
```
|
||
|
||
只有 DSV/Crane 在 headless 下登录、下载、回调(以及在启用时的上传)都能通过,才切换正式运行;否则仍用 2.3 的可见模式。
|
||
|
||
**注册后台任务(在 RPA 账号的 PowerShell 中执行):**
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 # 注册 test/prod 两个任务
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Start # 立即启动(默认 all)
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Status # 查看状态(默认 all)
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Stop # 停止(默认 all)
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Unregister # 注销(默认 all)
|
||
|
||
# 区分环境:用 -Scope test|prod|all(默认 all)
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Scope test -Status # 只看 test
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Scope prod -Start # 只启动 prod
|
||
.\scripts\rpa\inventory\register_inventory_worker_tasks.ps1 -Scope test -Unregister # 只注销 test
|
||
```
|
||
|
||
#### PowerShell 执行策略阻止脚本时
|
||
|
||
如果直接执行 `.ps1` 时出现“在此系统上禁止运行脚本”(`PSSecurityException` / `UnauthorizedAccess`),优先使用一次性的 `ExecutionPolicy Bypass`,不要修改计算机或用户的全局执行策略:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaTest
|
||
|
||
# 注册 TEST 计划任务
|
||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File `
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\register_inventory_worker_tasks.ps1" `
|
||
-Scope test `
|
||
-TestApiBase "http://192.168.100.192:8890" `
|
||
-TestRoot "C:\HxopsRpaTest" `
|
||
-SecretFile "C:\HxopsRpaSecrets"
|
||
|
||
# 启动或查看 TEST 任务
|
||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File `
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\register_inventory_worker_tasks.ps1" `
|
||
-Scope test -Start
|
||
|
||
powershell.exe -NoProfile -ExecutionPolicy Bypass -File `
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\register_inventory_worker_tasks.ps1" `
|
||
-Scope test -Status
|
||
```
|
||
|
||
如果脚本是从浏览器、邮件或压缩包复制而来,可能带有 Internet 下载标记;确认路径无误后可解除该文件的阻止状态,再按普通方式执行:
|
||
|
||
```powershell
|
||
Unblock-File -LiteralPath `
|
||
"C:\HxopsRpaTest\scripts\rpa\inventory\register_inventory_worker_tasks.ps1"
|
||
```
|
||
|
||
可用以下命令查看当前执行策略:
|
||
|
||
```powershell
|
||
Get-ExecutionPolicy -List
|
||
```
|
||
|
||
不建议直接设置 `Unrestricted`。如果 `MachinePolicy` 或 `UserPolicy` 已由公司组策略设置,`Bypass` 仍可能被拦截,此时需要管理员调整组策略或按公司运维流程批准执行该脚本。
|
||
|
||
- **`-Scope test|prod|all`**:`Start/Stop/Status/Unregister`(以及默认注册)都可只作用于单个环境;不带 `-Scope` 时默认操作 test 和 prod 两个任务。两个任务是独立命名的(`HXOPS Inventory RPA Worker TEST` / `...PROD`),也可用原生 `Get-ScheduledTask -TaskName <任务名>` 单独查询。
|
||
|
||
- 与 2.3 可见模式仅两处差异:计划任务给 `powershell.exe` 加 `-WindowStyle Hidden`(无控制台),并给 `start_rpa_worker.ps1` 加 `-Headless`(Playwright headless,无可见 Edge)。其余 `AtLogOn`、`Interactive`、`RestartCount/RestartInterval`、`ExecutionTimeLimit=0`、`MultipleInstances=IgnoreNew` 均与 2.3 一致。
|
||
- **不能改成 `SYSTEM` / `AtStartup` / Session 0 服务**:此处约束已从“浏览器需要交互桌面”变为“Secret 文件是用户级 DPAPI”。`new_rpa_worker_secret.ps1` 用 `Export-Clixml` 生成用户级 DPAPI 密文,改成非交互上下文(SYSTEM/服务/其他账号)会解密失败。
|
||
- **RPA 账号请配置自动登录,且不要锁屏/注销**:headless 去掉了浏览器窗口依赖,但 `AtLogOn + Interactive` 仍要求该账号保持交互登录,否则任务不会常驻。
|
||
- `-ApiBase` 同样填前端 nginx 网关地址(例如 `http://192.168.100.192:8890`),与 2.3 一致;脚本默认值已按此预填,可按实际地址改。
|
||
- **回滚**:改用 2.3 的可见模式重新注册即可(去掉 `-Headless` 与 `-WindowStyle Hidden`),无需改动 Worker 或共享目录。
|
||
|
||
### 2.4 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 使用 `start_rpa_worker.ps1 -Environment prod` 设置进程级变量;其 Tool API Key 与 Fernet 主密钥只能来自正式 Secret 文件或受控 Secret 渠道。
|
||
5. 正式库存站点已启用并绑定 published 解析方案;真实上传 Driver 尚未验收时必须保持 `SHIPPING_NOTICE_AUTO_UPLOAD_ENABLED=false`。
|
||
|
||
在 Windows RPA 主机设置并启动 prod Worker:
|
||
|
||
```powershell
|
||
cd C:\HxopsRpaProd
|
||
.\scripts\rpa\common\new_rpa_worker_secret.ps1 -Environment prod
|
||
.\scripts\rpa\inventory\start_inventory_worker.ps1 -Environment prod -ApiBase "http://<正式后端地址>:<端口>" -WorkerRoot "C:\HxopsRpaProd" -SecretFile "C:\HxopsRpaSecrets\prod.xml"
|
||
|
||
# 关闭并重新打开 PowerShell 后执行
|
||
cd C:\HxopsRpaProd
|
||
\.\scripts\rpa\inventory\start_inventory_worker.ps1 -Environment test -ApiBase "<test后端地址>:<端口>" -WorkerRoot "C:\HxopsRpaTest" -SecretFile "C:\HxopsRpaSecrets\test.xml"
|
||
```
|
||
|
||
|
||
|
||
#### 2. 启动正式 Linux 后端并验证共享
|
||
|
||
在正式服务器项目根目录执行。前提是 `/mnt/hxops-rpa-prod-data` 已经通过受控 SMB 凭据挂载,且 `docker-compose.yml` 中 `hxops-backend` / `hxops-schedule-worker` / `hxops-shipping-worker` 均已 bind mount:
|
||
|
||
```yaml
|
||
# 三个服务均需:
|
||
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 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
|
||
```
|
||
|
||
哨兵内容必须包含 `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 共享目录验证正式环境。
|
||
|
||
### 2.5 统一故障定位顺序
|
||
|
||
|
||
| 现象 | 先检查 | 常见处理 |
|
||
| ------------------------------------------ | -------------------------------------------------------------------- | --------------------------------------------------------------- |
|
||
| 后端触发即提示 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 方案 | 修订并发布解析方案后重新测试;不得在生产运行期自动猜列 |
|
||
| `smbclient` 可访问但后端无法写入 | `sudo -u hxops touch` 的执行结果、挂载参数中的 uid/gid、Windows 共享权限和 NTFS 权限 | 使后端服务用户与挂载 uid/gid 一致,并同时修正共享权限和 NTFS 权限 |
|
||
| Ubuntu 重启后挂载目录为空 | `mountpoint`、`sudo mount -a`、`/etc/fstab`、Windows 共享可达性 | 修正 `_netdev,nofail`、IP/凭据、uid/gid 或网络就绪顺序,再重新挂载 |
|
||
| 排程长期停在 `running` / `awaiting_rpa_callback` | `processed` 目录、Worker 日志、`inventory-batch-ready` 回调、后端 run 状态 | 确认 trigger 已处理且回调成功;不要手工伪造终态或跨环境移动 trigger |
|
||
|
||
|
||
|
||
|
||
## 三、开发/模拟
|
||
|
||
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/inventory/inventory_rpa_worker.py
|
||
```
|
||
|
||
1. 在 Chat 中走 `ensure_inventory` / `upload_to_freight`,worker 会自动回调 `/api/shipping-notice/rpa/*`。
|
||
|
||
|
||
|
||
## 四、Driver 与业务参考
|
||
|
||
`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/inventory/inventory_rpa_worker.py
|
||
```
|
||
|
||
说明:`SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR` 是浏览器 Profile 根目录。库存导出默认开启会话复用:目录形如 `{connector}__{rpa_profile_code}_session`,跨任务长期保留登录态,不以本地 TTL 强制清空;站点会话失效时由登录流程重登。关闭复用或上传任务仍使用 `run_*` 一次性子目录并在结束后清理。同一 WorkerRoot 不要并发启动第二个库存 Worker。
|
||
|
||
如果登录框默认识别不稳定,可覆盖登录 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\connectors\crane\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 下载 |
|
||
|
||
|
||
|
||
|
||
### 4.1 生产库存 RPA 与登录选项
|
||
|
||
|
||
|
||
### 4.2 批量库存导出(`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>`。
|