-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstart_dev.ps1
More file actions
427 lines (378 loc) · 18.7 KB
/
Copy pathstart_dev.ps1
File metadata and controls
427 lines (378 loc) · 18.7 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# Minimal local launcher for the MoonStone development stack.
param(
[switch]$Verify,
[switch]$GalGameMock
)
$ErrorActionPreference = 'Stop'
$projectRoot = $PSScriptRoot
$backendRoot = Join-Path $projectRoot 'backend'
$runtimeDirectory = Join-Path $projectRoot '.runtime'
$bugReportDirectory = Join-Path $projectRoot 'logs'
$gatewayPort = 5000
$gatewayBaseUrl = "http://127.0.0.1:$gatewayPort"
$moonStonePorts = $gatewayPort, 5101, 5102, 5103, 5104, 5105, 5106, 5107, 5108, 5109, 5121
$isMockMode = [string]::Equals($env:MOONSTONE_MODE, 'Mock', [System.StringComparison]::OrdinalIgnoreCase)
$useGalGameMock = $isMockMode -or $GalGameMock
$neo4jPassword = if ([string]::IsNullOrWhiteSpace($env:NEO4J_PASSWORD)) { 'knowledge-dev-password' } else { $env:NEO4J_PASSWORD }
function Normalize-ProcessPath {
$environment = [Environment]::GetEnvironmentVariables([EnvironmentVariableTarget]::Process)
$pathValues = @($environment.GetEnumerator() | Where-Object { [string]$_.Key -ieq 'PATH' } | ForEach-Object { [string]$_.Value })
if ($pathValues.Count -le 1) { return }
$seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$segments = foreach ($value in $pathValues) {
foreach ($segment in ($value -split ';')) {
$trimmed = $segment.Trim()
if ($trimmed -and $seen.Add($trimmed)) { $trimmed }
}
}
[Environment]::SetEnvironmentVariable('PATH', $null, [EnvironmentVariableTarget]::Process)
[Environment]::SetEnvironmentVariable('Path', ($segments -join ';'), [EnvironmentVariableTarget]::Process)
}
function Stop-MoonStoneServices {
$resolvedProjectRoot = [IO.Path]::GetFullPath($projectRoot).TrimEnd('\', '/')
$rootWithSeparator = $resolvedProjectRoot + [IO.Path]::DirectorySeparatorChar
$processSnapshot = @{}
try {
foreach ($item in @(Get-CimInstance Win32_Process -ErrorAction Stop)) {
$processSnapshot[[int]$item.ProcessId] = $item
}
}
catch {
Write-Warning "Process ownership details are unavailable; generic Node and dotnet processes will be preserved. $($_.Exception.Message)"
}
$recordedProcessIds = [System.Collections.Generic.HashSet[int]]::new()
$manifestPath = Join-Path $runtimeDirectory 'project-processes.json'
if (Test-Path -LiteralPath $manifestPath -PathType Leaf) {
try {
$parsedManifest = Get-Content -LiteralPath $manifestPath -Raw | ConvertFrom-Json
foreach ($manifestItem in @($parsedManifest)) {
$manifestProcessId = [int]$manifestItem.ProcessId
$runningProcess = Get-Process -Id $manifestProcessId -ErrorAction SilentlyContinue
if ($null -eq $runningProcess) { continue }
$manifestStartTime = [DateTime]::MinValue
$hasStartTime = [DateTime]::TryParse(
[string]$manifestItem.StartedAtUtc,
[Globalization.CultureInfo]::InvariantCulture,
[Globalization.DateTimeStyles]::RoundtripKind,
[ref]$manifestStartTime
)
if (-not $hasStartTime) { continue }
try {
$actualStartTime = $runningProcess.StartTime.ToUniversalTime()
if ([Math]::Abs(($actualStartTime - $manifestStartTime.ToUniversalTime()).TotalSeconds) -le 5) {
[void]$recordedProcessIds.Add($manifestProcessId)
}
}
catch {
}
}
}
catch {
Write-Warning "Could not read process manifest '$manifestPath': $($_.Exception.Message)"
}
}
function Test-IsMoonStoneProcess {
param(
[Parameter(Mandatory)][int]$ProcessId,
[Parameter(Mandatory)][string]$ProcessName
)
$projectSpecificNames = @(
'GalGame.AuthService',
'GalGame.FileService',
'GalGame.UserService',
'GalGame.GalGameService',
'KnowledgeService.API',
'PracticeService.API',
'CreditService.API'
)
if ($projectSpecificNames -contains $ProcessName) { return $true }
$cursor = $ProcessId
$seen = [System.Collections.Generic.HashSet[int]]::new()
while ($cursor -gt 0 -and $seen.Add($cursor)) {
if ($recordedProcessIds.Contains($cursor)) { return $true }
if (-not $processSnapshot.ContainsKey($cursor)) { break }
$cursor = [int]$processSnapshot[$cursor].ParentProcessId
}
if (-not $processSnapshot.ContainsKey($ProcessId)) { return $false }
$details = $processSnapshot[$ProcessId]
$executablePath = [string]$details.ExecutablePath
$commandLine = [string]$details.CommandLine
return ($executablePath -and $executablePath.StartsWith($rootWithSeparator, [StringComparison]::OrdinalIgnoreCase)) -or
($commandLine -and $commandLine.IndexOf($resolvedProjectRoot, [StringComparison]::OrdinalIgnoreCase) -ge 0)
}
foreach ($port in $moonStonePorts) {
$processIds = netstat -ano -p TCP |
ForEach-Object {
$parts = @($_ -split '\s+' | Where-Object { $_ })
if ($parts.Count -ge 5 -and $parts[0] -eq 'TCP' -and $parts[3] -eq 'LISTENING') {
$localPort = $parts[1].Substring($parts[1].LastIndexOf(':') + 1)
if ($localPort -eq [string]$port) { [int]$parts[4] }
}
} |
Select-Object -Unique
foreach ($processId in $processIds) {
$process = Get-Process -Id $processId -ErrorAction SilentlyContinue
$allowedNames = @('node', 'dotnet', 'GalGame.AuthService', 'GalGame.FileService', 'GalGame.UserService', 'GalGame.GalGameService', 'KnowledgeService.API', 'PracticeService.API', 'CreditService.API')
if ($process -and $allowedNames -contains $process.ProcessName -and (Test-IsMoonStoneProcess -ProcessId $processId -ProcessName $process.ProcessName)) {
Stop-Process -Id $processId -Force -ErrorAction SilentlyContinue
} elseif ($process -and $allowedNames -contains $process.ProcessName) {
Write-Host (" [KEEP] Port {0} belongs to unrelated process {1} (PID {2})." -f $port, $process.ProcessName, $processId) -ForegroundColor DarkYellow
}
}
}
}
function Test-TcpPort {
param(
[Parameter(Mandatory)][string]$HostName,
[Parameter(Mandatory)][int]$Port
)
try {
$client = [System.Net.Sockets.TcpClient]::new()
$connectTask = $client.ConnectAsync($HostName, $Port)
if (-not $connectTask.Wait(1000)) {
$client.Dispose()
return $false
}
$client.Dispose()
return $true
} catch {
return $false
}
}
function Start-Neo4jDependency {
if ($isMockMode) { return }
if (Test-TcpPort -HostName '127.0.0.1' -Port 5255) {
Write-Host ' [OK] neo4j: bolt://127.0.0.1:5255' -ForegroundColor Green
return
}
throw 'Neo4j is not listening on bolt://127.0.0.1:5255. Start Neo4j with the port specified in contract.md, then rerun .\start.ps1.'
}
function Start-LocalService {
param(
[Parameter(Mandatory)][string]$Name,
[Parameter(Mandatory)][string]$FilePath,
[Parameter(Mandatory)][string]$Arguments,
[Parameter(Mandatory)][string]$HealthUrl,
[string]$WorkingDirectory = $projectRoot
)
$standardOutput = Join-Path $runtimeDirectory "$Name.out.log"
$standardError = Join-Path $runtimeDirectory "$Name.error.log"
Remove-Item -LiteralPath $standardOutput, $standardError -Force -ErrorAction SilentlyContinue
$process = Start-Process -FilePath $FilePath `
-ArgumentList $Arguments `
-WorkingDirectory $WorkingDirectory `
-WindowStyle Hidden `
-RedirectStandardOutput $standardOutput `
-RedirectStandardError $standardError `
-PassThru
[PSCustomObject]@{
Name = $Name
Process = $process
HealthUrl = $HealthUrl
StandardOutput = $standardOutput
StandardError = $standardError
Command = "$FilePath $Arguments"
}
}
function Test-ServiceHealth {
param([Parameter(Mandatory)]$Service)
$deadline = (Get-Date).AddSeconds(45)
do {
try {
$request = [System.Net.HttpWebRequest]::Create($Service.HealthUrl)
$request.Method = 'GET'
$request.Timeout = 2000
$request.Proxy = $null
$response = $request.GetResponse()
$statusCode = [int]$response.StatusCode
$response.Dispose()
if ($statusCode -ge 200 -and $statusCode -lt 400) { return $true }
} catch {
}
Start-Sleep -Seconds 1
} while ((Get-Date) -lt $deadline)
return $false
}
function Write-BugReport {
param([Parameter(Mandatory)]$Service)
$timestamp = Get-Date -Format 'yyyyMMdd-HHmmss'
$reportPath = Join-Path $bugReportDirectory "$timestamp-$($Service.Name).bugreport.log"
$stderr = if (Test-Path $Service.StandardError) { Get-Content -Raw $Service.StandardError } else { '' }
$stdout = if (Test-Path $Service.StandardOutput) { Get-Content -Raw $Service.StandardOutput } else { '' }
@"
MoonStone startup bug report
Generated at: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss K')
Service: $($Service.Name)
Health endpoint: $($Service.HealthUrl)
Command: $($Service.Command)
--- standard error ---
$stderr
--- standard output ---
$stdout
"@ | Set-Content -LiteralPath $reportPath -Encoding utf8
return $reportPath
}
function Initialize-NodeServiceDependencies {
param(
[Parameter(Mandatory)][string]$Name,
[Parameter(Mandatory)][string]$WorkingDirectory,
[Parameter(Mandatory)][string]$RequiredCommand
)
$localCommand = Join-Path $WorkingDirectory "node_modules\.bin\$RequiredCommand.cmd"
if (Test-Path -LiteralPath $localCommand -PathType Leaf) { return }
$lockFile = Join-Path $WorkingDirectory 'package-lock.json'
if (-not (Test-Path -LiteralPath $lockFile -PathType Leaf)) {
throw "$Name dependency lock file is missing: $lockFile"
}
Write-Host "Installing $Name dependencies..." -ForegroundColor DarkCyan
Push-Location $WorkingDirectory
try {
& npm.cmd ci --no-audit --no-fund | Out-Host
if ($LASTEXITCODE -ne 0) { throw "$Name dependency installation failed." }
}
finally {
Pop-Location
}
if (-not (Test-Path -LiteralPath $localCommand -PathType Leaf)) {
throw "$Name dependency installation did not provide $RequiredCommand.cmd."
}
}
function Start-MoonStoneStack {
Stop-MoonStoneServices
Remove-Item -LiteralPath $runtimeDirectory -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Force -Path $runtimeDirectory, $bugReportDirectory | Out-Null
Start-Neo4jDependency
$env:GATEWAY_PORT = [string]$gatewayPort
$env:GATEWAY_HOST = '127.0.0.1'
$env:GATEWAY_URL = $gatewayBaseUrl
$gatewayRoot = Join-Path $projectRoot 'gateway'
$renderServiceRoot = Join-Path $backendRoot 'RenderService\service'
$frontendRoot = Join-Path $projectRoot 'frontend'
Initialize-NodeServiceDependencies 'Gateway' $gatewayRoot 'tsx'
Initialize-NodeServiceDependencies 'RenderService' $renderServiceRoot 'tsc'
Initialize-NodeServiceDependencies 'Frontend' $frontendRoot 'vite'
Write-Host 'Building Wiki for the Frontend development server...' -ForegroundColor DarkCyan
Push-Location $frontendRoot
try {
& npm.cmd run build:wiki:dev | Out-Host
if ($LASTEXITCODE -ne 0) { throw 'Wiki development build failed.' }
}
finally {
Pop-Location
}
Push-Location $renderServiceRoot
try {
Write-Host 'Building RenderService...' -ForegroundColor DarkCyan
& npm.cmd run build | Out-Host
if ($LASTEXITCODE -ne 0) { throw 'RenderService build failed.' }
}
finally {
Pop-Location
}
$services = @(
Start-LocalService -Name 'gateway' -FilePath 'npm.cmd' -Arguments 'run dev' -HealthUrl "$gatewayBaseUrl/healthz" -WorkingDirectory $gatewayRoot
Start-LocalService -Name 'auth-service' -FilePath 'dotnet' -Arguments "run --project `"$backendRoot\AuthService\GalGame.AuthService.csproj`" -- --Gateway:BaseUrl $gatewayBaseUrl" -HealthUrl 'http://127.0.0.1:5102/healthz'
Start-LocalService -Name 'file-service' -FilePath 'dotnet' -Arguments "run --project `"$backendRoot\FileService\GalGame.FileService.csproj`"" -HealthUrl 'http://127.0.0.1:5103/healthz'
Start-LocalService -Name 'user-service' -FilePath 'dotnet' -Arguments "run --project `"$backendRoot\UserService\GalGame.UserService.csproj`"" -HealthUrl 'http://127.0.0.1:5101/healthz'
)
if (-not $isMockMode) {
$knowledgeArguments = "run --project `"$backendRoot\KnowledgeService\KnowledgeService.API\KnowledgeService.API.csproj`" -- --urls http://127.0.0.1:5104 --Neo4j:Uri bolt://127.0.0.1:5255 --Neo4j:Username neo4j --Neo4j:Password $neo4jPassword --Neo4j:Database neo4j --GatewayMaterialText:BaseUrl $gatewayBaseUrl --GatewayMaterialText:ServiceName KnowledgeService --GatewayMaterialText:ServiceKey moonstone-local-gateway-key --Gateway:ServiceKey moonstone-local-gateway-key"
$services += Start-LocalService -Name 'knowledge-service' -FilePath 'dotnet' -Arguments $knowledgeArguments -HealthUrl 'http://127.0.0.1:5104/readyz'
}
$galGameArguments = "run --project `"$backendRoot\GalGameService\GalGame.GalGameService.csproj`" -- --urls http://127.0.0.1:5105 --Gateway:BaseUrl $gatewayBaseUrl --Gateway:ServiceKey moonstone-local-gateway-key"
if ($useGalGameMock) {
$galGameArguments += ' --MOONSTONE_MODE Mock --GalGameStore:Provider Memory'
if ($GalGameMock) { $galGameArguments += ' --GalGameMock:UseFixedStory true' }
}
$services += Start-LocalService -Name 'galgame-service' -FilePath 'dotnet' -Arguments $galGameArguments -HealthUrl 'http://127.0.0.1:5105/healthz'
$env:PORT = '5106'
$env:RENDER_HOST = '127.0.0.1'
$env:Gateway__BaseUrl = $gatewayBaseUrl
$env:Gateway__ServiceName = 'RenderService'
$env:Gateway__ServiceKey = 'moonstone-local-gateway-key'
$services += Start-LocalService -Name 'render-service' -FilePath 'npm.cmd' -Arguments 'run start' -HealthUrl 'http://127.0.0.1:5106/healthz' -WorkingDirectory $renderServiceRoot
$modelArguments = "run --project `"$backendRoot\ModelService\ModelService.API\ModelService.API.csproj`" -- --urls http://127.0.0.1:5109 --Gateway:ServiceKey moonstone-local-gateway-key"
$services += Start-LocalService -Name 'model-service' -FilePath 'dotnet' -Arguments $modelArguments -HealthUrl 'http://127.0.0.1:5109/readyz'
$practiceArguments = "run --project `"$backendRoot\PracticeService\PracticeService.API\PracticeService.API.csproj`" -- --urls http://127.0.0.1:5107 --Gateway:BaseUrl $gatewayBaseUrl --Gateway:ServiceName PracticeService --Gateway:ServiceKey moonstone-local-gateway-key"
if ($isMockMode) { $practiceArguments += ' --PracticeStore:Provider Memory' }
$services += Start-LocalService -Name 'practice-service' -FilePath 'dotnet' -Arguments $practiceArguments -HealthUrl 'http://127.0.0.1:5107/healthz'
$creditArguments = "run --project `"$backendRoot\CreditService\CreditService.API\CreditService.API.csproj`" -- --urls http://127.0.0.1:5108 --CreditStore:Provider Memory --Gateway:ServiceKey moonstone-local-gateway-key"
$services += Start-LocalService -Name 'credit-service' -FilePath 'dotnet' -Arguments $creditArguments -HealthUrl 'http://127.0.0.1:5108/healthz'
$services += Start-LocalService -Name 'new-frontend' -FilePath 'npm.cmd' -Arguments 'run dev' -HealthUrl 'http://127.0.0.1:5121/' -WorkingDirectory $frontendRoot
$processManifest = @(
$services | ForEach-Object {
$processName = $null
$startedAtUtc = $null
try {
$processName = $_.Process.ProcessName
$startedAtUtc = $_.Process.StartTime.ToUniversalTime().ToString('O')
}
catch {
# A process that exits immediately is reported by the health
# check below; keep writing the remaining process manifest.
}
[pscustomobject]@{
Name = $_.Name
ProcessId = $_.Process.Id
ProcessName = $processName
StartedAtUtc = $startedAtUtc
}
}
)
$processManifest |
ConvertTo-Json -Depth 3 |
Set-Content -LiteralPath (Join-Path $runtimeDirectory 'project-processes.json') -Encoding UTF8
$failed = @()
foreach ($service in $services) {
if (Test-ServiceHealth $service) {
Write-Host " [OK] $($service.Name): $($service.HealthUrl)" -ForegroundColor Green
} else {
$report = Write-BugReport $service
$failed += "$($service.Name) -> $report"
Write-Host " [FAILED] $($service.Name). Bug report: $report" -ForegroundColor Red
}
}
return $failed
}
try {
Normalize-ProcessPath
do {
Write-Host 'QZWL services are starting and being checked...' -ForegroundColor Yellow
$failed = Start-MoonStoneStack
if ($failed.Count -gt 0) {
throw "Startup failed. Review: $($failed -join '; ')"
}
Write-Host ''
Write-Host 'QZWL services are ready:' -ForegroundColor Green
Write-Host " Gateway: http://localhost:$gatewayPort"
Write-Host ' UserService: http://localhost:5101'
Write-Host ' AuthService: http://localhost:5102'
Write-Host ' FileService: http://localhost:5103'
Write-Host ' KnowledgeService: http://localhost:5104'
Write-Host ' GalGameService: http://localhost:5105'
Write-Host ' RenderService: http://localhost:5106'
Write-Host ' PracticeService: http://localhost:5107'
Write-Host ' CreditService: http://localhost:5108'
Write-Host ' ModelService: http://localhost:5109'
Write-Host ' Frontend: http://localhost:5121'
Write-Host ''
Write-Host 'Press Ctrl+R to restart, or Enter to stop services.'
if ($Verify) {
$restart = $false
Stop-MoonStoneServices
break
}
$key = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
$restart = $key.VirtualKeyCode -eq 82 -and $key.ControlKeyState.ToString() -match 'Ctrl'
Stop-MoonStoneServices
} while ($restart)
}
catch {
Write-Host $_.Exception.Message -ForegroundColor Red
if ($Verify) { exit 1 }
Read-Host 'Press Enter to close this launcher window'
}
finally {
Stop-MoonStoneServices
Remove-Item -LiteralPath $runtimeDirectory -Recurse -Force -ErrorAction SilentlyContinue
}