mirror of
https://github.com/LightZirconite/Microsoft-Rewards-Bot.git
synced 2026-01-10 17:26:17 +00:00
fix: Improve scheduler management by enabling it first to provide immediate information and then run tasks
This commit is contained in:
45
src/index.ts
45
src/index.ts
@@ -1051,20 +1051,11 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
// Check if scheduling is enabled
|
// Check if scheduling is enabled
|
||||||
if (config.scheduling?.enabled) {
|
if (config.scheduling?.enabled) {
|
||||||
// IMPROVED: Run tasks immediately first, THEN start scheduler for future runs
|
// IMPROVED: Start scheduler FIRST to show schedule info immediately, THEN run tasks
|
||||||
// This ensures "npm start" always executes tasks right away instead of waiting until next scheduled time
|
// This gives users instant confirmation of the cron schedule without waiting for long execution
|
||||||
log('main', 'MAIN', 'Scheduling enabled - executing immediate run, then activating scheduler', 'log', 'cyan')
|
log('main', 'MAIN', 'Scheduling enabled - activating scheduler, then executing immediate run', 'log', 'cyan')
|
||||||
|
|
||||||
try {
|
// Initialize and start scheduler first
|
||||||
await rewardsBot.initialize()
|
|
||||||
await rewardsBot.run()
|
|
||||||
log('main', 'MAIN', '✓ Initial run completed successfully', 'log', 'green')
|
|
||||||
} catch (error) {
|
|
||||||
log('main', 'MAIN', `Initial run failed: ${error instanceof Error ? error.message : String(error)}`, 'error')
|
|
||||||
// Continue to scheduler activation even if initial run fails
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize scheduler for future runs
|
|
||||||
scheduler = new InternalScheduler(config, async () => {
|
scheduler = new InternalScheduler(config, async () => {
|
||||||
try {
|
try {
|
||||||
await rewardsBot.initialize()
|
await rewardsBot.initialize()
|
||||||
@@ -1077,17 +1068,27 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
const schedulerStarted = scheduler.start()
|
const schedulerStarted = scheduler.start()
|
||||||
|
|
||||||
if (schedulerStarted) {
|
if (!schedulerStarted) {
|
||||||
log('main', 'MAIN', 'Bot running in scheduled mode. Process will stay alive.', 'log', 'green')
|
log('main', 'MAIN', 'Scheduler failed to start. Exiting.', 'error')
|
||||||
log('main', 'MAIN', 'Press CTRL+C to stop the scheduler and exit.', 'log', 'cyan')
|
gracefulExit(1)
|
||||||
// Keep process alive - scheduler handles execution
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
log('main', 'MAIN', 'Scheduler failed to start. Exiting after one-time execution.', 'warn')
|
|
||||||
scheduler = null
|
|
||||||
// Already ran once above, so just exit
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log('main', 'MAIN', 'Bot running in scheduled mode. Process will stay alive.', 'log', 'green')
|
||||||
|
log('main', 'MAIN', 'Press CTRL+C to stop the scheduler and exit.', 'log', 'cyan')
|
||||||
|
|
||||||
|
// Now run initial execution (scheduler already active for future runs)
|
||||||
|
try {
|
||||||
|
await rewardsBot.initialize()
|
||||||
|
await rewardsBot.run()
|
||||||
|
log('main', 'MAIN', '✓ Initial run completed successfully', 'log', 'green')
|
||||||
|
} catch (error) {
|
||||||
|
log('main', 'MAIN', `Initial run failed: ${error instanceof Error ? error.message : String(error)}`, 'error')
|
||||||
|
// Scheduler still active - will retry at next scheduled time
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep process alive - scheduler handles future executions
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// One-time execution (scheduling disabled)
|
// One-time execution (scheduling disabled)
|
||||||
|
|||||||
Reference in New Issue
Block a user