Files
trading_bot_v3/lib/aggressive-cleanup.ts
mindesbunister 71e1a64b5d fix: correct entry prices and position sizing in trading system
- Fixed automation service to use real SOL price (~89) instead of hardcoded 00
- Updated position size calculation to properly convert USD investment to token amount
- Enhanced trade display to show separate entry/exit prices with price difference
- Added data quality warnings for trades with missing exit data
- Updated API to use current SOL price (189.50) and improved trade result determination
- Added detection and warnings for old trades with incorrect price data

Resolves issue where trades showed 9-100 entry prices instead of real SOL price of 89
and position sizes of 2.04 SOL instead of correct ~0.53 SOL for 00 investment
2025-07-21 09:26:48 +02:00

198 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Aggressive process cleanup utility
import { exec } from 'child_process'
import { promisify } from 'util'
const execAsync = promisify(exec)
class AggressiveCleanup {
private static instance: AggressiveCleanup
private cleanupInterval: NodeJS.Timeout | null = null
private isRunning = false
private isInitialized = false
private constructor() {
// Don't auto-start - let startup.ts control it
}
static getInstance(): AggressiveCleanup {
if (!AggressiveCleanup.instance) {
AggressiveCleanup.instance = new AggressiveCleanup()
}
return AggressiveCleanup.instance
}
startPeriodicCleanup() {
if (this.isInitialized) {
console.log('🔄 Aggressive cleanup already initialized')
return
}
this.isInitialized = true
console.log('🚀 Starting aggressive cleanup system')
// In development, use on-demand cleanup instead of periodic
if (process.env.NODE_ENV === 'development') {
console.log('🔧 Development mode: Using on-demand cleanup (triggered after analysis)')
console.log('✅ On-demand cleanup system ready')
return
}
// 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)
}
}, 10 * 60 * 1000) // 10 minutes
// Also run initial cleanup after 60 seconds
setTimeout(() => {
this.cleanupOrphanedProcesses().catch(console.error)
}, 60000)
console.log('✅ Periodic cleanup system started (10 min intervals)')
}
async cleanupOrphanedProcesses(): Promise<void> {
if (this.isRunning) return
this.isRunning = true
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
try {
const { progressTracker } = await import('./progress-tracker')
const activeSessions = progressTracker.getActiveSessions()
if (activeSessions.length > 0) {
console.log(`⚠️ Skipping cleanup - ${activeSessions.length} active analysis sessions: ${activeSessions.join(', ')}`)
return
}
console.log('✅ No active analysis sessions, proceeding with cleanup')
} catch (importError) {
console.error('❌ Error importing progress tracker:', importError)
console.log('⚠️ Skipping cleanup due to import error')
return
}
// Find and kill orphaned chromium processes
const chromiumProcesses = await this.findChromiumProcesses()
if (chromiumProcesses.length > 0) {
console.log(`Found ${chromiumProcesses.length} chromium processes, cleaning up...`)
for (const pid of chromiumProcesses) {
try {
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
try {
await execAsync('rm -rf /tmp/puppeteer_dev_chrome_profile-* 2>/dev/null || true')
console.log('✅ Cleaned up temp directories')
} catch (error) {
// Ignore errors
}
// Clean up shared memory
try {
await execAsync('rm -rf /dev/shm/.org.chromium.* 2>/dev/null || true')
console.log('✅ Cleaned up shared memory')
} catch (error) {
// Ignore errors
}
} catch (error) {
console.error(`Error in ${cleanupType} cleanup:`, error)
} finally {
this.isRunning = false
}
}
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: string) => pid && pid !== '')
} catch (error) {
return []
}
}
async forceCleanup(): Promise<void> {
console.log('🚨 Force cleanup initiated...')
// Stop periodic cleanup
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval)
}
// Run aggressive cleanup
await this.cleanupOrphanedProcesses()
// Kill all chromium processes
try {
await execAsync('pkill -9 -f "chromium" 2>/dev/null || true')
await execAsync('pkill -9 -f "chrome" 2>/dev/null || true')
console.log('✅ Force killed all browser processes')
} catch (error) {
console.error('Error in force cleanup:', error)
}
}
// 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)
this.cleanupInterval = null
}
}
}
// Initialize the aggressive cleanup
const aggressiveCleanup = AggressiveCleanup.getInstance()
export default aggressiveCleanup