-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.ps1
More file actions
162 lines (138 loc) · 6.1 KB
/
Copy pathwatch.ps1
File metadata and controls
162 lines (138 loc) · 6.1 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
# Knowledge Wiki - Auto-Ingest Watcher
# Startet: .\watch.ps1 (im knowledge-wiki Ordner)
# Stoppt: Strg+C
# Logs: logs\watcher.log
$wikiRoot = $PSScriptRoot
$rawPath = Join-Path $wikiRoot "raw"
$logsDir = Join-Path $wikiRoot "logs"
$logFile = Join-Path $logsDir "watcher.log"
# Logs-Ordner anlegen falls nicht vorhanden
if (-not (Test-Path $logsDir)) { New-Item -ItemType Directory -Path $logsDir | Out-Null }
function Write-Log {
param([string]$Message, [string]$Level = "INFO")
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$line = "$ts $($Level.PadRight(8)) $Message"
Add-Content -Path $logFile -Value $line -Encoding UTF8
switch ($Level) {
"ERROR" { Write-Host $line -ForegroundColor Red }
"WARN" { Write-Host $line -ForegroundColor Yellow }
"INFO" { Write-Host $line -ForegroundColor Cyan }
default { Write-Host $line }
}
}
function Invoke-IngestFile {
param([string]$FilePath, [string]$RelativePath)
Write-Log "Starte Ingest: $RelativePath"
Set-Location $wikiRoot
$result = uv run "$wikiRoot\ingest.py" $FilePath 2>&1
$exitCode = $LASTEXITCODE
foreach ($line in $result) {
Add-Content -Path $logFile -Value $line -Encoding UTF8
Write-Host $line
}
if ($exitCode -eq 0) {
Write-Log "Ingest abgeschlossen: $RelativePath"
} else {
Write-Log "Ingest FEHLGESCHLAGEN (Exit $exitCode): $RelativePath" "ERROR"
}
}
function Invoke-StartupScan {
$cacheDir = Join-Path $rawPath ".cache"
Write-Log "Startup-Scan: prüfe ungeverarbeitete Dateien in raw/ ..."
$supportedExt = @('.txt', '.pptx', '.docx', '.pdf')
$files = Get-ChildItem $rawPath -Recurse -File | Where-Object {
# .cache, manuals und transcripts (jeweils eigene Pipeline) ausschliessen
$_.FullName -notmatch '\\\.cache\\' -and
$_.FullName -notmatch '\\manuals\\' -and
$_.FullName -notmatch '\\transcripts\\' -and
$_.Name -notmatch '~\$' -and
$_.Name -notmatch '\.tmp$' -and
$_.Name -notmatch 'README' -and
$supportedExt -contains $_.Extension.ToLower()
}
$pending = 0
foreach ($file in $files) {
$rel = $file.FullName.Substring($rawPath.Length + 1)
$stem = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$subDir = Split-Path $rel -Parent
if ($subDir) {
$cacheMd = Join-Path $cacheDir "$subDir\$stem.md"
} else {
$cacheMd = Join-Path $cacheDir "$stem.md"
}
if (-not (Test-Path $cacheMd)) {
$pending++
Write-Log "AUSSTEHEND: $rel" "WARN"
Start-Sleep -Seconds 2 # OneDrive-Sync abwarten
Invoke-IngestFile -FilePath $file.FullName -RelativePath $rel
}
}
if ($pending -eq 0) {
Write-Log "Startup-Scan: alle Dateien bereits verarbeitet."
} else {
Write-Log "Startup-Scan abgeschlossen — $pending Datei(en) nachverarbeitet."
}
}
Write-Log "Watcher gestartet — überwache: $rawPath"
Write-Log "Log-Datei: $logFile"
Write-Host "Press Ctrl+C to stop." -ForegroundColor DarkGray
# Beim Start: bereits vorhandene, unverarbeitete Dateien nachholen
Invoke-StartupScan
# FileSystemWatcher für neue Dateien
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $rawPath
$watcher.Filter = "*.*"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
$action = {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
# Ignoriere temp-Dateien, .cache, manuals und transcripts (eigene Pipelines)
if ($name -match '~\$' -or
$name -match '\.tmp$' -or
$name -match 'README' -or
$path -match '\\\.cache\\' -or
$path -match '\\manuals\\' -or
$path -match '\\transcripts\\') {
return
}
if ($changeType -eq "Created") {
$relativePath = $path.Substring($using:wikiRoot.Length + 1)
$wRoot = $using:wikiRoot
$logF = $using:logFile
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logF -Value "$ts INFO Neue Datei erkannt: $relativePath" -Encoding UTF8
Write-Host "$ts INFO Neue Datei erkannt: $relativePath" -ForegroundColor Yellow
# Kurz warten bis Datei vollständig geschrieben ist
Start-Sleep -Seconds 2
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logF -Value "$ts INFO Starte Ingest: $relativePath" -Encoding UTF8
Write-Host "$ts INFO Starte Ingest: $relativePath" -ForegroundColor Green
Set-Location $wRoot
$result = uv run "$wRoot\ingest.py" $path 2>&1
$exitCode = $LASTEXITCODE
foreach ($line in $result) {
Add-Content -Path $logF -Value $line -Encoding UTF8
Write-Host $line
}
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
if ($exitCode -eq 0) {
Add-Content -Path $logF -Value "$ts INFO Ingest abgeschlossen: $relativePath" -Encoding UTF8
Write-Host "$ts INFO Ingest abgeschlossen: $relativePath" -ForegroundColor Green
} else {
Add-Content -Path $logF -Value "$ts ERROR Ingest FEHLGESCHLAGEN (Exit $exitCode): $relativePath" -Encoding UTF8
Write-Host "$ts ERROR Ingest FEHLGESCHLAGEN (Exit $exitCode): $relativePath" -ForegroundColor Red
}
}
}
Register-ObjectEvent $watcher "Created" -Action $action | Out-Null
try {
while ($true) { Start-Sleep -Seconds 1 }
} finally {
$watcher.EnableRaisingEvents = $false
$watcher.Dispose()
$ts = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
Add-Content -Path $logFile -Value "$ts INFO Watcher gestoppt." -Encoding UTF8
Write-Host "Watcher gestoppt." -ForegroundColor DarkGray
}