package com.gkhy.safePlatform.safeCheck.Schedules;
|
|
|
import com.gkhy.safePlatform.safeCheck.entity.SafeCheckWork;
|
import com.gkhy.safePlatform.safeCheck.enums.WorkStatusEnum;
|
import com.gkhy.safePlatform.safeCheck.mq.msg.SafeCheckCreateTaskMsg;
|
import com.gkhy.safePlatform.safeCheck.service.SafeCheckTaskUnitSchedulesService;
|
import com.gkhy.safePlatform.safeCheck.service.impl.SafeCheckTaskUnitSchedulesServicelmpl;
|
import com.gkhy.safePlatform.safeCheck.util.ConsolePrintIfEnvIsDevUtil;
|
import org.apache.log4j.LogManager;
|
import org.apache.log4j.Logger;
|
import org.apache.rocketmq.client.producer.SendResult;
|
import org.apache.rocketmq.client.producer.SendStatus;
|
import org.apache.rocketmq.spring.core.RocketMQTemplate;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.stereotype.Component;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.PostConstruct;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
import java.util.List;
|
|
|
@Component
|
@ConditionalOnProperty(prefix = "safeCheckScheduling", name = "enabled", havingValue = "true")
|
public class SafeCheckScheduleWork {
|
|
private Logger logger = LogManager.getLogger(SafeCheckWork.class);
|
|
|
@Autowired
|
private SafeCheckTaskUnitSchedulesService taskUnitSchedulesService;
|
|
@Autowired
|
private RocketMQTemplate rocketMQTemplate;
|
|
@Value("${rocketmq.topic.safeCheckCreateTaskTopic}")
|
private String safeCheckCreateTaskTopic;
|
|
|
public SafeCheckScheduleWork() {
|
}
|
|
|
@Scheduled(cron = "0 0/5 * * * ?") //每隔5分钟执行一次
|
@PostConstruct
|
public void createNewWorkTask() throws Exception {
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev("【智能巡检】【定时生成巡检任务】[定时查询正常任务-->启动中.......]"+new Date());
|
// System.out.println("【智能巡检】[定时生成巡检任务][定时任务启动中.......]"+new Date());
|
logger.info("【智能巡检】[定时生成巡检任务]");
|
List<SafeCheckWork> worksList = taskUnitSchedulesService.findActiveWorkListByTime();
|
if(worksList != null && worksList.size()>0){
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev(new Date() +" 【智能巡检】[定时生成巡检任务单元][创建巡检任务] 数量 : "+worksList.size());
|
// System.out.println(new Date() +" 【智能巡检】[定时生成巡检任务单元][创建巡检任务] 数量 : "+worksList.size());
|
logger.info(" 【智能巡检】[定时生成巡检任务单元][创建巡检任务] 数量 : "+ worksList.size());
|
worksList.forEach(work -> {
|
//1、mq发送生成task消息
|
SafeCheckCreateTaskMsg createTaskMsg = new SafeCheckCreateTaskMsg();
|
createTaskMsg.setWorkId(work.getId());
|
//在获取到调度信息后的一分钟内创建任务
|
// createTaskMsg.setCreateTaskTime(new Date(work.getNextNoticeTime().getTime()-60*1000));
|
createTaskMsg.setCreateTaskTime(new Date((new Date()).getTime()+60*1000));
|
SendResult sendResult = rocketMQTemplate.syncSend(safeCheckCreateTaskTopic, createTaskMsg);
|
// System.out.println(new Date() +" 【智能巡检】[定时生成巡检任务单元][发送消息,在获取到调度信息后的一分钟内创建任务] : "+work.getId());
|
logger.info(new Date() +" 【智能巡检】[定时生成巡检任务单元][发送消息,在获取到调度信息后的一分钟内创建任务] : "+work.getId());
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev(new Date() +" 【智能巡检】[定时生成巡检任务单元][发送消息,在获取到调度信息后的一分钟内创建任务] : "+work.getId());
|
if(sendResult.getSendStatus() == SendStatus.SEND_OK){
|
//2、修改调度状态
|
work.setWorkStatus(WorkStatusEnum.WORK_STATUS_DISPATCHING.getStatus());
|
taskUnitSchedulesService.updateWorkStatusById(work);
|
}
|
});
|
}
|
}
|
|
/**
|
* 重置错误的任务调度信息 调度表中如果通知时间在当前时间之前,对调度时间进行重置
|
* @throws Exception
|
*/
|
@Scheduled(cron = "0 0/4 * * * ?") //每隔5分钟执行一次
|
public void resetWorkErrorSchedule() throws Exception {
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev("【智能巡检】【定时生成巡检任务】[定时排查异常任务-->启动中.......]"+new Date());
|
// System.out.println("【智能巡检】[定时生成巡检任务]【定时排查异常任务】启动中.......]"+new Date());
|
logger.info("[调度异常任务处理]");
|
List<SafeCheckWork> worksList = taskUnitSchedulesService.findFaildScheduleList();
|
if(worksList != null && worksList.size()>0){
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev(new Date()+" [调度异常任务处理][重置调度时间] 数量 : "+worksList.size());
|
// System.out.println(new Date()+" [调度异常任务处理][重置调度时间] 数量 : "+worksList.size());
|
logger.info(" [调度异常任务处理][重置调度时间] 数量 : "+worksList.size());
|
worksList.forEach(schedule -> {
|
Date nextNoticeTime = schedule.getNextNoticeTime();
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev(new Date() +" 【智能巡检】[定时检查异常巡检任务单元][在当前时间十分钟后发送进行消息通知] : "+schedule.getId());
|
logger.info(new Date() +" 【智能巡检】[定时检查异常巡检任务单元][在当前时间十分钟后发送进行消息通知] : "+schedule.getId());
|
// System.out.println(new Date() +" 【智能巡检】[定时检查异常巡检任务单元][在当前时间十分钟后发送进行消息通知] : "+schedule.getId());
|
Date newNoticeTime = new Date((new Date()).getTime() + (10 * 60 * 1000));
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
logger.info(" [调度异常任务处理][重置任务调度时间] WORKID : "+ schedule.getId()+" 旧的通知时间:"+ formatter.format(nextNoticeTime)+" 新的通知时间:"+formatter.format(newNoticeTime));
|
ConsolePrintIfEnvIsDevUtil.consolePrintIfEnvIsDev(
|
"[调度异常任务处理][重置任务调度时间] WORKID : "+ schedule.getId()+" 旧的通知时间:"+ formatter.format(nextNoticeTime)+" 新的通知时间:"+formatter.format(newNoticeTime));
|
// System.out.println(" [调度异常任务处理][重置任务调度时间] WORKID : "+ schedule.getId()+" 旧的通知时间:"+ formatter.format(nextNoticeTime)+" 新的通知时间:"+formatter.format(newNoticeTime));
|
if(schedule !=null && schedule.getId()>0){
|
taskUnitSchedulesService.resetScheduleTime(schedule.getId(),newNoticeTime);
|
}
|
});
|
}
|
}
|
}
|