From de2ad53e48cfd145b5505e20831bf513749c5884 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 12 Jun 2026 12:46:44 +0100 Subject: [PATCH] Run Vili through a Raven scheduled task --- scripts/install-vili-task.ps1 | 16 ++++++++++++++++ scripts/restart-vili-daemon.ps1 | 15 ++++++++++++++- scripts/run-vili-daemon.ps1 | 17 +++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 scripts/install-vili-task.ps1 create mode 100644 scripts/run-vili-daemon.ps1 diff --git a/scripts/install-vili-task.ps1 b/scripts/install-vili-task.ps1 new file mode 100644 index 0000000..595d2dd --- /dev/null +++ b/scripts/install-vili-task.ps1 @@ -0,0 +1,16 @@ +param( + [string]$TaskName = "GameCult\Vili", + [int]$Port = 8824, + [string]$HostName = "0.0.0.0", + [string]$StateRoot = "E:\Projects\Vili\.vili" +) + +$ErrorActionPreference = "Stop" +$RunScript = Join-Path $PSScriptRoot "run-vili-daemon.ps1" +$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$RunScript`" -Port $Port -HostName `"$HostName`" -StateRoot `"$StateRoot`"" +$Trigger = New-ScheduledTaskTrigger -AtStartup +$Principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U -RunLevel Highest +$Settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -ExecutionTimeLimit ([TimeSpan]::Zero) -MultipleInstances IgnoreNew + +Register-ScheduledTask -TaskName $TaskName -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings -Force | Out-Null +Write-Host "Installed scheduled task $TaskName" diff --git a/scripts/restart-vili-daemon.ps1 b/scripts/restart-vili-daemon.ps1 index ee9874e..9db76a3 100644 --- a/scripts/restart-vili-daemon.ps1 +++ b/scripts/restart-vili-daemon.ps1 @@ -6,6 +6,7 @@ param( $ErrorActionPreference = "Stop" $PidPath = Join-Path $StateRoot "vili-daemon.pid" +$TaskName = "GameCult\Vili" if (Test-Path $PidPath) { $existingPid = (Get-Content $PidPath -ErrorAction SilentlyContinue | Select-Object -First 1) @@ -18,4 +19,16 @@ if (Test-Path $PidPath) { } } -& (Join-Path $PSScriptRoot "start-vili-daemon.ps1") -Port $Port -HostName $HostName -StateRoot $StateRoot +if (Get-ScheduledTask -TaskName "Vili" -TaskPath "\GameCult\" -ErrorAction SilentlyContinue) { + Stop-ScheduledTask -TaskName "Vili" -TaskPath "\GameCult\" -ErrorAction SilentlyContinue + Start-Sleep -Seconds 1 + Start-ScheduledTask -TaskName "Vili" -TaskPath "\GameCult\" + Start-Sleep -Seconds 3 + node (Join-Path (Split-Path -Parent $PSScriptRoot) "scripts\vili-daemon.mjs") --health --host 127.0.0.1 --port $Port --state-root $StateRoot + if ($LASTEXITCODE -ne 0) { + throw "Vili scheduled task started but health check failed." + } + Write-Host "Vili restarted through scheduled task $TaskName" +} else { + & (Join-Path $PSScriptRoot "start-vili-daemon.ps1") -Port $Port -HostName $HostName -StateRoot $StateRoot +} diff --git a/scripts/run-vili-daemon.ps1 b/scripts/run-vili-daemon.ps1 new file mode 100644 index 0000000..0e10cba --- /dev/null +++ b/scripts/run-vili-daemon.ps1 @@ -0,0 +1,17 @@ +param( + [int]$Port = 8824, + [string]$HostName = "0.0.0.0", + [string]$StateRoot = "E:\Projects\Vili\.vili" +) + +$ErrorActionPreference = "Stop" +$Root = Split-Path -Parent $PSScriptRoot +$Node = (Get-Command node -ErrorAction Stop).Source +$LogDir = Join-Path $StateRoot "logs" +$OutLog = Join-Path $LogDir "vili-daemon.out.log" +$ErrLog = Join-Path $LogDir "vili-daemon.err.log" + +New-Item -ItemType Directory -Force -Path $LogDir | Out-Null + +Set-Location $Root +& $Node (Join-Path $Root "scripts\vili-daemon.mjs") --host $HostName --port $Port --state-root $StateRoot 1>> $OutLog 2>> $ErrLog