From 8c80c577cbfc6e5e824efc8aa6bc2a77cedec5cb Mon Sep 17 00:00:00 2001 From: mindesbunister Date: Thu, 24 Jul 2025 17:42:50 +0200 Subject: [PATCH] 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. --- app/api/automation/stop/route.js | 12 +++++++++--- lib/automation-service-simple.ts | 11 ++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/api/automation/stop/route.js b/app/api/automation/stop/route.js index b2b2274..c2014e6 100644 --- a/app/api/automation/stop/route.js +++ b/app/api/automation/stop/route.js @@ -6,21 +6,27 @@ 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 - await prisma.automationSession.updateMany({ + console.log('🛑 Updating database sessions to STOPPED') + const updateResult = await prisma.automationSession.updateMany({ where: { status: 'ACTIVE' }, data: { - status: 'INACTIVE', + status: 'STOPPED', // Use STOPPED instead of INACTIVE for clarity updatedAt: new Date() } }) - console.log('🛑 All automation sessions marked as INACTIVE in database') + 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' }) diff --git a/lib/automation-service-simple.ts b/lib/automation-service-simple.ts index ffe12ce..c0b1e20 100644 --- a/lib/automation-service-simple.ts +++ b/lib/automation-service-simple.ts @@ -214,7 +214,11 @@ export class AutomationService { } private async runAutomationCycle(): Promise { - if (!this.config) return + // Check if automation should still be running + if (!this.isRunning || !this.config) { + console.log('🛑 Automation cycle stopped - isRunning:', this.isRunning, 'config:', !!this.config) + return + } try { console.log(`🔍 Running automation cycle for ${this.config.symbol} ${this.config.timeframe}`) @@ -1310,14 +1314,19 @@ ${validResults.map(r => `• ${r.timeframe}: ${r.analysis?.recommendation} (${r. async stopAutomation(): Promise { try { + console.log('🛑 Stopping automation service...') this.isRunning = false // Clear the interval if it exists if (this.intervalId) { + console.log('🛑 Clearing automation interval') clearInterval(this.intervalId) this.intervalId = null } + // Reset config to prevent any residual processes + this.config = null + // Stop price monitoring try { await priceMonitorService.stopMonitoring()