-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_script.ps1
More file actions
112 lines (94 loc) · 4.33 KB
/
monitor_script.ps1
File metadata and controls
112 lines (94 loc) · 4.33 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
# Script to monitor and automatically restart mcp_md_done.ps1 if it exits
$scriptPath = Join-Path $PSScriptRoot "mcp_md_done.ps1"
$maxRetries = 100000 # Maximum number of restart attempts
$retryCount = 0
$waitTime = 5 # Seconds to wait between restart attempts
$logFile = Join-Path $PSScriptRoot "monitor_log.txt"
# Create or append to log file
function Write-Log {
param (
[string]$Message
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$timestamp - $Message" | Out-File -FilePath $logFile -Append
Write-Host "$timestamp - $Message"
}
Write-Log "Starting monitoring for $scriptPath"
Write-Log "Press Ctrl+C to stop monitoring"
# Check if .env file exists and load it
$envFile = Join-Path $PSScriptRoot ".env"
if (Test-Path $envFile) {
Write-Log "Found .env configuration file"
# Load environment variables from .env
Get-Content $envFile | ForEach-Object {
if ($_ -match '^\s*([^#][^=]+)=(.*)$') {
$key = $matches[1].Trim()
$value = $matches[2].Trim()
# Remove quotes if present
if ($value -match '^["''](.*)["'']$') {
$value = $matches[1]
}
[Environment]::SetEnvironmentVariable($key, $value)
}
}
# Verify LM Studio endpoint format
$lmStudioEndpoint = $env:LMSTUDIO_ENDPOINT
if ($lmStudioEndpoint -and -not $lmStudioEndpoint.EndsWith("/v1/chat/completions")) {
Write-Log "Warning: LMSTUDIO_ENDPOINT doesn't end with '/v1/chat/completions'. This may cause API errors."
Write-Log "Current endpoint: $lmStudioEndpoint"
Write-Log "Recommended format: http://host:port/v1/chat/completions"
# Auto-fix the endpoint if it doesn't have the correct path
if ($lmStudioEndpoint -match '^(https?://[^/]+)/?$') {
$fixedEndpoint = "$($matches[1])/v1/chat/completions"
Write-Log "Auto-fixing endpoint to: $fixedEndpoint"
[Environment]::SetEnvironmentVariable("LMSTUDIO_ENDPOINT", $fixedEndpoint)
# Update the .env file
$envContent = Get-Content -Path $envFile
$updatedContent = $envContent -replace "LMSTUDIO_ENDPOINT=.*", "LMSTUDIO_ENDPOINT=$fixedEndpoint"
$updatedContent | Set-Content -Path $envFile
Write-Log "Updated .env file with corrected endpoint"
}
}
} else {
Write-Log "Warning: .env file not found at $envFile. Using default configuration."
}
while ($retryCount -lt $maxRetries) {
try {
Write-Log "Attempt $($retryCount + 1) of $($maxRetries): Starting script..."
# Start PowerShell 7 with elevated privileges to run the script
# When using -Verb RunAs, we can't use -NoNewWindow
$process = Start-Process pwsh -ArgumentList "-File `"$scriptPath`"" -PassThru -Verb RunAs
# Wait for the process to exit
$process.WaitForExit()
$exitCode = $process.ExitCode
Write-Log "Script exited with code: $exitCode"
# Check for specific error codes
if ($exitCode -eq 1) {
Write-Log "Error detected. Checking error logs..."
$errorFiles = Get-ChildItem -Path (Split-Path $scriptPath) -Filter "Error_*.json" |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
if ($errorFiles) {
$errorContent = Get-Content -Path $errorFiles.FullName -Raw
Write-Log "Latest error: $errorContent"
# Check for API endpoint errors
if ($errorContent -match "Unexpected endpoint" -or $errorContent -match "404 Not Found") {
Write-Log "API endpoint error detected. Please check LMSTUDIO_ENDPOINT in .env file."
}
}
}
# Increment retry counter
$retryCount++
# Wait before restarting
Write-Log "Waiting $waitTime seconds before restarting..."
Start-Sleep -Seconds $waitTime
}
catch {
Write-Log "Error monitoring script: $_"
$retryCount++
Start-Sleep -Seconds $waitTime
}
}
Write-Log "Maximum retry attempts ($maxRetries) reached. Monitoring stopped."
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")