refactor: simplify bot control logic and improve error handling in routes

This commit is contained in:
2025-11-04 19:44:20 +01:00
parent 9e2a8a65dc
commit c256f5265e
4 changed files with 61 additions and 105 deletions

View File

@@ -1,15 +1,12 @@
import { dashboardState } from './state'
// We'll import and run the bot directly in the same process
let botRunning = false
let botPromise: Promise<void> | null = null
import type { MicrosoftRewardsBot } from '../index'
export class BotController {
private isRunning: boolean = false
private botInstance: MicrosoftRewardsBot | null = null
private botPromise: Promise<void> | null = null
private startTime?: Date
constructor() {
// Cleanup on exit
process.on('exit', () => this.stop())
}
@@ -26,30 +23,26 @@ export class BotController {
}
public async start(): Promise<{ success: boolean; error?: string; pid?: number }> {
if (this.isRunning || botRunning) {
if (this.botInstance) {
return { success: false, error: 'Bot is already running' }
}
try {
this.log('🚀 Starting bot...', 'log')
// Import the bot main logic
const { MicrosoftRewardsBot } = await import('../index')
this.isRunning = true
botRunning = true
this.botInstance = new MicrosoftRewardsBot(false)
this.startTime = new Date()
dashboardState.setRunning(true)
dashboardState.setBotInstance(this.botInstance)
// Run the bot in the same process using the exact same logic as npm start
botPromise = (async () => {
this.botPromise = (async () => {
try {
const rewardsBot = new MicrosoftRewardsBot(false)
this.log('✓ Bot initialized, starting execution...', 'log')
await rewardsBot.initialize()
await rewardsBot.run()
await this.botInstance!.initialize()
await this.botInstance!.run()
this.log('✓ Bot completed successfully', 'log')
} catch (error) {
@@ -61,8 +54,7 @@ export class BotController {
}
})()
// Don't await - let it run in background
botPromise.catch(error => {
this.botPromise.catch(error => {
this.log(`Bot execution failed: ${error instanceof Error ? error.message : String(error)}`, 'error')
})
@@ -77,15 +69,12 @@ export class BotController {
}
public stop(): { success: boolean; error?: string } {
if (!this.isRunning && !botRunning) {
if (!this.botInstance) {
return { success: false, error: 'Bot is not running' }
}
try {
this.log('🛑 Stopping bot...', 'warn')
// For now, we can't gracefully stop a running bot in the same process
// This would require refactoring the bot to support cancellation
this.log('⚠ Note: Bot will complete current task before stopping', 'warn')
this.cleanup()
@@ -103,7 +92,6 @@ export class BotController {
this.log('🔄 Restarting bot...', 'log')
this.stop()
// Wait a bit before restarting
return new Promise((resolve) => {
setTimeout(async () => {
const result = await this.start()
@@ -119,7 +107,7 @@ export class BotController {
startTime?: string
} {
return {
running: this.isRunning || botRunning,
running: !!this.botInstance,
pid: process.pid,
uptime: this.startTime ? Date.now() - this.startTime.getTime() : undefined,
startTime: this.startTime?.toISOString()
@@ -127,13 +115,12 @@ export class BotController {
}
private cleanup(): void {
this.isRunning = false
botRunning = false
botPromise = null
this.botInstance = null
this.botPromise = null
this.startTime = undefined
dashboardState.setRunning(false)
dashboardState.setBotInstance(undefined)
}
}
// Singleton instance
export const botController = new BotController()