package cd.casic.ci.process.engine.scheduler.config;

import org.quartz.Scheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName QuartzConfig
 * @Date: 2025/5/27 21:16
 * @Description:
 */

@Configuration
public class QuartzConfig {
    private final ApplicationContext applicationContext;

    public QuartzConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public Scheduler scheduler() throws Exception {
        SpringBeanJobFactory jobFactory = new SpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);

        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setJobFactory(jobFactory);
        factory.afterPropertiesSet();

        Scheduler scheduler = factory.getScheduler();
        scheduler.start();
        return scheduler;
    }
}
