Commit 64b060a6 by tiger

任务自动过期机制

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