diff options
Diffstat (limited to 'packaging/win/service.ps1')
| -rw-r--r-- | packaging/win/service.ps1 | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/packaging/win/service.ps1 b/packaging/win/service.ps1 index 1f84a29..a46bb19 100644 --- a/packaging/win/service.ps1 +++ b/packaging/win/service.ps1 @@ -78,15 +78,28 @@ switch ($Action) { $taskAction = New-ScheduledTaskAction -Execute $node $bootTrigger = New-ScheduledTaskTrigger -AtStartup $taskPrincipal = New-ScheduledTaskPrincipal -UserId $user -LogonType S4U -RunLevel Limited + # Task Scheduler's defaults end a task after 72 hours, never start it on + # battery and stop it when the cable comes out. Kept identical to + # meshbay_node.platform.SERVICE_TASK_SETTINGS. + $taskSettings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit ([TimeSpan]::Zero) ` + -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries ` + -MultipleInstances IgnoreNew -StartWhenAvailable Register-ScheduledTask -TaskName $TASK_NAME -Action $taskAction -Trigger $bootTrigger ` - -Principal $taskPrincipal -Force -ErrorAction Stop | Out-Null + -Principal $taskPrincipal -Settings $taskSettings -Force -ErrorAction Stop | Out-Null Write-Host "service: installed ($user, runs at boot)" } "remove" { + # Deleting a task does not end its running instance. + & schtasks /end /tn $TASK_NAME 2>$null | Out-Null & schtasks /delete /tn $TASK_NAME /f 2>$null | Out-Null Write-Host "service: removed" } "status" { + # Exit 0: installed and current. 1: not installed. 2: installed but + # stale -- it runs another executable than this install's (an older + # install dir, a dev venv), or it still has the 72-hour / battery + # defaults. The installer re-registers a stale task; reading all of this + # needs no admin. $out = & schtasks /query /tn $TASK_NAME /fo list 2>$null if ($LASTEXITCODE -ne 0) { Write-Output "NOT_INSTALLED" @@ -94,6 +107,18 @@ switch ($Action) { } $line = $out | Select-String "^Status:" $state = if ($line) { ($line -replace "^Status:\s*", "").Trim() } else { "unknown" } + $task = Get-ScheduledTask -TaskName $TASK_NAME -ErrorAction SilentlyContinue + $stale = $true + if ($task) { + $exe = ([string]$task.Actions[0].Execute).Trim('"') + $s = $task.Settings + $stale = ($exe -ne $node) -or ($s.ExecutionTimeLimit -ne "PT0S") ` + -or $s.DisallowStartIfOnBatteries -or $s.StopIfGoingOnBatteries + } + if ($stale) { + Write-Output "INSTALLED_STALE:$state" + exit 2 + } Write-Output "INSTALLED:$state" exit 0 } |