Skip to content

数据循环的启动问题 #245

@Vip4pt

Description

@Vip4pt

由于我使用git的 base窗口执行之后,看板一直无法连接
所以我弄了一个powershell的脚本去启动,并且成功连接

param(
    [int]$Interval = 15,
    [int]$ScanInterval = 120
)

# ── 基础配置 ──
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
if (-not $env:EDICT_HOME) {
    $env:EDICT_HOME = Split-Path -Parent $ScriptDir
}

$Log = "$env:TEMP\sansheng_liubu_refresh.log"
$PidFile = "$env:TEMP\sansheng_liubu_refresh.pid"
$MaxLogSize = 10MB
$ScriptTimeout = 30
$DashboardPort = $env:EDICT_DASHBOARD_PORT
if (-not $DashboardPort) { $DashboardPort = 7891 }

# ── 单实例保护 ──
if (Test-Path $PidFile) {
    $OldPid = Get-Content $PidFile -ErrorAction SilentlyContinue
    if ($OldPid -and (Get-Process -Id $OldPid -ErrorAction SilentlyContinue)) {
        Write-Host "❌ 已有实例运行中 (PID=$OldPid),退出"
        exit 1
    }
    Remove-Item $PidFile -Force -ErrorAction SilentlyContinue
}
$PID | Out-File $PidFile

# ── 优雅退出 ──
$cleanup = {
    "$(Get-Date -Format HH:mm:ss) [loop] 收到退出信号,清理中..." | Out-File $Log -Append
    Remove-Item $PidFile -Force -ErrorAction SilentlyContinue
    exit
}

Register-EngineEvent PowerShell.Exiting -Action $cleanup | Out-Null

# ── 日志轮转 ──
function Rotate-Log {
    if (Test-Path $Log) {
        $size = (Get-Item $Log).Length
        if ($size -gt $MaxLogSize) {
            Move-Item $Log "$Log.1" -Force
            "$(Get-Date -Format HH:mm:ss) [loop] 日志已轮转" | Out-File $Log
        }
    }
}

# ── 安全执行(带超时)──
function Safe-Run($script) {
    $psi = New-Object System.Diagnostics.ProcessStartInfo
    $psi.FileName = "python"
    $psi.Arguments = "`"$script`""
    $psi.RedirectStandardOutput = $true
    $psi.RedirectStandardError = $true
    $psi.UseShellExecute = $false

    $process = New-Object System.Diagnostics.Process
    $process.StartInfo = $psi
    $process.Start() | Out-Null

    if (-not $process.WaitForExit($ScriptTimeout * 1000)) {
        try {
            $process.Kill()
            "$(Get-Date -Format HH:mm:ss) [loop] ⚠️ 脚本超时(${ScriptTimeout}s): $script" | Out-File $Log -Append
        } catch {}
    }

    $stdout = $process.StandardOutput.ReadToEnd()
    $stderr = $process.StandardError.ReadToEnd()

    if ($stdout) { $stdout | Out-File $Log -Append }
    if ($stderr) { $stderr | Out-File $Log -Append }
}

# ── 启动信息 ──
Write-Host "🏛️  三省六部数据刷新循环启动 (PID=$PID)"
Write-Host "   脚本目录: $ScriptDir"
Write-Host "   间隔: ${Interval}s"
Write-Host "   巡检间隔: ${ScanInterval}s"
Write-Host "   脚本超时: ${ScriptTimeout}s"
Write-Host "   日志: $Log"
Write-Host "   PID文件: $PidFile"
Write-Host "   Ctrl+C 停止"

$ScanCounter = 0

# ── 主循环 ──
while ($true) {
    Rotate-Log

    Safe-Run "$ScriptDir\sync_from_openclaw_runtime.py"
    Safe-Run "$ScriptDir\sync_agent_config.py"
    Safe-Run "$ScriptDir\apply_model_changes.py"
    Safe-Run "$ScriptDir\sync_officials_stats.py"
    Safe-Run "$ScriptDir\refresh_live_data.py"

    # ── 巡检任务 ──
    $ScanCounter += $Interval
    if ($ScanCounter -ge $ScanInterval) {
        $ScanCounter = 0
        try {
            Invoke-RestMethod -Uri "http://127.0.0.1:$DashboardPort/api/scheduler-scan" `
                -Method POST `
                -ContentType "application/json" `
                -Body '{"thresholdSec":180}' | Out-Null
        } catch {
            $_ | Out-File $Log -Append
        }
    }

    Start-Sleep -Seconds $Interval
}

保存到run_loop.sh同样的目录下改名为run_loop.ps1就好了

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions