Spaces:
Runtime error

File size: 3,097 Bytes
56c0d82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Keep one local Aureon stack running and one health monitor watching it.
# Task Scheduler (or a WMI-created process) starts this script, so it is not
# tied to a terminal session. Each cycle calls the single launcher; the
# launcher starts only missing components and never duplicates a live pid.
param(
    [int]$IntervalSeconds = 300
)

$ErrorActionPreference = "Continue"
$Root = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path
Set-Location -LiteralPath $Root
$StateDir = Join-Path $Root "state\launcher"
$LogDir = Join-Path $StateDir "logs"
$PidFile = Join-Path $StateDir "service.pid"
$StopFlag = Join-Path $StateDir "service.stop"
$CoreOnlyFlag = Join-Path $StateDir "core-only"
$LogFile = Join-Path $LogDir "stack_service.log"
$Launcher = Join-Path $PSScriptRoot "aureon-local-organism.ps1"

New-Item -ItemType Directory -Force -Path $StateDir | Out-Null
New-Item -ItemType Directory -Force -Path $LogDir | Out-Null

function Rotate-ServiceLog {
    if (-not (Test-Path -LiteralPath $LogFile)) {
        return
    }
    $size = (Get-Item -LiteralPath $LogFile -ErrorAction SilentlyContinue).Length
    if ($size -lt 8MB) {
        return
    }
    $archive = "$LogFile.1"
    Remove-Item -LiteralPath $archive -Force -ErrorAction SilentlyContinue
    Move-Item -LiteralPath $LogFile -Destination $archive -Force -ErrorAction SilentlyContinue
}

function Test-ExistingService {
    if (-not (Test-Path -LiteralPath $PidFile)) {
        return $false
    }
    $raw = Get-Content -LiteralPath $PidFile -ErrorAction SilentlyContinue | Select-Object -First 1
    $serviceProcessId = 0
    if (-not $raw -or -not [int]::TryParse($raw.Trim(), [ref]$serviceProcessId)) {
        return $false
    }
    $process = Get-CimInstance Win32_Process -Filter "ProcessId=$serviceProcessId" -ErrorAction SilentlyContinue
    if (-not $process) {
        return $false
    }
    return ($process.CommandLine -match "aureon-stack-service\.ps1")
}

if (Test-ExistingService) {
    Add-Content -LiteralPath $LogFile -Value "[$(Get-Date -Format o)] another stack service is already running"
    exit 0
}

Set-Content -LiteralPath $PidFile -Value $PID -Encoding ASCII
Add-Content -LiteralPath $LogFile -Value "[$(Get-Date -Format o)] stack service started (pid $PID, interval ${IntervalSeconds}s)"

try {
    while (-not (Test-Path -LiteralPath $StopFlag)) {
        Rotate-ServiceLog
        Add-Content -LiteralPath $LogFile -Value "[$(Get-Date -Format o)] launcher cycle"
        try {
            $LauncherArgs = @()
            if (Test-Path -LiteralPath $CoreOnlyFlag) {
                $LauncherArgs += "-CoreOnly"
            }
            & $Launcher @LauncherArgs *>> $LogFile
        } catch {
            Add-Content -LiteralPath $LogFile -Value "[$(Get-Date -Format o)] launcher failed: $($_.Exception.Message)"
        }
        Start-Sleep -Seconds ([Math]::Max(30, $IntervalSeconds))
    }
    Add-Content -LiteralPath $LogFile -Value "[$(Get-Date -Format o)] stop flag seen; stack service exiting"
} finally {
    Remove-Item -LiteralPath $PidFile -Force -ErrorAction SilentlyContinue
}