Files
inquiry_robot/deploy/_remote_wmi_start_ledger.ps1
T

109 lines
4.5 KiB
PowerShell

# 持久拉起主账:由 WMI 启动 cmd 循环,SSH 断开后仍存活。禁止动现网 8080。
$ErrorActionPreference = 'Stop'
$Root = 'E:\wwwroot\inquiry-new'
$Www = 'E:\wwwroot'
$Jar = Join-Path $Root 'jeecg-system-start-3.5.3.jar'
$LogDir = Join-Path $Root 'logs'
$CfgDir = Join-Path $Root 'config'
$IncomingJar = 'C:\Users\ytd_extdev_0903\incoming\jeecg-system-start-3.5.3.jar'
$IncomingCfg = 'C:\Users\ytd_extdev_0903\incoming\ledger-config.zip'
$Java = 'C:\yutongda\tools\jdk8\bin\java.exe'
New-Item -ItemType Directory -Force -Path $Root, $LogDir, $CfgDir, (Join-Path $Www 'ops') | Out-Null
if (Test-Path $IncomingJar) { Copy-Item $IncomingJar $Jar -Force }
if (-not (Test-Path $Jar)) { throw 'jar missing' }
if (-not (Test-Path $Java)) { throw 'java missing' }
if (Test-Path $IncomingCfg) {
Expand-Archive -Path $IncomingCfg -DestinationPath $CfgDir -Force
}
$inner = @'
$ErrorActionPreference = "Stop"
Get-Content "E:\wwwroot\.env" | ForEach-Object {
$line = $_.Trim()
if (-not $line -or $line.StartsWith("#")) { return }
$i = $line.IndexOf("=")
if ($i -lt 1) { return }
Set-Item -Path ("Env:" + $line.Substring(0, $i).Trim()) -Value $line.Substring($i + 1).Trim().Trim('"')
}
if (-not $env:YTD_REDIS_PASSWORD -and $env:REDIS_URL) {
if ($env:REDIS_URL -match "redis://:([^@]+)@") { $env:YTD_REDIS_PASSWORD = $Matches[1] }
elseif ($env:REDIS_URL -match "redis://[^:]+:([^@]+)@") { $env:YTD_REDIS_PASSWORD = $Matches[1] }
}
if (-not $env:YTD_REDIS_HOST) { $env:YTD_REDIS_HOST = "10.206.0.14" }
if (-not $env:YTD_REDIS_PORT) { $env:YTD_REDIS_PORT = "6379" }
if (-not $env:YTD_REDIS_DB) { $env:YTD_REDIS_DB = "1" }
$env:LEDGER_PORT = "8180"
$Java = "C:\yutongda\tools\jdk8\bin\java.exe"
$Jar = "E:\wwwroot\inquiry-new\jeecg-system-start-3.5.3.jar"
Set-Location "E:\wwwroot\inquiry-new"
$arg = @(
"-jar", $Jar,
"--spring.profiles.active=inquiry",
"--server.port=8180",
"--spring.config.additional-location=optional:file:./config/",
"--cas.prefixUrl=http://127.0.0.1/cas-disabled",
"--jeecg.loginCaptchaEnabled=false"
)
if ($env:YTD_REDIS_USERNAME) { $arg += ("--spring.redis.username=" + $env:YTD_REDIS_USERNAME) }
& $Java @arg
'@
$innerPath = Join-Path $Www 'ops\run-ledger-inner.ps1'
Set-Content -Path $innerPath -Value $inner -Encoding UTF8
$cmd = @"
@echo off
cd /d E:\wwwroot\inquiry-new
:loop
powershell.exe -NoProfile -ExecutionPolicy Bypass -File E:\wwwroot\ops\run-ledger-inner.ps1 >> E:\wwwroot\inquiry-new\logs\stdout.log 2>> E:\wwwroot\inquiry-new\logs\stderr.log
echo ledger exited, restart in 5s >> E:\wwwroot\inquiry-new\logs\stderr.log
timeout /t 5 /nobreak >nul
goto loop
"@
$cmdPath = Join-Path $Www 'ops\run-ledger.cmd'
Set-Content -Path $cmdPath -Value $cmd -Encoding ASCII
# 只停 inquiry-new 的 java
Get-CimInstance Win32_Process -Filter "Name='java.exe'" | ForEach-Object {
if ($_.CommandLine -and $_.CommandLine -like '*inquiry-new*jeecg-system-start*') {
Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
}
}
Get-CimInstance Win32_Process | ForEach-Object {
if ($_.CommandLine -and $_.CommandLine -like '*run-ledger.cmd*') {
Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue
}
}
Start-Sleep -Seconds 2
$w = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
CommandLine = ('cmd.exe /c "' + $cmdPath + '"')
}
Write-Output ("LEDGER_WMI=" + $w.ReturnValue + " pid=" + $w.ProcessId)
$ok = $false
for ($i = 1; $i -le 30; $i++) {
Start-Sleep -Seconds 5
$listen = Get-NetTCPConnection -LocalPort 8180 -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $listen) { Write-Output ("try=" + $i + " not listen"); continue }
try {
$r = Invoke-WebRequest -Uri 'http://127.0.0.1:8180/jeecgboot/sys/randomImage/wmi' -UseBasicParsing -TimeoutSec 5
Write-Output ("try=" + $i + " listen pid=" + $listen.OwningProcess + " http=" + $r.StatusCode)
$ok = $true
break
} catch {
$code = $null
try { $code = $_.Exception.Response.StatusCode.value__ } catch {}
Write-Output ("try=" + $i + " listen pid=" + $listen.OwningProcess + " http_code=" + $code)
if ($code) { $ok = $true; break }
}
}
if (-not $ok) {
Get-Content (Join-Path $LogDir 'stdout.log') -Tail 40 -ErrorAction SilentlyContinue
throw 'ledger 8180 failed to stay up'
}
$prod = Get-NetTCPConnection -LocalPort 8080 -State Listen -ErrorAction SilentlyContinue | Select-Object -First 1
if (-not $prod) { throw 'CRITICAL: prod 8080 down' }
Write-Output 'LEDGER_WMI_OK'
Write-Output ("PROD_8080_OK pid=" + $prod.OwningProcess)