Files
trading_bot_v3/app/api/automation/stop/route.js
mindesbunister 8c80c577cb fix: Improve automation stop functionality and add debug logging
Stop API improvements:
- Added comprehensive debug logging for stop process
- Changed session status from INACTIVE to STOPPED for clarity
- Better error tracking and result reporting

Automation service improvements:
- Added isRunning check at start of runAutomationCycle to prevent zombie cycles
- Enhanced stop method with better logging and state reset
- Proper config cleanup after database update to prevent residual processes
- More robust interval clearing and state management

These changes should fix the issue where automation appears stopped
but continues running in background.
2025-07-24 17:42:50 +02:00

45 lines
1.5 KiB
JavaScript

import { NextResponse } from 'next/server'
import { automationService } from '@/lib/automation-service-simple'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export async function POST() {
try {
console.log('🛑 Stop automation request received')
// Stop the automation service
console.log('🛑 Calling automationService.stopAutomation()')
const success = await automationService.stopAutomation()
console.log('🛑 Stop automation result:', success)
// Also update all active automation sessions in database to INACTIVE
console.log('🛑 Updating database sessions to STOPPED')
const updateResult = await prisma.automationSession.updateMany({
where: {
status: 'ACTIVE'
},
data: {
status: 'STOPPED', // Use STOPPED instead of INACTIVE for clarity
updatedAt: new Date()
}
})
console.log('🛑 Database update result:', updateResult)
console.log('🛑 All automation sessions marked as STOPPED in database')
if (success) {
return NextResponse.json({ success: true, message: 'Automation stopped successfully' })
} else {
return NextResponse.json({ success: false, error: 'Failed to stop automation' }, { status: 500 })
}
} catch (error) {
console.error('Stop automation error:', error)
return NextResponse.json({
success: false,
error: 'Internal server error',
message: error.message
}, { status: 500 })
}
}