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

import cd.casic.ci.process.engine.scheduler.config.QuartzSchedulerManager;
import cd.casic.ci.process.engine.scheduler.dao.PipelineSchedulingPropertiesDao;
import cd.casic.ci.process.engine.scheduler.dateObject.PipelineSchedulingProperties;
import cd.casic.ci.process.engine.scheduler.req.PipelineSchedulingReq;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author HopeLi
 * @version v1.0
 * @ClassName PipelineSchedulingBootstrapper
 * @Date: 2025/5/27 17:46
 * @Description: 
 */

@Component
@RequiredArgsConstructor
public class PipelineSchedulingBootstrapper {
    private final QuartzSchedulerManager quartzSchedulerManager;
    @Resource
    private PipelineSchedulingPropertiesDao pipelineSchedulingPropertiesDao;

    @PostConstruct
    public void init(){
        List<PipelineSchedulingProperties> tasks = pipelineSchedulingPropertiesDao.selectList();
        if (!CollectionUtils.isEmpty(tasks)){
            for (PipelineSchedulingProperties task : tasks) {
                try {
                    PipelineSchedulingReq req = new PipelineSchedulingReq();
                    req.setPipelineId(task.getPipelineId());
                    req.setCron(task.getCron());
                    quartzSchedulerManager.addTask(req);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
