diff --git a/ka-note/VERSION b/ka-note/VERSION index 91eac0c..57cd83c 100644 --- a/ka-note/VERSION +++ b/ka-note/VERSION @@ -1 +1 @@ -1.2.4 \ No newline at end of file +1.2.5 \ No newline at end of file diff --git a/ka-note/deploy.ps1 b/ka-note/deploy.ps1 index 4cd6995..92b96a6 100644 --- a/ka-note/deploy.ps1 +++ b/ka-note/deploy.ps1 @@ -129,70 +129,80 @@ Write-Host "=== Restart App Service ===" -ForegroundColor Cyan az webapp restart --name $APP --resource-group $RG Write-Host "=== Post-Deploy DB Validation ===" -ForegroundColor Cyan -Write-Host " Waiting for server to boot (~20s)..." -ForegroundColor DarkGray -$bootDeadline = (Get-Date).AddSeconds(45) -$serverReady = $false -while ((Get-Date) -lt $bootDeadline) { - Start-Sleep -Seconds 3 - try { - $h = Invoke-RestMethod -Uri "$AppUrl/api/health" -Method GET -UseBasicParsing -TimeoutSec 5 - if ($h.status -eq 'ok') { $serverReady = $true; break } - } catch { } -} -if (-not $serverReady) { - Write-Host " WARNING: Server did not respond within 45s" -ForegroundColor Red -} $validationOk = $false -if ($serverReady -and $env:KA_NOTE_DEPLOY_API_KEY) { - $authHeader = @{ Authorization = "Bearer $env:KA_NOTE_DEPLOY_API_KEY" } - $maxRetries = 6 - for ($attempt = 1; $attempt -le $maxRetries; $attempt++) { - try { - $stats = Invoke-RestMethod -Uri "$AppUrl/api/admin/stats" -Method GET ` - -Headers $authHeader -UseBasicParsing -TimeoutSec 15 - $contextCount = $stats.contextCount - $topicCount = $stats.topicCount - Write-Host " [$attempt/$maxRetries] Contexts: $contextCount, Topics: $topicCount" -ForegroundColor DarkGray - if ($contextCount -gt 0 -and $topicCount -gt 0) { - Write-Host " DB validation passed." -ForegroundColor Green - $validationOk = $true - break - } - if ($attempt -lt $maxRetries) { Start-Sleep -Seconds 10 } - } catch { - Write-Host " [$attempt/$maxRetries] Request error: $_" -ForegroundColor Yellow - if ($attempt -lt $maxRetries) { Start-Sleep -Seconds 10 } - } - } - if (-not $validationOk) { - Write-Host " DB validation FAILED after $maxRetries attempts" -ForegroundColor Red - } -} elseif (-not $env:KA_NOTE_DEPLOY_API_KEY) { - Write-Host " KA_NOTE_DEPLOY_API_KEY not set, skipping DB validation" -ForegroundColor Yellow - $validationOk = $true -} +$keepValidating = $true -if (-not $validationOk) { - $latestBackup = Get-ChildItem -Path $BackupDir -Filter "*.db" | - Sort-Object LastWriteTime -Descending | Select-Object -First 1 - Write-Host "" - Write-Host " !! DB appears empty or unreachable after deploy !!" -ForegroundColor Red - Write-Host " Pre-deploy backup: $($latestBackup.FullName)" -ForegroundColor Yellow - Write-Host "" - $answer = Read-Host " Restore backup to prod? (yes/no)" - if ($answer -eq 'yes') { - Write-Host " Uploading backup to prod..." -ForegroundColor Cyan - Invoke-WebRequest -Uri $KuduDbUrl ` - -Method PUT ` - -Headers @{ Authorization = "Bearer $KuduToken" } ` - -InFile $latestBackup.FullName -UseBasicParsing | Out-Null - Write-Host " Backup uploaded. Restarting app..." -ForegroundColor Cyan - az webapp restart --name $APP --resource-group $RG - Write-Host " Restore complete. Verify manually: $AppUrl" -ForegroundColor Green - } else { - Write-Host " Restore skipped. Manual intervention required!" -ForegroundColor Red - exit 1 +while ($keepValidating) { + $keepValidating = $false + + Write-Host " Waiting for server to boot (~20s)..." -ForegroundColor DarkGray + $bootDeadline = (Get-Date).AddSeconds(45) + $serverReady = $false + while ((Get-Date) -lt $bootDeadline) { + Start-Sleep -Seconds 3 + try { + $h = Invoke-RestMethod -Uri "$AppUrl/api/health" -Method GET -UseBasicParsing -TimeoutSec 5 + if ($h.status -eq 'ok') { $serverReady = $true; break } + } catch { } + } + if (-not $serverReady) { + Write-Host " WARNING: Server did not respond within 45s" -ForegroundColor Red + } + + if ($serverReady -and $env:KA_NOTE_DEPLOY_API_KEY) { + $authHeader = @{ Authorization = "Bearer $env:KA_NOTE_DEPLOY_API_KEY" } + $maxRetries = 6 + for ($attempt = 1; $attempt -le $maxRetries; $attempt++) { + try { + $stats = Invoke-RestMethod -Uri "$AppUrl/api/admin/stats" -Method GET ` + -Headers $authHeader -UseBasicParsing -TimeoutSec 15 + $contextCount = $stats.contextCount + $topicCount = $stats.topicCount + Write-Host " [$attempt/$maxRetries] Contexts: $contextCount, Topics: $topicCount" -ForegroundColor DarkGray + if ($contextCount -gt 0 -and $topicCount -gt 0) { + Write-Host " DB validation passed." -ForegroundColor Green + $validationOk = $true + break + } + if ($attempt -lt $maxRetries) { Start-Sleep -Seconds 10 } + } catch { + Write-Host " [$attempt/$maxRetries] Request error: $_" -ForegroundColor Yellow + if ($attempt -lt $maxRetries) { Start-Sleep -Seconds 10 } + } + } + if (-not $validationOk) { + Write-Host " DB validation FAILED after $maxRetries attempts" -ForegroundColor Red + } + } elseif (-not $env:KA_NOTE_DEPLOY_API_KEY) { + Write-Host " KA_NOTE_DEPLOY_API_KEY not set, skipping DB validation" -ForegroundColor Yellow + $validationOk = $true + } + + if (-not $validationOk) { + $latestBackup = Get-ChildItem -Path $BackupDir -Filter "*.db" | + Sort-Object LastWriteTime -Descending | Select-Object -First 1 + Write-Host "" + Write-Host " !! DB appears empty or unreachable after deploy !!" -ForegroundColor Red + Write-Host " Pre-deploy backup: $($latestBackup.FullName)" -ForegroundColor Yellow + Write-Host "" + $answer = Read-Host " Restore backup to prod? (yes/no/retry)" + if ($answer -eq 'retry') { + Write-Host " Retrying validation..." -ForegroundColor Cyan + $keepValidating = $true + } elseif ($answer -eq 'yes') { + Write-Host " Uploading backup to prod..." -ForegroundColor Cyan + Invoke-WebRequest -Uri $KuduDbUrl ` + -Method PUT ` + -Headers @{ Authorization = "Bearer $KuduToken" } ` + -InFile $latestBackup.FullName -UseBasicParsing | Out-Null + Write-Host " Backup uploaded. Restarting app..." -ForegroundColor Cyan + az webapp restart --name $APP --resource-group $RG + Write-Host " Restore complete. Verify manually: $AppUrl" -ForegroundColor Green + } else { + Write-Host " Restore skipped. Manual intervention required!" -ForegroundColor Red + exit 1 + } } }