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

import com.ozs.da.constant.Constant;
import com.ozs.da.dao.TaskDao;
import com.ozs.da.entity.task.Task;
import com.ozs.da.job.JobServer;
import com.ozs.da.service.job.ScheduleJobContainerManagerService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 许明
 * on 2019/3/13.
 * @version 2.0.0
 */
@Service
public class ScheduleJobContainerManagerServiceImpl implements ScheduleJobContainerManagerService
{
	private static final Logger logger = LogManager.getLogger(ScheduleJobContainerManagerServiceImpl.class);

	private static final ReentrantLock jobFlowStructLock = new ReentrantLock();

	@Autowired
	private TaskDao taskDao;

	@Override
	public int initMemJobToCache() throws ParseException, SchedulerException {
		List<Task> taskList = taskDao.getAllInitJob();
		if (taskList == null)
		{
			return Constant.RETURN_FAILD;
		}
		for (Task task : taskList)
		{
			JobServer.MEM_JOB_CACHE.put(task.getId(), task);
		}
		logger.info("job信息由数据库加载到内存成功..........");
		logger.info("开始重新刷新所有调度的定时器....");
        SchedulerTimeTriggeJob.putMemJobSchedule(taskList);
		logger.info("重新刷新所有调度的定时器结束....");
		taskList.clear();
		return Constant.RETURN_OK;
	}

}