package com.friday.custody.applicationRunner;

import com.friday.custody.service.basicService.QuartzScheduleTaskService;
import com.friday.custody.entity.ScheduleTask;
import com.friday.custody.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DynamicCronJobsStarter implements ApplicationRunner {
    @Autowired
    private QuartzScheduleTaskService quartzScheduleTaskService;
    @Resource
    private Scheduler scheduler;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<ScheduleTask> list = quartzScheduleTaskService.list();
        list.forEach(t-> {
            try {
                QuartzUtils.resumeScheduleJob(scheduler, t.getJobName());
            } catch (SchedulerException e) {
                throw new RuntimeException(e);
            }

        });
    }
}
