- 新增 erp-remote.ps1:上传与远程执行统一重试(默认 3 次,重试前重新上传 CSV) - run-erp-daily.ps1:导出失败重试一次;导出/转CSV/笔数核对/上传/导入回验失败都不写状态、不归档,退出码 1,下次自动重导 - 实测:真实 SSH 断连两次后第 3 次重试成功(8002 笔 / 283870.49 / 46435.6);连续失败的一次按新逻辑记失败并 exit 1
78 lines
3.2 KiB
PowerShell
78 lines
3.2 KiB
PowerShell
<#
|
||
每日账单脚本的远程执行辅助:上传 + 远程执行 + 失败重试。
|
||
|
||
远端命令失败(包括 SSH 连接被服务器断开)时不再静默当成功:
|
||
统一返回 { Ok; Output; Attempts },由调用方决定是否写本地状态、是否归档。
|
||
上传与远程执行都按 Attempts 重试;重试时会重新上传 CSV,
|
||
避免上一次失败已经把远端文件删掉导致二次失败。
|
||
#>
|
||
|
||
function New-ErpRemoteResult {
|
||
param([bool]$Ok, [string]$Output, [int]$Attempts)
|
||
|
||
return [pscustomobject]@{ Ok = $Ok; Output = $Output; Attempts = $Attempts }
|
||
}
|
||
|
||
function Invoke-ErpRemoteUpload {
|
||
param(
|
||
[Parameter(Mandatory = $true)][string]$PwshPath,
|
||
[Parameter(Mandatory = $true)][string]$UploaderScript,
|
||
[Parameter(Mandatory = $true)][string]$LocalFile,
|
||
[Parameter(Mandatory = $true)][string]$RemoteFile
|
||
)
|
||
|
||
$out = (& $PwshPath -NoProfile -ExecutionPolicy Bypass -File $UploaderScript -LocalPath $LocalFile -RemotePath $RemoteFile 2>&1 | Out-String)
|
||
|
||
return (New-ErpRemoteResult -Ok ($LASTEXITCODE -eq 0) -Output $out -Attempts 1)
|
||
}
|
||
|
||
function Invoke-ErpRemote {
|
||
param(
|
||
[Parameter(Mandatory = $true)][string]$PwshPath,
|
||
[Parameter(Mandatory = $true)][string]$SshScript,
|
||
[Parameter(Mandatory = $true)][string]$Body,
|
||
[string]$LocalFile = '',
|
||
[string]$RemoteFile = '',
|
||
[string]$UploaderScript = '',
|
||
[int]$Attempts = 3,
|
||
[int]$RetryDelaySeconds = 10,
|
||
[scriptblock]$Log = $null
|
||
)
|
||
|
||
$write = { param([string]$Message) if ($Log) { & $Log $Message } }
|
||
$tmp = [IO.Path]::GetTempFileName() + '.sh'
|
||
[IO.File]::WriteAllText($tmp, ($Body -replace "`r`n", "`n"))
|
||
try {
|
||
$output = ''
|
||
for ($attempt = 1; $attempt -le $Attempts; $attempt++) {
|
||
if ($LocalFile -and $RemoteFile -and $UploaderScript) {
|
||
$upload = Invoke-ErpRemoteUpload -PwshPath $PwshPath -UploaderScript $UploaderScript -LocalFile $LocalFile -RemoteFile $RemoteFile
|
||
if (-not $upload.Ok) {
|
||
$output = $upload.Output
|
||
& $write ('上传失败(第 {0}/{1} 次):{2}' -f $attempt, $Attempts, ($output.Trim() -replace "`r?`n", ' | '))
|
||
if ($attempt -lt $Attempts) {
|
||
Start-Sleep -Seconds $RetryDelaySeconds
|
||
continue
|
||
}
|
||
|
||
return (New-ErpRemoteResult -Ok $false -Output $output -Attempts $attempt)
|
||
}
|
||
}
|
||
|
||
$output = (& $PwshPath -NoProfile -ExecutionPolicy Bypass -File $SshScript -ScriptPath $tmp 2>&1 | Out-String)
|
||
if ($LASTEXITCODE -eq 0) {
|
||
return (New-ErpRemoteResult -Ok $true -Output $output -Attempts $attempt)
|
||
}
|
||
|
||
& $write ('远程执行失败(第 {0}/{1} 次,exit={2}):{3}' -f $attempt, $Attempts, $LASTEXITCODE, ($output.Trim() -replace "`r?`n", ' | '))
|
||
if ($attempt -lt $Attempts) {
|
||
Start-Sleep -Seconds $RetryDelaySeconds
|
||
}
|
||
}
|
||
|
||
return (New-ErpRemoteResult -Ok $false -Output $output -Attempts $Attempts)
|
||
} finally {
|
||
Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue
|
||
}
|
||
}
|