Commit ebf8214a by 林永健

Merge remote-tracking branch 'origin/v1.0.1' into v1.0.1

2 parents a8db5c8f b8a00a76
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,15 +48,19 @@ public class SopXxlJobUtil {
// 遍历node,停止任务
for (SopInstNode node : sop.getInstNodeMap().values()) {
jobBusId = jobBusId(sop.getInst(), node);
job = XxlJobUtil.findByBusKey(jobBusId);
pauseSopNode(sop, node);
}
}
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);
}
}
}
public static void initLoadJob() {
XxlJobInfo job = XxlJobUtil.findByBusKey(SOP_LOAD_JOB_ID);
......@@ -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;
......@@ -31,11 +30,9 @@ public class SopInstExecuteJobHandler {
SopStruct sop = sopService.loadSop(instId);
// 创建执行批次
SopInstBatch batch = sopService.createBatch(sop.getInst());
sop.setBatch(batch);
// 重置节点执行次数
sopService.resetNodeDailyCount(sop);
if (sop.getInst().getInstStatus() == 1) {
sopService.createBatch(sop);
}
// 创建所有节点任务
SopXxlJobUtil.initSopNodesJob(sop);
......
......@@ -114,6 +114,16 @@ 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);
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,节点dailyExpire已到期,停止执行!", sop.getInst().getInstId(), sop.getInst().getInstName());
return;
}
// 没超执行次数限制
if (node.getDailyLimit() == 0 || node.getDailyCount() < node.getDailyLimit()) {
// 预备执行参数
......@@ -157,6 +167,8 @@ public class SopInstNodeExecuteJobHandler {
inst.setInstSnapshot(snapshot);
node.setDailyCount(ObjectUtil.defaultIfNull(node.getDailyCount(), 0) + 1);
SopXxlJobUtil.log(log, "【执行Sop节点】【{}:{}】,节点已执行次数:{}!", sop.getInst().getInstId(), sop.getInst().getInstName(), node.getDailyCount());
// 刷新实例批次快照
SopInstBatch batch = sop.getBatch();
if (batch != null) {
......
......@@ -2,6 +2,11 @@ package cn.com.glsx.process.modules.api;
import javax.annotation.Resource;
import cn.com.glsx.cdp.api.customer.CustomerSopJourneyFeignClient;
import cn.com.glsx.cdp.modules.model.customer.CustomerSopJourneyDTO;
import cn.com.glsx.cdp.modules.model.customer.CustomerSopJourneySearch;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
......@@ -39,13 +44,19 @@ public class UserApiController implements UserCenterFeignClient {
@Resource
private EchoCenterFeignClient echoCenterFeignClient;
@Resource
private CustomerSopJourneyFeignClient customerSopJourneyFeignClient;
@Operation(summary = "FeignClient调用")
@Override
@GetMapping("/echo")
public String echo(@RequestParam String message) {
String echo = echoCenterFeignClient.echo(message);
return echo;
CustomerSopJourneySearch customerSopJourneySearch = new CustomerSopJourneySearch();
customerSopJourneySearch.setPageNum(1);
customerSopJourneySearch.setPageSize(10);
Result<PageInfo<CustomerSopJourneyDTO>> pageInfoResult = customerSopJourneyFeignClient.pageCustomerSopJourney(customerSopJourneySearch);
return JSONObject.toJSONString(pageInfoResult);
}
@Operation(summary = "本地调用")
......
......@@ -19,6 +19,7 @@ import cn.com.glsx.process.modules.rabbitmq.RabbitConfig;
import cn.com.glsx.process.modules.rabbitmq.RabbitMQConstants;
import cn.com.glsx.process.util.FileUploadServerData;
import cn.com.glsx.process.util.FileUploadUtil;
import cn.com.glsx.process.util.SopTplParamHandler;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.glsx.plat.core.enums.SysConstants;
......@@ -306,6 +307,8 @@ public class SopInstService {
sopInst.setInstName(String.format("%s-%s", sopTplGrant.getGroupName(), sopTplDTO.getTplName()));
sopInst.setInstId(tplId + grantId);
sopInst.setGrantId(grantId);
// 处理模板定义的初始化参数
sopInst.setInstParam(SopTplParamHandler.addMerchantId(SopTplParamHandler.extract(sopTplDTO.getTplParamDef()), sopTplGrant.getGroupId()) );
sopInst.setCreateTime(date);
sopInst.setUpdateTime(date);
sopInst.setInstStatus(SopInstStatusEnum.UNUSED.getValue());
......@@ -328,6 +331,9 @@ public class SopInstService {
sopInstNode.setInstId(instId);
sopInstNode.setNodeStatus(SopInstNode.NODE_STATUS_NORMAL);
// 处理模板节点定义的初始化参数
sopInstNode.setNodeParam(SopTplParamHandler.extract(sopTplNode.getNodeParamDef()));
//页面选择是否跳过处理
SopInstNode sopInstNodeTemp = customNodesMap.get(sopInstNode.getNodeId());
sopInstNode.setNodeSkip(!Objects.isNull(sopInstNodeTemp) ? sopInstNodeTemp.getNodeSkip() : SopInstNode.NODE_SKIP_FALSE);
......
......@@ -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());
......@@ -176,6 +179,24 @@ public class SopService {
instMapper.updateInstBatchNo(sopInst.getInstId(), batch.getBatchNo(), batch.getSnapshot());
// 重置节点执行次数
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;
}
......@@ -202,12 +223,13 @@ public class SopService {
instBatchMapper.updateByPrimaryKey(batch);
}
instMapper.updateByPrimaryKey(sopInst); // 更新快照
instNodeMapper.updateByPrimaryKey(node); // 更新执行次数
// 过期机制
if (sopInst.getExpireTime() != null && sopInst.getExpireTime().before(new Date())) {
sopInst.setInstStatus((byte) -1);
}
public void resetNodeDailyCount(SopStruct sop) {
instNodeMapper.resetInstNodeDailyCount(sop.getInst().getInstId());
instMapper.updateByPrimaryKey(sopInst); // 更新快照
instNodeMapper.updateByPrimaryKey(node); // 更新执行次数
}
/** 从实例倒推出模板,主要用于开发阶段 */
......
package cn.com.glsx.process.util;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.glsx.plat.exception.ResultException;
import com.glsx.plat.exception.SystemMessage;
import lombok.experimental.UtilityClass;
import org.springframework.util.StringUtils;
import java.util.HashMap;
import java.util.Map;
@UtilityClass
public class SopTplParamHandler {
public ObjectMapper objectMapper = new ObjectMapper();
public String extract(String tplParam) {
if (!StringUtils.hasLength(tplParam)) {
return null;
}
try {
JsonNode rootNode = objectMapper.readTree(tplParam);
return processNode(rootNode);
} catch (Exception e) {
throw ResultException.create(SystemMessage.DATA_HANDLE_ERROR.getCode(), "参数转json对象异常");
}
}
public String addMerchantId(String paramJson, String merchantId) {
if (!StringUtils.hasLength(paramJson)) {
return null;
}
HashMap map = JSON.parseObject(paramJson, HashMap.class);
map.put("merchantId", merchantId);
return JSON.toJSONString(map);
}
private String processNode(JsonNode node) {
Map<String, Object> map = new HashMap<>();
for (JsonNode childNode : node) {
String key = childNode.path("paramKey").asText();
String valueType = childNode.path("valueType").asText();
JsonNode defaultValueNode = childNode.path("defaultValue");
switch (valueType) {
case "Number":
int defaultValueInt = defaultValueNode.asInt();
map.put(key, defaultValueInt);
break;
case "String":
String defaultValueString = defaultValueNode.asText();
map.put(key, defaultValueString);
break;
case "Map":
// 解析Map类型的默认值
Map<String, Object> defaultValueMap = objectMapper.convertValue(defaultValueNode, HashMap.class);
map.put(key, defaultValueMap);
break;
// 其他数据类型的解析
default:
break;
}
}
return JSON.toJSONString(map);
}
}
......@@ -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">
......
......@@ -7,9 +7,14 @@ import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import cn.com.glsx.cdp.api.customer.CustomerSopJourneyFeignClient;
import cn.com.glsx.cdp.modules.model.customer.CustomerSopJourneyDTO;
import cn.com.glsx.cdp.modules.model.customer.CustomerSopJourneySearch;
import cn.com.glsx.process.modules.service.SopInstLinkService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import com.glsx.plat.core.web.Result;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -32,6 +37,8 @@ import cn.com.glsx.process.modules.sop.execute.plugin.dbconn.SopBizAdapter;
import cn.com.glsx.process.modules.sop.execute.plugin.java.DynamicEngine;
import cn.com.glsx.process.modules.sop.java.example.ControllerHttpDemo;
import javax.annotation.Resource;
/**
* 测试spring cloud 服务调用方式
*/
......@@ -51,6 +58,9 @@ public class TestApplication {
@Autowired
private SopInstLinkService sopInstLinkService;
@Resource
private CustomerSopJourneyFeignClient customerSopJourneyFeignClient;
@Test
public void test() throws SQLException {
SopDataSource byId = sopDataSourceService.findByDataSourceId("SOP_CDP");
......@@ -149,5 +159,13 @@ public class TestApplication {
String listString = JSON.toJSONString(xblc7, SerializerFeature.DisableCircularReferenceDetect);
System.out.println(listString);
}
@Test
public void pageCustomerSopJourney() {
CustomerSopJourneySearch customerSopJourneySearch=new CustomerSopJourneySearch();
customerSopJourneySearch.setPageNum(1);
customerSopJourneySearch.setPageSize(10);
Result<PageInfo<CustomerSopJourneyDTO>> pageInfoResult = customerSopJourneyFeignClient.pageCustomerSopJourney(customerSopJourneySearch);
System.out.println(pageInfoResult);
}
}
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!