package com.ruoyi.robot.config;

import com.ruoyi.robot.task.LdTaskScheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定时任务配置
 * 应用启动时自动启动任务调度器
 */
@Configuration
public class TaskSchedulerConfig {

    private static final Logger log = LoggerFactory.getLogger(TaskSchedulerConfig.class);

    @Autowired
    private LdTaskScheduler ldTaskScheduler;

    /**
     * 应用启动后自动启动定时任务调度器
     */
    @Bean
    public ApplicationRunner taskSchedulerRunner() {
        return applicationArguments -> {
            log.info("正在启动定时任务调度器...");
            try {
                ldTaskScheduler.start();
                log.info("定时任务调度器启动成功");
            } catch (Exception e) {
                log.error("定时任务调度器启动失败", e);
            }
        };
    }
}
