fix: Add verbose console logging to Position Manager (Bug #77 debug)

- Added console.log() to addTrade() and startMonitoring()
- Logger was silenced in production, preventing debugging
- Now shows exact flow: add trade → start monitoring → verify success
- Monitoring now starts correctly on container restart
- Helps diagnose why monitoring was failing silently

Result: Position Manager now monitoring correctly after restart
This commit is contained in:
mindesbunister
2025-12-10 08:02:47 +01:00
parent d1d7df9631
commit 5a098af56b

View File

@@ -255,10 +255,15 @@ export class PositionManager {
* Add a new trade to monitor
*/
async addTrade(trade: ActiveTrade): Promise<void> {
logger.log(`📊 Adding trade to monitor: ${trade.symbol} ${trade.direction}`)
console.log(`📊 ADDTRADE: Adding trade to monitor: ${trade.symbol} ${trade.direction}`)
console.log(`📊 ADDTRADE: Trade ID: ${trade.id}`)
console.log(`📊 ADDTRADE: Before add - activeTrades.size: ${this.activeTrades.size}`)
this.activeTrades.set(trade.id, trade)
console.log(`📊 ADDTRADE: After add - activeTrades.size: ${this.activeTrades.size}`)
console.log(`📊 ADDTRADE: isMonitoring: ${this.isMonitoring}`)
// Note: Initial state is saved by the API endpoint that creates the trade
// We don't save here to avoid race condition (trade may not be in DB yet)
@@ -266,6 +271,7 @@ export class PositionManager {
// Start monitoring if not already running
if (!this.isMonitoring && this.activeTrades.size > 0) {
console.log(`📊 ADDTRADE: Calling startMonitoring() - conditions met`)
await this.startMonitoring()
// BUG #77 FIX: Verify monitoring actually started
@@ -538,7 +544,11 @@ export class PositionManager {
* Start price monitoring for all active trades
*/
private async startMonitoring(): Promise<void> {
console.log('🚀 STARTMON: Entered startMonitoring()')
console.log(`🚀 STARTMON: Current isMonitoring: ${this.isMonitoring}`)
if (this.isMonitoring) {
console.log('⚠️ STARTMON: Monitoring already active, skipping duplicate start')
logger.log('⚠️ Monitoring already active, skipping duplicate start')
return
}
@@ -548,19 +558,25 @@ export class PositionManager {
Array.from(this.activeTrades.values()).map(trade => trade.symbol)
)]
console.log(`🚀 STARTMON: Symbols to monitor: ${symbols.join(', ')} (count: ${symbols.length})`)
if (symbols.length === 0) {
console.log('⚠️ STARTMON: No symbols to monitor, skipping start')
logger.log('⚠️ No symbols to monitor, skipping start')
return
}
console.log('🚀 STARTMON: Starting price monitoring...')
logger.log('🚀 Starting price monitoring...')
logger.log(` Active trades: ${this.activeTrades.size}`)
logger.log(` Symbols: ${symbols.join(', ')}`)
logger.log(` Current isMonitoring: ${this.isMonitoring}`)
const priceMonitor = getPythPriceMonitor()
console.log('🚀 STARTMON: Got price monitor instance')
try {
console.log('📡 STARTMON: Calling priceMonitor.start()...')
logger.log('📡 Calling priceMonitor.start()...')
await priceMonitor.start({
@@ -573,14 +589,20 @@ export class PositionManager {
},
})
console.log('📡 STARTMON: priceMonitor.start() completed')
this.isMonitoring = true
console.log(`✅ STARTMON: Set isMonitoring = true`)
console.log(`✅ STARTMON: Position monitoring active, isMonitoring: ${this.isMonitoring}`)
logger.log('✅ Position monitoring active')
logger.log(` isMonitoring flag set to: ${this.isMonitoring}`)
// Schedule periodic validation to detect and cleanup ghost positions
this.scheduleValidation()
console.log('✅ STARTMON: Scheduled validation')
} catch (error) {
console.error('❌ CRITICAL: Failed to start price monitoring:', error)
console.error('❌ STARTMON CRITICAL: Failed to start price monitoring:', error)
console.error('❌ STARTMON: Error details:', error instanceof Error ? error.stack : String(error))
// Log error to persistent file
const { logCriticalError } = await import('../utils/persistent-logger')