Files
syops/scripts/rpa/start_rpa_worker.ps1
T

103 lines
4.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<#
.SYNOPSIS
在当前 PowerShell 进程树中启动指定环境的 RPA Worker。
.DESCRIPTION
该脚本只写入 Process 级环境变量,绝不调用 User/Machine 环境变量 API。
test/prod 可由同一 Windows 用户、同一交互式桌面、两个独立计划任务同时运行。
每个环境必须有独立 root、scope、共享目录、browser profile、日志和 DPAPI Secret 文件。
DSV/Crane 登录地址、账号和密码来自 HXOPS trigger 的 credential_envelope,
不从本机 Secret 文件读取。
#>
param(
[Parameter(Mandatory = $true)]
[ValidateSet("test", "prod")]
[string]$Environment,
[Parameter(Mandatory = $true)]
[string]$ApiBase,
[Parameter(Mandatory = $true)]
[string]$WorkerRoot,
[Parameter(Mandatory = $true)]
[string]$SecretFile,
[string]$PythonExe = "python.exe",
[switch]$Mock,
[switch]$Headless,
[switch]$ReuseDsvSession
)
$ErrorActionPreference = "Stop"
if (-not (Test-Path -LiteralPath $SecretFile -PathType Leaf)) {
throw "Secret 文件不存在:$SecretFile。请先运行 new_rpa_worker_secret.ps1。"
}
$workerScript = Join-Path $WorkerRoot "scripts\rpa\freight_rpa_worker.py"
if (-not (Test-Path -LiteralPath $workerScript -PathType Leaf)) {
throw "Worker 不存在:$workerScript"
}
if ($Environment -eq "test") {
$scopeId = "hxops-rpa-test-data"
} else {
$scopeId = "hxops-rpa-prod-data"
}
$dataRoot = Join-Path $WorkerRoot "data"
function ConvertTo-PlainText {
param([Parameter(Mandatory = $true)][System.Security.SecureString]$Value)
$pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Value)
try { return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer) }
finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer) }
}
$secret = Import-Clixml -LiteralPath $SecretFile
foreach ($required in @("ToolApiKey", "CredentialKey")) {
if (-not ($secret.PSObject.Properties.Name -contains $required)) {
throw "Secret 文件缺少字段:$required"
}
}
# 只设置当前 launcher 及其 Worker 子进程可见的环境变量;不写 User/Machine 持久配置。
$processVars = [ordered]@{
HXOPS_API_BASE = $ApiBase
SHIPPING_NOTICE_TOOL_API_KEY = ConvertTo-PlainText $secret.ToolApiKey
SHIPPING_NOTICE_RPA_CREDENTIAL_KEY = ConvertTo-PlainText $secret.CredentialKey
SHIPPING_NOTICE_RPA_WORKER_ENVIRONMENT = $Environment
SHIPPING_NOTICE_RPA_SHARED_SCOPE_ID = $scopeId
SHIPPING_NOTICE_RPA_WORKER_HEARTBEAT_SECONDS = "20"
SHIPPING_NOTICE_RPA_WORKER_MOCK = $(if ($Mock) { "true" } else { "false" })
SHIPPING_NOTICE_RPA_WORKER_HEADLESS = $(if ($Headless) { "true" } else { "false" })
# 仅显式启用时让 DSV 库存任务复用专属 profile;默认继续使用一次性会话。
SHIPPING_NOTICE_RPA_WORKER_DSV_SESSION_REUSE = $(if ($ReuseDsvSession) { "true" } else { "false" })
SHIPPING_NOTICE_RPA_WORKER_BROWSER_CHANNEL = "msedge"
SHIPPING_NOTICE_RPA_WORKER_POLL_SECONDS = "3"
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_TRIGGER_DIR = Join-Path $dataRoot "inventory_triggers"
SHIPPING_NOTICE_RPA_SHARED_INVENTORY_DROP_DIR = Join-Path $dataRoot "inventory_drop"
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_TRIGGER_DIR = Join-Path $dataRoot "upload_triggers"
SHIPPING_NOTICE_RPA_SHARED_UPLOAD_QUEUE_DIR = Join-Path $dataRoot "upload_queue"
SHIPPING_NOTICE_RPA_WORKER_BROWSER_PROFILE_DIR = Join-Path $dataRoot "browser_profile"
SHIPPING_NOTICE_RPA_WORKER_LOG_FILE = Join-Path $dataRoot "logs\worker.log"
SHIPPING_NOTICE_RPA_WORKER_LOG_MAX_BYTES = "10485760"
SHIPPING_NOTICE_RPA_WORKER_LOG_BACKUP_COUNT = "5"
SHIPPING_NOTICE_RPA_WORKER_UPLOAD_ENABLED = "false"
SHIPPING_NOTICE_RPA_WORKER_UPLOAD_CONFIRM_SUBMIT = "false"
}
foreach ($entry in $processVars.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($entry.Key, [string]$entry.Value, "Process")
}
Write-Host "Starting RPA Worker: environment=$Environment scope=$scopeId root=$WorkerRoot"
try {
& $PythonExe $workerScript
exit $LASTEXITCODE
}
finally {
foreach ($entry in $processVars.GetEnumerator()) {
[Environment]::SetEnvironmentVariable($entry.Key, $null, "Process")
}
}