Fix cleanup process timing and coordination with analysis sessions
CRITICAL BUG FIX: Cleanup process was interfering with active analysis sessions - Aggressive cleanup was running during active analysis, causing navigation failures - Progress tracking was not properly coordinated with cleanup system - No session state checking before process termination 1. AUTOMATION SERVICE COORDINATION: - Added proper progress tracking to automation cycles - Created unique session IDs for each analysis run - Integrated with progressTracker for session state management - Added post-analysis cleanup triggers with proper timing 2. ENHANCED CLEANUP INTELLIGENCE: - Improved session checking with detailed logging of active sessions - Added process age filtering in development mode (only kill >5min old processes) - Better error handling when progress tracker import fails - More granular cleanup control with session state awareness 3. TIMING IMPROVEMENTS: - Post-analysis cleanup now waits for session completion - Added proper delays between analysis phases - Implemented graceful cleanup deferral when sessions are active - Added delayed cleanup fallback for stuck sessions 4. DEVELOPMENT MODE SAFETY: - Gentler SIGTERM → SIGKILL progression for development - Only clean processes older than 5 minutes during dev - Better logging of process age and cleanup decisions - Safer fallback behavior when session tracking fails This resolves the 'Failed to navigate to layout' errors by ensuring cleanup doesn't interfere with active browser sessions during analysis.
This commit is contained in:
@@ -3,6 +3,8 @@ import { aiAnalysisService, AnalysisResult } from './ai-analysis'
|
||||
import { jupiterDEXService } from './jupiter-dex-service'
|
||||
import { enhancedScreenshotService } from './enhanced-screenshot-simple'
|
||||
import { TradingViewCredentials } from './tradingview-automation'
|
||||
import { progressTracker } from './progress-tracker'
|
||||
import aggressiveCleanup from './aggressive-cleanup'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
@@ -197,8 +199,22 @@ export class AutomationService {
|
||||
screenshots: string[]
|
||||
analysis: AnalysisResult | null
|
||||
} | null> {
|
||||
// Generate unique session ID for this analysis
|
||||
const sessionId = `automation-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
||||
|
||||
try {
|
||||
console.log('📸 Starting multi-timeframe analysis with dual layouts...')
|
||||
console.log(`📸 Starting multi-timeframe analysis with dual layouts... (Session: ${sessionId})`)
|
||||
|
||||
// Create progress tracking session
|
||||
const progressSteps = [
|
||||
{ id: 'init', title: 'Initialize', description: 'Starting multi-timeframe analysis', status: 'pending' as const },
|
||||
{ id: 'capture', title: 'Capture', description: 'Capturing screenshots for all timeframes', status: 'pending' as const },
|
||||
{ id: 'analysis', title: 'Analysis', description: 'Running AI analysis on screenshots', status: 'pending' as const },
|
||||
{ id: 'complete', title: 'Complete', description: 'Analysis complete', status: 'pending' as const }
|
||||
]
|
||||
|
||||
progressTracker.createSession(sessionId, progressSteps)
|
||||
progressTracker.updateStep(sessionId, 'init', 'active', 'Starting multi-timeframe analysis...')
|
||||
|
||||
// Multi-timeframe analysis: 15m, 1h, 2h, 4h
|
||||
const timeframes = ['15', '1h', '2h', '4h']
|
||||
@@ -206,48 +222,88 @@ export class AutomationService {
|
||||
|
||||
console.log(`🔍 Analyzing ${symbol} across timeframes: ${timeframes.join(', ')} with AI + DIY layouts`)
|
||||
|
||||
progressTracker.updateStep(sessionId, 'init', 'completed', `Starting analysis for ${timeframes.length} timeframes`)
|
||||
progressTracker.updateStep(sessionId, 'capture', 'active', 'Capturing screenshots...')
|
||||
|
||||
// Analyze each timeframe with both AI and DIY layouts
|
||||
const multiTimeframeResults = await this.analyzeMultiTimeframeWithDualLayouts(symbol, timeframes)
|
||||
const multiTimeframeResults = await this.analyzeMultiTimeframeWithDualLayouts(symbol, timeframes, sessionId)
|
||||
|
||||
if (multiTimeframeResults.length === 0) {
|
||||
console.log('❌ No multi-timeframe analysis results')
|
||||
progressTracker.updateStep(sessionId, 'capture', 'error', 'No analysis results captured')
|
||||
progressTracker.deleteSession(sessionId)
|
||||
|
||||
// Run post-analysis cleanup
|
||||
await aggressiveCleanup.runPostAnalysisCleanup()
|
||||
return null
|
||||
}
|
||||
|
||||
progressTracker.updateStep(sessionId, 'capture', 'completed', `Captured ${multiTimeframeResults.length} timeframe analyses`)
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'active', 'Processing multi-timeframe results...')
|
||||
|
||||
// Process and combine multi-timeframe results
|
||||
const combinedResult = this.combineMultiTimeframeAnalysis(multiTimeframeResults)
|
||||
|
||||
if (!combinedResult.analysis) {
|
||||
console.log('❌ Failed to combine multi-timeframe analysis')
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'error', 'Failed to combine analysis results')
|
||||
progressTracker.deleteSession(sessionId)
|
||||
|
||||
// Run post-analysis cleanup
|
||||
await aggressiveCleanup.runPostAnalysisCleanup()
|
||||
return null
|
||||
}
|
||||
|
||||
console.log(`✅ Multi-timeframe analysis completed: ${combinedResult.analysis.recommendation} with ${combinedResult.analysis.confidence}% confidence`)
|
||||
console.log(`📊 Timeframe alignment: ${this.analyzeTimeframeAlignment(multiTimeframeResults)}`)
|
||||
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'completed', `Analysis complete: ${combinedResult.analysis.recommendation}`)
|
||||
progressTracker.updateStep(sessionId, 'complete', 'completed', 'Multi-timeframe analysis finished')
|
||||
|
||||
// Clean up session after successful completion
|
||||
setTimeout(() => {
|
||||
progressTracker.deleteSession(sessionId)
|
||||
}, 2000)
|
||||
|
||||
// Run post-analysis cleanup
|
||||
await aggressiveCleanup.runPostAnalysisCleanup()
|
||||
|
||||
return combinedResult
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error performing multi-timeframe analysis:', error)
|
||||
progressTracker.updateStep(sessionId, 'analysis', 'error', error instanceof Error ? error.message : 'Unknown error')
|
||||
setTimeout(() => {
|
||||
progressTracker.deleteSession(sessionId)
|
||||
}, 5000)
|
||||
|
||||
// Run post-analysis cleanup even on error
|
||||
await aggressiveCleanup.runPostAnalysisCleanup()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private async analyzeMultiTimeframeWithDualLayouts(
|
||||
symbol: string,
|
||||
timeframes: string[]
|
||||
timeframes: string[],
|
||||
sessionId: string
|
||||
): Promise<Array<{ symbol: string; timeframe: string; analysis: AnalysisResult | null }>> {
|
||||
const results: Array<{ symbol: string; timeframe: string; analysis: AnalysisResult | null }> = []
|
||||
|
||||
for (const timeframe of timeframes) {
|
||||
for (let i = 0; i < timeframes.length; i++) {
|
||||
const timeframe = timeframes[i]
|
||||
try {
|
||||
console.log(`📊 Analyzing ${symbol} ${timeframe} with AI + DIY layouts...`)
|
||||
console.log(`📊 Analyzing ${symbol} ${timeframe} with AI + DIY layouts... (${i + 1}/${timeframes.length})`)
|
||||
|
||||
// Update progress for timeframe
|
||||
progressTracker.updateTimeframeProgress(sessionId, i + 1, timeframes.length, timeframe)
|
||||
|
||||
// Use the dual-layout configuration for each timeframe
|
||||
const screenshotConfig = {
|
||||
symbol: symbol,
|
||||
timeframe: timeframe,
|
||||
layouts: ['ai', 'diy']
|
||||
layouts: ['ai', 'diy'],
|
||||
sessionId: sessionId
|
||||
}
|
||||
|
||||
const result = await aiAnalysisService.captureAndAnalyzeWithConfig(screenshotConfig)
|
||||
|
||||
Reference in New Issue
Block a user