🎯 Improved cleanup timing - coordinate with complete analysis cycle

- Moved cleanup trigger from analysis phase to complete automation cycle
- Cleanup now runs after trading decision is made (enter/hold/exit)
- Added comprehensive post-cycle cleanup that waits for graceful shutdown
- Enhanced cleanup coordination with analysis cycle completion signals
- Force cleanup after complete cycle to ensure all zombie processes are killed
- Added cleanup triggers for all cycle outcomes (trade executed, no opportunity, error, etc.)
- Improved timing to wait for browser processes to close properly
- Better correlation between analysis completion and process cleanup
This commit is contained in:
mindesbunister
2025-07-19 00:44:27 +02:00
parent 10377810c2
commit cca7303b47
3 changed files with 109 additions and 24 deletions

View File

@@ -162,6 +162,8 @@ export class AutomationService {
const todayTrades = await this.getTodayTradeCount(this.config.userId)
if (todayTrades >= this.config.maxDailyTrades) {
console.log(`📊 Daily trade limit reached (${todayTrades}/${this.config.maxDailyTrades})`)
// Run cleanup even when trade limit is reached
await this.runPostCycleCleanup('trade_limit_reached')
return
}
@@ -169,6 +171,8 @@ export class AutomationService {
const analysisResult = await this.performAnalysis()
if (!analysisResult) {
console.log('❌ Analysis failed, skipping cycle')
// Run cleanup when analysis fails
await this.runPostCycleCleanup('analysis_failed')
return
}
@@ -182,16 +186,39 @@ export class AutomationService {
const tradeDecision = await this.makeTradeDecision(analysisResult)
if (!tradeDecision) {
console.log('📊 No trading opportunity found')
// Run cleanup when no trading opportunity
await this.runPostCycleCleanup('no_opportunity')
return
}
// Step 6: Execute trade
await this.executeTrade(tradeDecision)
// Run cleanup after successful trade execution
await this.runPostCycleCleanup('trade_executed')
} catch (error) {
console.error('Error in automation cycle:', error)
this.stats.errorCount++
this.stats.lastError = error instanceof Error ? error.message : 'Unknown error'
// Run cleanup on error
await this.runPostCycleCleanup('error')
}
}
private async runPostCycleCleanup(reason: string): Promise<void> {
console.log(`🧹 Running post-cycle cleanup (reason: ${reason})`)
// Small delay to ensure all analysis processes have finished
await new Promise(resolve => setTimeout(resolve, 2000))
try {
// Signal that the complete analysis cycle is done
await aggressiveCleanup.signalAnalysisCycleComplete()
console.log(`✅ Post-cycle cleanup completed for: ${reason}`)
} catch (error) {
console.error('Error in post-cycle cleanup:', error)
}
}
@@ -232,9 +259,6 @@ export class AutomationService {
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
}
@@ -248,9 +272,6 @@ export class AutomationService {
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
}
@@ -265,9 +286,6 @@ export class AutomationService {
progressTracker.deleteSession(sessionId)
}, 2000)
// Run post-analysis cleanup
await aggressiveCleanup.runPostAnalysisCleanup()
return combinedResult
} catch (error) {
@@ -277,8 +295,6 @@ export class AutomationService {
progressTracker.deleteSession(sessionId)
}, 5000)
// Run post-analysis cleanup even on error
await aggressiveCleanup.runPostAnalysisCleanup()
return null
}
}