package cn.reghao.autodop.dmaster.monitor;

import cn.reghao.autodop.dmaster.monitor.job.AppHealthCheckJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 监控使用的定时任务调度器
 *
 * @author reghao
 * @date 2020-03-20 17:04:48
 */
@Slf4j
@Service
public class MonitorScheduler {
    private final Scheduler scheduler;
    private final MonitorJobData monitorJobData;

    public MonitorScheduler(MonitorJobData monitorJobData) throws SchedulerException {
        this.scheduler = StdSchedulerFactory.getDefaultScheduler();
        this.monitorJobData = monitorJobData;
    }

    /**
     * TODO 系统启动时启用所有存在的任务
     *
     * @param
     * @return
     * @date 2021-06-24 上午10:22
     */
    @PostConstruct
    public void startScheduler() throws SchedulerException {
        //startAllJobs();
        //scheduler.start();
    }

    private void startAllJobs() throws SchedulerException {
        List<MonitorJob> jobs = new ArrayList<>();
        for (MonitorJob job : jobs) {
            if (job.getEnable()) {
                JobDataMap jobDataMap = monitorJobData.appDataMap();
                if (jobDataMap != null) {
                    addJob(job, jobDataMap);
                }
            }
        }
    }

    public void addJob(MonitorJob monitorJob, JobDataMap jobDataMap) throws SchedulerException {
        String jobId = monitorJob.getJobId();
        String cronExp = monitorJob.getCronExp();
        Class<? extends Job> clazz = AppHealthCheckJob.class;

        JobDetail jobDetail = JobBuilder.newJob(clazz)
                .withIdentity(jobId)
                // job 需要使用的数据
                .usingJobData(jobDataMap)
                .build();

        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(jobId)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExp))
                .build();

        if (!isJobExist(jobId)) {
            scheduler.scheduleJob(jobDetail, cronTrigger);
            log.info("添加新定时任务 {}", jobId);
        }
    }

    public void rescheduleJob(String jobId, String cronExp) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobId);
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(jobId)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronExp))
                .build();
        scheduler.rescheduleJob(triggerKey, cronTrigger);
    }

    public void deleteJob(String jobId) throws SchedulerException {
        scheduler.deleteJob(JobKey.jobKey(jobId));
    }

    public void pauseJob(String jobId) throws SchedulerException {
        scheduler.pauseJob(JobKey.jobKey(jobId));
    }

    public void resumeJob(String jobId) throws SchedulerException {
        scheduler.resumeJob(JobKey.jobKey(jobId));
    }

    public boolean isJobExist(String jobId) throws SchedulerException {
        return scheduler.checkExists(JobKey.jobKey(jobId));
    }
}
