# Windows Scheduled Task Setup for CNPERP Auto-Update

This guide sets up automatic nightly updates for the CNPERP application on Windows.

## Quick Setup (PowerShell as Administrator)

Copy and paste this **entire block** into PowerShell (running as Administrator):

```powershell
$TaskName = "CNPERP Auto-Update"
$ScriptPath = "C:\dev\cnperp-dimensions\deploy\auto_update.ps1"
$LogPath = "C:\dev\cnperp-dimensions\logs\auto_update.log"

# Create scheduled task to run daily at 2:00 AM
$Trigger = New-ScheduledTaskTrigger -Daily -At 2:00AM
$Action = New-ScheduledTaskAction `
    -Execute "powershell.exe" `
    -Argument "-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$ScriptPath`""
$Settings = New-ScheduledTaskSettingsSet `
    -StartWhenAvailable `
    -RunOnlyIfNetworkAvailable `
    -MultipleInstances IgnoreNew `
    -WakeToRun:$false

Register-ScheduledTask `
    -TaskName $TaskName `
    -Trigger $Trigger `
    -Action $Action `
    -Settings $Settings `
    -Description "Automatic CNPERP updates: pulls latest code, updates dependencies, restarts service" `
    -User "SYSTEM" `
    -RunLevel Highest `
    -Force | Out-Null

Write-Host "✅ Scheduled task '$TaskName' created successfully!" -ForegroundColor Green
Write-Host "   Time: Daily at 2:00 AM" -ForegroundColor Green
Write-Host "   Script: $ScriptPath" -ForegroundColor Green
Write-Host "   Logs: $LogPath" -ForegroundColor Green
```

## Manual Setup (GUI)

If you prefer the graphical interface:

1. **Open Task Scheduler**
   - Press `Win + R` → type `taskschd.msc` → Enter
   - Or: Admin Tools → Task Scheduler

2. **Create Basic Task**
   - Right-click "Task Scheduler Library" → New Folder → Name: `CNPERP`
   - Right-click the `CNPERP` folder → Create Basic Task

3. **Configure Trigger**
   - Name: `CNPERP Auto-Update`
   - Trigger: Daily → 2:00:00 AM
   - Checkbox: "Wake the computer to run this task" (optional, unchecked is fine)

4. **Configure Action**
   - Action: Start a program
   - Program: `powershell.exe`
   - Arguments: `-NoProfile -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\dev\cnperp-dimensions\deploy\auto_update.ps1"`

5. **Configure Settings**
   - Run with highest privileges: ✓ Checked
   - Run whether user is logged in or not: ✓ Checked
   - If task fails, restart every: 5 minutes (up to 3 times)
   - Stop if running longer than: 1 hour

6. **Finish & Verify**
   - Click Finish
   - Right-click the new task → Run (test it)
   - Check logs at: `C:\dev\cnperp-dimensions\logs\auto_update.log`

## Verify It's Working

### Check Scheduled Task
```powershell
Get-ScheduledTask -TaskName "CNPERP Auto-Update" | Select-Object State, LastRunTime, LastTaskResult
```

### View Logs
```powershell
Get-Content "C:\dev\cnperp-dimensions\logs\auto_update.log" -Tail 30
```

### Manual Test Run
```powershell
Start-ScheduledTask -TaskName "CNPERP Auto-Update"
Start-Sleep -Seconds 10
Get-Content "C:\dev\cnperp-dimensions\logs\auto_update.log" -Tail 30
```

## What Happens During Auto-Update

✅ **2:00 AM (Nightly)**
1. Pre-update database backup created
2. Git pulls latest code from `origin/main`
3. Python dependencies updated (`pip install -r requirements.txt --upgrade`)
4. CNPERP Windows Service restarted
5. Log written to `logs/auto_update.log`

⏱️ **Duration**: ~30-60 seconds (downtime)
🔄 **Automatic Recovery**: If service fails to start, task retries in 5 minutes
📧 **Notifications**: Logs only (email optional, see script)

## Rollback (if needed)

If an update causes issues, someone at the office can manually rollback:

```powershell
cd C:\dev\cnperp-dimensions
git reset --hard HEAD@{1}          # Undo the update
Restart-Service CNPERP            # Restart service
```

## Disable Auto-Updates

To temporarily pause automatic updates:

```powershell
Disable-ScheduledTask -TaskName "CNPERP Auto-Update"
```

To re-enable:

```powershell
Enable-ScheduledTask -TaskName "CNPERP Auto-Update"
```

## Remove Scheduled Task

If you need to remove the scheduled task entirely:

```powershell
Unregister-ScheduledTask -TaskName "CNPERP Auto-Update" -Confirm:$false
```

---

**Questions?** Check `logs/auto_update.log` for details on any update run.
