ObsiViewer/apply-phase3-patch.ps1

153 lines
5.1 KiB
PowerShell

# Phase 3 Patch Application Script
# This script applies the final Phase 3 modifications to server/index.mjs
param(
[switch]$Backup = $true,
[switch]$Verify = $true
)
$ErrorActionPreference = "Stop"
$indexFile = "c:\dev\git\web\ObsiViewer\server\index.mjs"
$backupFile = "$indexFile.backup.$(Get-Date -Format 'yyyyMMdd-HHmmss')"
Write-Host "🚀 Phase 3 Patch Application" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""
# Step 1: Create backup
if ($Backup) {
Write-Host "📦 Creating backup..." -ForegroundColor Yellow
Copy-Item $indexFile $backupFile
Write-Host "✅ Backup created: $backupFile" -ForegroundColor Green
Write-Host ""
}
# Step 2: Read the file
Write-Host "📖 Reading index.mjs..." -ForegroundColor Yellow
$content = Get-Content $indexFile -Raw
# Step 3: Add setup endpoint call before app.listen()
Write-Host "🔧 Adding performance endpoint setup..." -ForegroundColor Yellow
$performanceEndpointSetup = @"
// Phase 3: Setup performance monitoring endpoint
setupPerformanceEndpoint(app, performanceMonitor, metadataCache, meilisearchCircuitBreaker);
"@
# Find the line with "// Créer le répertoire de la voûte" and insert before it
$content = $content -replace `
"// Créer le répertoire de la voûte s'il n'existe pas", `
"$performanceEndpointSetup`n`n// Créer le répertoire de la voûte s'il n'existe pas"
Write-Host "✅ Performance endpoint setup added" -ForegroundColor Green
# Step 4: Update app.listen() to include deferred indexing
Write-Host "🔧 Updating app.listen() for deferred indexing..." -ForegroundColor Yellow
$oldListen = @"
app.listen(PORT, '0.0.0.0', () => {
console.log(`ObsiViewer server running on http://0.0.0.0:${PORT}`);
console.log(`Vault directory: ${vaultDir}`);
});
"@
$newListen = @"
// Phase 3: Deferred Meilisearch indexing (non-blocking)
let indexingState = { inProgress: false, completed: false };
const scheduleIndexing = async () => {
if (indexingState.inProgress) return;
indexingState.inProgress = true;
setImmediate(async () => {
try {
console.time('[Meilisearch] Background indexing');
await fullReindex(vaultDir);
console.timeEnd('[Meilisearch] Background indexing');
indexingState.completed = true;
console.log('[Meilisearch] Background indexing completed');
} catch (error) {
console.error('[Meilisearch] Background indexing failed:', error.message);
indexingState.completed = false;
// Retry after 5 minutes
setTimeout(() => {
indexingState.inProgress = false;
scheduleIndexing();
}, 5 * 60 * 1000);
}
});
};
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 ObsiViewer server running on http://0.0.0.0:${PORT}`);
console.log(`📁 Vault directory: ${vaultDir}`);
console.log(`📊 Performance monitoring: http://0.0.0.0:${PORT}/__perf`);
// Schedule background indexing (non-blocking)
scheduleIndexing();
console.log(' Server ready - Meilisearch indexing in background');
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n🛑 Shutting down server...');
server.close(() => {
console.log(' Server shutdown complete');
process.exit(0);
});
});
"@
$content = $content -replace [regex]::Escape($oldListen), $newListen
Write-Host "✅ app.listen() updated with deferred indexing" -ForegroundColor Green
# Step 5: Write the modified content
Write-Host "💾 Writing modified index.mjs..." -ForegroundColor Yellow
Set-Content $indexFile $content -Encoding UTF8
Write-Host "✅ File written successfully" -ForegroundColor Green
# Step 6: Verify changes
if ($Verify) {
Write-Host ""
Write-Host "🔍 Verifying changes..." -ForegroundColor Yellow
$verifyContent = Get-Content $indexFile -Raw
$checks = @(
@{ Name = "Performance endpoint setup"; Pattern = "setupPerformanceEndpoint" },
@{ Name = "Deferred indexing"; Pattern = "scheduleIndexing" },
@{ Name = "Graceful shutdown"; Pattern = "process.on\('SIGINT'" },
@{ Name = "Performance monitoring URL"; Pattern = "__perf" }
)
$allPassed = $true
foreach ($check in $checks) {
if ($verifyContent -match $check.Pattern) {
Write-Host "$($check.Name)" -ForegroundColor Green
} else {
Write-Host "$($check.Name)" -ForegroundColor Red
$allPassed = $false
}
}
if ($allPassed) {
Write-Host ""
Write-Host "✅ All verification checks passed!" -ForegroundColor Green
} else {
Write-Host ""
Write-Host "⚠️ Some verification checks failed" -ForegroundColor Yellow
}
}
Write-Host ""
Write-Host "================================" -ForegroundColor Cyan
Write-Host "✅ Phase 3 patch applied successfully!" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host "1. Test the server: npm run start"
Write-Host "2. Check performance: curl http://localhost:3000/__perf"
Write-Host "3. Monitor cache hits in the logs"
Write-Host "4. Verify Meilisearch indexing in background"
Write-Host ""