-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-watchers.ps1
More file actions
198 lines (165 loc) · 7.26 KB
/
Copy pathsync-watchers.ps1
File metadata and controls
198 lines (165 loc) · 7.26 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# sync-watchers.ps1 - Synchronisiert Scheduled Tasks mit watchers.json
#
# Liest $VAULT_ROOT\watchers.json (oder -WatchersFile) und stellt sicher,
# dass fuer jeden Eintrag ein Scheduled Task 'KnowledgeTree-<name>' existiert
# und den konfigurierten Parametern entspricht. Entfernt Tasks, deren Eintrag
# aus der Config entfernt wurde (schuetzt 'KnowledgeTree-MetaSync' selbst).
#
# Wird von setup.ps1 als Task 'KnowledgeTree-MetaSync' alle 15 Min getriggert,
# damit Aenderungen an watchers.json ohne Neuausfuehrung von setup.ps1 wirken.
#
# Usage:
# .\sync-watchers.ps1
# .\sync-watchers.ps1 -WatchersFile C:\pfad\watchers.json
param(
[string]$WatchersFile = ""
)
$ErrorActionPreference = "Stop"
# Geschuetzter Task-Name — wird nie entfernt (das ist unser eigener Trigger).
$MetaTaskName = "KnowledgeTree-MetaSync"
$TaskPrefix = "KnowledgeTree-"
# -- Config-Pfad ermitteln ---------------------------------------------------
function Resolve-WatchersFile {
param([string]$Explicit)
# Explizit uebergebener Pfad MUSS existieren — sonst koennte ein
# tippfehlerhafter/transient nicht erreichbarer Pfad spaeter dazu
# fuehren, dass die "Remove obsolete"-Schleife alle Custom-Tasks loescht.
if ($Explicit) {
if (Test-Path $Explicit) { return $Explicit }
return $null
}
# Versuche $env:VAULT_ROOT (aus .env gesetzt)
if ($env:VAULT_ROOT -and (Test-Path (Join-Path $env:VAULT_ROOT "watchers.json"))) {
return Join-Path $env:VAULT_ROOT "watchers.json"
}
# .env im Skript-Verzeichnis lesen
$envFile = Join-Path $PSScriptRoot ".env"
if (Test-Path $envFile) {
$vaultLine = (Get-Content $envFile) | Where-Object { $_ -match '^VAULT_ROOT=' } | Select-Object -First 1
if ($vaultLine) {
$vault = ($vaultLine -replace '^VAULT_ROOT=', '').Trim().Trim('"')
$candidate = Join-Path $vault "watchers.json"
if (Test-Path $candidate) { return $candidate }
}
}
# KEIN Fallback auf das Repo-Template: das wuerde nur die Default-Eintraege
# laden und im Anschluss alle haendig ergaenzten Watcher-Tasks als
# "obsolet" loeschen. Lieber abbrechen.
return $null
}
$path = Resolve-WatchersFile -Explicit $WatchersFile
if (-not $path) {
if ($WatchersFile) {
Write-Host "sync-watchers: -WatchersFile '$WatchersFile' nicht gefunden. Abbruch ohne Aenderungen." -ForegroundColor Red
} else {
Write-Host "sync-watchers: keine Vault-watchers.json gefunden (VAULT_ROOT gesetzt? .env vorhanden?). Abbruch ohne Aenderungen." -ForegroundColor Red
}
exit 1
}
Write-Host "sync-watchers: Config = $path"
try {
$config = Get-Content $path -Raw -Encoding UTF8 | ConvertFrom-Json
} catch {
Write-Host "sync-watchers: watchers.json ungueltig: $_" -ForegroundColor Red
exit 1
}
if (-not $config.watchers) {
Write-Host "sync-watchers: keine watchers-Eintraege." -ForegroundColor Yellow
exit 0
}
# -- Soll-Zustand aus Config -------------------------------------------------
$desired = @{}
foreach ($w in $config.watchers) {
if (-not $w.name) { continue }
$desired["$TaskPrefix$($w.name)"] = $w
}
# -- Ist-Zustand aus Task Scheduler ------------------------------------------
$existing = @(Get-ScheduledTask -ErrorAction SilentlyContinue |
Where-Object { $_.TaskName -like "$TaskPrefix*" })
$existingNames = @($existing | ForEach-Object { $_.TaskName })
# -- Register / Update -------------------------------------------------------
foreach ($name in $desired.Keys) {
$w = $desired[$name]
$cwd = if ($w.cwd) { $w.cwd } else { $PSScriptRoot }
$scriptPath = Join-Path $cwd $w.script
if (-not (Test-Path $scriptPath)) {
Write-Host " ! $name : Skript nicht gefunden ($scriptPath)" -ForegroundColor Red
continue
}
$runner = if ($w.runner) { $w.runner } else { "uv" }
$triggerType = if ($w.trigger) { $w.trigger } else { "interval" }
$desc = if ($w.description) { $w.description } else { "Knowledge Tree Watcher: $($w.name)" }
$interval = if ($w.interval_minutes) { $w.interval_minutes } else { 15 }
$timeout = if ($w.timeout_minutes) { $w.timeout_minutes } else { 30 }
try {
# -- Action: runner bestimmt, wie das Skript aufgerufen wird ----------
switch ($runner) {
"powershell" {
$extraArgs = if ($w.args) { " " + ($w.args -join " ") } else { "" }
$action = New-ScheduledTaskAction `
-Execute "powershell.exe" `
-Argument "-ExecutionPolicy Bypass -NoProfile -WindowStyle Hidden -File `"$scriptPath`"$extraArgs" `
-WorkingDirectory $cwd
}
default {
# "uv" (default): uv run <script> <args...>
$argList = @("run", "`"$scriptPath`"")
if ($w.args) { $argList += $w.args }
$action = New-ScheduledTaskAction `
-Execute "uv" `
-Argument ($argList -join " ") `
-WorkingDirectory $cwd
}
}
# -- Trigger + Settings: trigger-Type bestimmt das Takt-Modell --------
switch ($triggerType) {
"at_logon" {
# Langlaufender Daemon, startet beim User-Login.
$trigger = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
# ExecutionTimeLimit 0 = unbegrenzt
$settings = New-ScheduledTaskSettingsSet `
-MultipleInstances IgnoreNew `
-ExecutionTimeLimit (New-TimeSpan -Hours 0) `
-StartWhenAvailable
$modeLabel = "at_logon"
}
default {
# "interval": Single-Poll alle N Minuten
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes $interval) `
-RepetitionDuration (New-TimeSpan -Days 9999)
$settings = New-ScheduledTaskSettingsSet `
-MultipleInstances IgnoreNew `
-ExecutionTimeLimit (New-TimeSpan -Minutes $timeout) `
-StartWhenAvailable
$modeLabel = "alle $interval Min"
}
}
Register-ScheduledTask `
-TaskName $name `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Description $desc `
-Force | Out-Null
if ($existingNames -contains $name) {
Write-Host " UPDATE $name ($modeLabel)" -ForegroundColor DarkCyan
} else {
Write-Host " CREATE $name ($modeLabel)" -ForegroundColor Green
}
} catch {
Write-Host " ! $name konnte nicht registriert werden: $_" -ForegroundColor Red
}
}
# -- Remove obsolete ---------------------------------------------------------
foreach ($task in $existing) {
$n = $task.TaskName
if ($n -eq $MetaTaskName) { continue } # niemals selbst entfernen
if ($desired.ContainsKey($n)) { continue } # noch in Config
try {
Unregister-ScheduledTask -TaskName $n -Confirm:$false
Write-Host " DELETE $n (nicht mehr in Config)" -ForegroundColor Yellow
} catch {
Write-Host " ! $n konnte nicht entfernt werden: $_" -ForegroundColor Red
}
}