Files
INFRAESTRUCTURA/scripts/instalar_watchers.ps1

91 lines
3.3 KiB
PowerShell

<#
.SYNOPSIS
Registra en el equipo actual las tareas programadas de los watchers de
infraestructura (Telegram + recordatorios con message box).
.DESCRIPTION
Los watchers viven ahora en `INFRAESTRUCTURA/scripts/` (centralizados; antes
en biblioteca_negocio_prolongo/scripts/). Este instalador crea/actualiza dos
tareas programadas que arrancan al iniciar sesion y se reinician si fallan:
1. `Telegram Watcher` -> scripts\telegram_watcher.py
2. `Recordatorios Msgbox` -> scripts\recordatorios_msgbox.py
No requiere administrador. Idempotente: re-ejecutar tras un git pull.
.NOTES
Repo: INFRAESTRUCTURA. Los datos de estado quedan en INFRAESTRUCTURA\estado\.
#>
[CmdletBinding()]
param(
[switch]$NoStart
)
$ErrorActionPreference = 'Stop'
$RepoRoot = Split-Path -Parent $PSScriptRoot
$PwshExe = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe'
$UserId = "$env:COMPUTERNAME\$env:USERNAME"
# Resolver el interprete de Python
$Python = (Get-Command python -ErrorAction SilentlyContinue).Source
if (-not $Python) { throw 'No se encuentra python en el PATH.' }
$Watchers = @(
@{ Task = 'Telegram Watcher'; Script = Join-Path $RepoRoot 'scripts\telegram_watcher.py' },
@{ Task = 'Recordatorios Msgbox'; Script = Join-Path $RepoRoot 'scripts\recordatorios_msgbox.py' },
@{ Task = 'Shutdown Listener'; Script = Join-Path $RepoRoot 'scripts\shutdown_listener.py' }
)
Write-Host '== Instalando tareas de watchers (INFRAESTRUCTURA) ==' -ForegroundColor Cyan
Write-Host " - Python: $Python"
Write-Host " - Repo : $RepoRoot"
foreach ($w in $Watchers) {
if (-not (Test-Path $w.Script)) { throw "No se encuentra el script: $($w.Script)" }
if (Get-ScheduledTask -TaskName $w.Task -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $w.Task -Confirm:$false
}
$Action = New-ScheduledTaskAction `
-Execute $PwshExe `
-Argument "-NoProfile -NonInteractive -ExecutionPolicy Bypass -WindowStyle Hidden -Command `"& '$Python' '$($w.Script)'`"" `
-WorkingDirectory $RepoRoot
$Trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
$Settings = New-ScheduledTaskSettingsSet `
-AllowStartIfOnBatteries `
-DontStopIfGoingOnBatteries `
-StartWhenAvailable `
-RestartCount 999 `
-RestartInterval (New-TimeSpan -Minutes 1) `
-MultipleInstances IgnoreNew
$Settings.ExecutionTimeLimit = 'PT0S'
$Settings.Hidden = $true
$Principal = New-ScheduledTaskPrincipal -UserId $UserId -LogonType Interactive -RunLevel Limited
Register-ScheduledTask `
-TaskName $w.Task `
-Action $Action `
-Trigger $Trigger `
-Settings $Settings `
-Principal $Principal `
-Description "Watcher de infraestructura: $($w.Script)" `
-Force | Out-Null
Write-Host " - Tarea '$($w.Task)' registrada -> $($w.Script)" -ForegroundColor Green
if (-not $NoStart) {
Start-ScheduledTask -TaskName $w.Task
Write-Host " arrancada." -ForegroundColor Yellow
}
}
Write-Host ''
Write-Host 'Listo. Comprobar:' -ForegroundColor Cyan
Write-Host " Get-ScheduledTask -TaskName 'Telegram Watcher','Recordatorios Msgbox'"
Write-Host " Get-Content `"$RepoRoot\estado\telegram_mensajes.log`" -Tail 5"