package com.qqchen.deploy.backend.schedule.config;

import com.qqchen.deploy.backend.schedule.service.IScheduleJobService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 定时任务加载器
 * 在应用启动时自动加载数据库中的定时任务到Quartz调度器
 *
 * @author qichen
 */
@Slf4j
@Component
public class JobLoader implements ApplicationRunner {

    @Resource
    private IScheduleJobService scheduleJobService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("========== 开始加载定时任务 ==========");
        try {
            scheduleJobService.loadAllEnabledJobs();
            log.info("========== 定时任务加载完成 ==========");
        } catch (Exception e) {
            log.error("========== 定时任务加载失败 ==========", e);
        }
    }
}

