feat: implement on-demand cleanup triggered after analysis completion

- Replace time-based cleanup with on-demand cleanup in development mode
- Cleanup is triggered immediately after AI analysis completes
- Added runPostAnalysisCleanup() method to aggressive-cleanup service
- Cleanup triggers added to both single and batch analysis endpoints
- More efficient: cleanup happens only when needed, not on timer
- Prevents zombie processes without interfering with active analysis
- Production mode still uses periodic cleanup as backup (10 min intervals)
- Gentle cleanup in development: SIGTERM first, then SIGKILL if needed
This commit is contained in:
mindesbunister
2025-07-18 19:11:15 +02:00
parent 2bdf9e2b41
commit 74b0087f17
4 changed files with 84 additions and 12 deletions

View File

@@ -30,34 +30,38 @@ class AggressiveCleanup {
this.isInitialized = true
console.log('🚀 Starting aggressive cleanup system')
// In development, disable aggressive cleanup to avoid interfering with analysis
// In development, use on-demand cleanup instead of periodic
if (process.env.NODE_ENV === 'development') {
console.log('🔒 Aggressive cleanup disabled in development mode')
console.log('🔧 Development mode: Using on-demand cleanup (triggered after analysis)')
console.log('✅ On-demand cleanup system ready')
return
}
// Clean up every 5 minutes
// Production: Clean up every 10 minutes (longer intervals)
this.cleanupInterval = setInterval(async () => {
try {
await this.cleanupOrphanedProcesses()
} catch (error) {
console.error('Error in periodic cleanup:', error)
}
}, 5 * 60 * 1000) // 5 minutes
}, 10 * 60 * 1000) // 10 minutes
// Also run initial cleanup after 30 seconds
// Also run initial cleanup after 60 seconds
setTimeout(() => {
this.cleanupOrphanedProcesses().catch(console.error)
}, 30000)
}, 60000)
console.log('✅ Aggressive cleanup system started (5 min intervals)')
console.log('✅ Periodic cleanup system started (10 min intervals)')
}
async cleanupOrphanedProcesses(): Promise<void> {
if (this.isRunning) return
this.isRunning = true
console.log('🧹 Running aggressive cleanup for orphaned processes...')
const isDevelopment = process.env.NODE_ENV === 'development'
const cleanupType = isDevelopment ? 'gentle' : 'aggressive'
console.log(`🧹 Running ${cleanupType} cleanup for orphaned processes...`)
try {
// Check for active analysis sessions
@@ -85,12 +89,35 @@ class AggressiveCleanup {
for (const pid of chromiumProcesses) {
try {
await execAsync(`kill -9 ${pid}`)
console.log(`✅ Killed process ${pid}`)
if (isDevelopment) {
// In development, use gentler SIGTERM first
console.log(`🔧 Dev mode: Gentle shutdown of process ${pid}`)
await execAsync(`kill -TERM ${pid}`)
// Give process 3 seconds to shut down gracefully
await new Promise(resolve => setTimeout(resolve, 3000))
// Check if process is still running
try {
await execAsync(`kill -0 ${pid}`)
// Process still running, force kill
console.log(`⚠️ Process ${pid} didn't shut down gracefully, force killing`)
await execAsync(`kill -9 ${pid}`)
} catch {
// Process already dead, that's good
console.log(`✅ Process ${pid} shut down gracefully`)
}
} else {
// Production: immediate force kill
await execAsync(`kill -9 ${pid}`)
console.log(`✅ Killed process ${pid}`)
}
} catch (error) {
// Process might already be dead
console.log(` Process ${pid} may already be terminated`)
}
}
} else {
console.log('✅ No orphaned chromium processes found')
}
// Clean up temp directories
@@ -110,7 +137,7 @@ class AggressiveCleanup {
}
} catch (error) {
console.error('Error in aggressive cleanup:', error)
console.error(`Error in ${cleanupType} cleanup:`, error)
} finally {
this.isRunning = false
}
@@ -119,7 +146,7 @@ class AggressiveCleanup {
private async findChromiumProcesses(): Promise<string[]> {
try {
const { stdout } = await execAsync('ps aux | grep -E "(chromium|chrome)" | grep -v grep | awk \'{print $2}\'')
return stdout.trim().split('\n').filter(pid => pid && pid !== '')
return stdout.trim().split('\n').filter((pid: string) => pid && pid !== '')
} catch (error) {
return []
}
@@ -146,6 +173,16 @@ class AggressiveCleanup {
}
}
// New method for on-demand cleanup after analysis
async runPostAnalysisCleanup(): Promise<void> {
console.log('🧹 Post-analysis cleanup triggered...')
// Small delay to ensure analysis processes are fully closed
await new Promise(resolve => setTimeout(resolve, 2000))
await this.cleanupOrphanedProcesses()
}
stop(): void {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval)