package com.ozs.da.service.job.impl;

import com.ozs.da.constant.Constant;
import com.ozs.da.service.job.JobService;
import com.ozs.da.service.job.ScheDisruptInitService;
import com.ozs.da.service.job.ScheduleJobContainerManagerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 许明
 * on 2019/3/7.
 * @version 2.0.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class JobServiceImpl implements JobService {

    private static final Logger logger = LogManager.getLogger(JobServiceImpl.class);
    private static boolean initFlag = false;

    @Autowired
    private ScheDisruptInitService scheDisruptInitService;
    @Autowired
    private ScheduleJobContainerManagerService scheduleJobContainerManagerService;

    @Override
    public int init() throws Exception {
        if (!(initFlag)) {
            logger.info("进入数据中心调度任务轮询初始化方法init ...........................");
            initFlag = true;
            try
            {
                if(Constant.RETURN_FAILD == SchedulerTimeTriggeJob.initScheduler()){
                    logger.error("调度初始化Scheduler实例失败");
                }
                else{
                    SchedulerTimeTriggeJob.start();
                    SchedulerTimeTriggeJob.clear();
                }
            }
            catch (SchedulerException e)
            {
                logger.error("启动Scheduler实例失败.........", e);
                return Constant.RETURN_FAILD;
            }
            logger.info("初始化调度信息到内存.........");
            if(Constant.RETURN_FAILD == scheduleJobContainerManagerService.initMemJobToCache()){
                logger.error("初始化调度信息到内存失败.........");
                return Constant.RETURN_FAILD;
            }
//            scheduleJobContainerManagerService.startJobContainerSchedule();
            logger.info("初始化job运行条件判断线程.........");
            scheDisruptInitService.initJobConJudgeDisruptor();
        }
        return Constant.RETURN_OK;
    }
}
