Commit 64b060a6 by tiger

任务自动过期机制

1 parent 5b731da3
package cn.com.glsx.process.modules.entity;
import java.util.Date;
import java.util.List;
import javax.persistence.Column;
......@@ -112,13 +113,18 @@ public class SopInstNode {
*/
@Column(name = "daily_limit")
private Integer dailyLimit;
/**
* 每日执行次数限制,0表示不限制。
*/
@Column(name = "daily_count")
private Integer dailyCount = 0;
/**
* 每日任务过期时间,创建批次时重置,任务执行时检查。
*/
@Column(name = "daily_expire")
private Date dailyExpire;
@Transient
private List<SopInstNode> sopInstNodeList;
......@@ -420,4 +426,12 @@ public class SopInstNode {
this.dailyCount = dailyCount;
}
public Date getDailyExpire() {
return dailyExpire;
}
public void setDailyExpire(Date dailyExpire) {
this.dailyExpire = dailyExpire;
}
}
\ No newline at end of file
......@@ -48,13 +48,17 @@ public class SopXxlJobUtil {
// 遍历node,停止任务
for (SopInstNode node : sop.getInstNodeMap().values()) {
jobBusId = jobBusId(sop.getInst(), node);
job = XxlJobUtil.findByBusKey(jobBusId);
pauseSopNode(sop, node);
}
}
if (job != null && job.getTriggerStatus() == 1) {
String pause = XxlJobUtil.pause(jobBusId);
SopXxlJobUtil.log(log, "【{}】Sop任务节点停止:{}", jobBusId, pause);
}
public static void pauseSopNode(SopStruct sop, SopInstNode node) {
String jobBusId = jobBusId(sop.getInst(), node);
XxlJobInfo job = XxlJobUtil.findByBusKey(jobBusId);
if (job != null && job.getTriggerStatus() == 1) {
String pause = XxlJobUtil.pause(jobBusId);
SopXxlJobUtil.log(log, "【{}】Sop任务节点停止:{}", jobBusId, pause);
}
}
......@@ -122,6 +126,12 @@ public class SopXxlJobUtil {
int i = 0;
for (Integer nodeId : sop.getNodeSort()) {
SopInstNode node = sop.getInstNodeMap().get(nodeId);
if (node.getDailyExpire() != null && node.getDailyExpire().before(new Date())) {
node.setNodeStatus(-1);
continue;
}
initSopNodeJob(sop, node, i++);
}
}
......
......@@ -8,7 +8,6 @@ import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import cn.com.glsx.process.job.xxljob.core.SopXxlJobUtil;
import cn.com.glsx.process.modules.entity.SopInstBatch;
import cn.com.glsx.process.modules.model.SopStruct;
import cn.com.glsx.process.modules.service.SopService;
import lombok.extern.slf4j.Slf4j;
......@@ -32,8 +31,7 @@ public class SopInstExecuteJobHandler {
// 创建执行批次
if (sop.getInst().getInstStatus() == 1) {
SopInstBatch batch = sopService.createBatch(sop.getInst());
sop.setBatch(batch);
sopService.createBatch(sop);
}
// 创建所有节点任务
......
......@@ -114,6 +114,13 @@ public class SopInstNodeExecuteJobHandler {
sop = reloadAndCreateNodeBatch(sop, node);
node = sop.getInstNodeMap().get(node.getNodeId());
// 节点过期
if (node.getDailyExpire() != null && node.getDailyExpire().before(new Date())) {
node.setNodeStatus(-1);
SopXxlJobUtil.pauseSopNode(sop, node);
return;
}
// 没超执行次数限制
if (node.getDailyLimit() == 0 || node.getDailyCount() < node.getDailyLimit()) {
// 预备执行参数
......
......@@ -9,6 +9,7 @@ import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.quartz.CronExpression;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -161,7 +162,9 @@ public class SopService {
return sop;
}
public SopInstBatch createBatch(SopInst sopInst) {
public SopInstBatch createBatch(SopStruct sop) {
SopInst sopInst = sop.getInst();
SopInstBatch batch = new SopInstBatch();
batch.setBatchNo(SnowFlake.nextSerialNumber());
batch.setCreateTime(new Date());
......@@ -179,6 +182,21 @@ public class SopService {
// 重置节点执行次数
instNodeMapper.resetInstNodeDailyCount(sopInst.getInstId());
for (SopInstNode node : sop.getInstNodeMap().values()) {
if (StrUtil.isBlank(node.getNodeExpireCron())) {
continue;
}
try {
Date expire = new CronExpression(node.getNodeExpireCron()).getNextValidTimeAfter(new Date());
node.setDailyExpire(expire);
instNodeMapper.updateByPrimaryKey(node);
} catch (Exception e) {
log.info("【{}】过期格式不对:{}", node.getNodeName(), node.getNodeExpireCron(), e);
}
}
sop.setBatch(batch);
return batch;
}
......@@ -214,7 +232,6 @@ public class SopService {
instNodeMapper.updateByPrimaryKey(node); // 更新执行次数
}
/** 从实例倒推出模板,主要用于开发阶段 */
@Transactional
public void generateTplFromInst(SopStruct sop) {
......
......@@ -20,12 +20,13 @@
<result column="node_status" jdbcType="INTEGER" property="nodeStatus" />
<result column="daily_limit" jdbcType="INTEGER" property="dailyLimit" />
<result column="daily_count" jdbcType="INTEGER" property="dailyCount" />
<result column="daily_expire" jdbcType="TIMESTAMP" property="dailyExpire" />
</resultMap>
<sql id="Base_Column_List">
<!-- WARNING - @mbg.generated -->
inst_node_id, inst_id, node_id, node_name, node_type, node_label, node_key, node_show,
node_param, node_content, node_visible, node_skip, node_cron, node_expire_cron, node_status, daily_limit, daily_count
node_param, node_content, node_visible, node_skip, node_cron, node_expire_cron, node_status, daily_limit, daily_count, daily_expire
</sql>
<select id="findByInstId" resultMap="BaseResultMap" parameterType="java.lang.String">
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!