Commit 3622ddfb by huangfusuper

删除过期类

parent dc010ac7
package com.byit.thread.helper;
import com.byit.dto.plugin.StatisticData;
import com.byit.mapper.FlowMapper;
import com.byit.mapper.FlowStatusSnapshootMapper;
import com.byit.mapper.JobTaskRunLogMapper;
import com.byit.mapper.RunRecordingMapper;
import com.byit.model.Flow;
import com.byit.model.FlowStatusSnapshoot;
import com.byit.model.RunRecording;
import com.byit.thread.BaseThreadRunHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Description 扫描状态并保存快照的线程类
* @Author guo_m
* @Date 2020-04-02
*/
@Deprecated
@Slf4j
public class StatusSnapshootThreadRunHelper extends BaseThreadRunHelper {
private static final String LOCK_NAME = "flow_status_snapshoot_lock";
@Resource
private FlowMapper flowMapper;
@Resource
private FlowStatusSnapshootMapper flowStatusSnapshootMapper;
@Resource
private RunRecordingMapper runRecordingMapper;
@Resource
private JobTaskRunLogMapper jobTaskRunLogMapper;
@Resource
private DataSource dataSource;
@Value("${myth-job.snapshoot-date}")
private Integer snapshootDate;
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
@Override
public Long start() {
LocalDateTime localDateTime = LocalDateTime.now();
String date = localDateTime.format(DATE_FORMATTER);
String hour = String.valueOf(localDateTime.getHour());
log.info("开始记录快照 day【{}】hour【{}】---", date, hour);
Integer num = flowStatusSnapshootMapper.exist(date, hour);
//判断是否存储过快照
if (num != null && num >= 1){
log.info("没有工作流无需记录---");
return (60 - LocalDateTime.now().getMinute()) * 60 * 1000L;
}else {
//获取所有剩余执行次数不为零的工作流
List<Flow> flowList = flowMapper.findAll();
if (flowList != null && flowList.size() > 0){
LocalDateTime hourDateTime = localDateTime
.minusMinutes(localDateTime.getMinute()) //减去分钟
.minusSeconds(localDateTime.getSecond()) //减去秒
.minusNanos(localDateTime.getNano()); //减去纳秒
Long hourTime = hourDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); //转换时间戳
List<FlowStatusSnapshoot> flowStatusSnapshootList = new ArrayList<>();
flowList.forEach(flow -> {
//获取今天凌晨的时间戳
Long time = LocalDate.now().atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
//获取今天的运行实例
List<RunRecording> runRecordingList = runRecordingMapper.findByPreTime(new Date(time), flow.getFlowId());
FlowStatusSnapshoot flowStatusSnapshoot = new FlowStatusSnapshoot();
flowStatusSnapshoot.setDay(date);
flowStatusSnapshoot.setFlowId(flow.getFlowId());
flowStatusSnapshoot.setHour(hour);
flowStatusSnapshoot.setFlowName(flow.getFlowName());
flowStatusSnapshoot.setWorkspaceId(flow.getWorkspaceId());
flowStatusSnapshoot.setSnapshootTime(hourTime);
//统计运行实例的数据
//如果存在实例
if (null != runRecordingList && runRecordingList.size() > 0){
buildUnFinish(runRecordingList, flowStatusSnapshootList, flowStatusSnapshoot, flow);
}else {
//如果不存在运行实例
flowStatusSnapshoot.setFlowStatus(1);
flowStatusSnapshoot.setUnstartNode(flow.getFlowNodeCount());
flowStatusSnapshootList.add(flowStatusSnapshoot);
}
});
if (null != flowStatusSnapshootList && flowStatusSnapshootList.size() > 0){
flowStatusSnapshootMapper.saveList(flowStatusSnapshootList);
}
//获取过期的时间戳
Long outTime = hourDateTime.minusDays(snapshootDate).toInstant(ZoneOffset.of("+8")).toEpochMilli(); //转换时间戳;
//删除超过期限的快照
flowStatusSnapshootMapper.deleteOutSnapShoot(outTime);
}
}
log.info("记录快照完成---");
return (60 - LocalTime.now().getMinute()) * 60 * 1000L;
}
/**
* 功能描述 当天的和未完成的工作流的快照
* @author gml
* @date 2020-05-08 10:20
* @param runRecordingList
* @param flowStatusSnapshootList
* @param flowStatusSnapshoot
* @return void
*/
private void buildUnFinish(List<RunRecording> runRecordingList, List<FlowStatusSnapshoot> flowStatusSnapshootList, FlowStatusSnapshoot flowStatusSnapshoot, Flow flow){
//如果存在,记录快照
runRecordingList.forEach(runRecording -> {
FlowStatusSnapshoot hourFlowStatusSnapshoot = new FlowStatusSnapshoot();
BeanUtils.copyProperties(flowStatusSnapshoot, hourFlowStatusSnapshoot);
//判断运行状态
if(StringUtils.isEmpty(runRecording.getFlowStatus())){
//如果为空设置为未运行
hourFlowStatusSnapshoot.setFlowStatus(1);
}else if (runRecording.getFlowStatus().equals("1") || runRecording.getFlowStatus().equals("2") || runRecording.getFlowStatus().equals("3")){
hourFlowStatusSnapshoot.setFlowStatus(Integer.valueOf(runRecording.getFlowStatus()));
}else if (runRecording.getFlowStatus().equals("4")){
switch (runRecording.getFlowRunResult()){
case "1" :
case "3" : hourFlowStatusSnapshoot.setFlowStatus(4); break;
case "2" :
case "4" : hourFlowStatusSnapshoot.setFlowStatus(5); break;
case "5" : hourFlowStatusSnapshoot.setFlowStatus(6); break;
default : hourFlowStatusSnapshoot.setFlowStatus(5); break;
}
}else {
//如果没在判断之内,设置为未运行
hourFlowStatusSnapshoot.setFlowStatus(1);
}
//获取运行实例下各类状态的节点数目
StatisticData nodeStatistic = jobTaskRunLogMapper.findStatisticDataByRunIdAndFlowId(runRecording.getRunId(), flow.getFlowId());
//判断是否存在节点的运行日志
//存在节点运行日志
if (nodeStatistic != null){
int sum = nodeStatistic.getFail() + nodeStatistic.getSuccess() + nodeStatistic.getRunIng() + nodeStatistic.getKill();
if(flow.getFlowNodeCount() > sum){
hourFlowStatusSnapshoot.setUnstartNode(flow.getFlowNodeCount() - sum);
}else {
hourFlowStatusSnapshoot.setUnstartNode(0);
}
hourFlowStatusSnapshoot.setRuningNode(nodeStatistic.getRunIng());
hourFlowStatusSnapshoot.setSuccessNode(nodeStatistic.getSuccess());
hourFlowStatusSnapshoot.setFailNode(nodeStatistic.getFail());
hourFlowStatusSnapshoot.setKillNode(nodeStatistic.getKill());
}else {
//不存在节点的运行日志
hourFlowStatusSnapshoot.setUnstartNode(flow.getFlowNodeCount());
}
flowStatusSnapshootList.add(hourFlowStatusSnapshoot);
});
}
@Override
public DataSource getDataSource() {
return dataSource;
}
@Override
public String getLockName() {
return LOCK_NAME;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment