Commit f65ab167 by tiger

SopJob

1 parent 1d818b68
package cn.com.glsx.process.modules.model; package cn.com.glsx.process.modules.model;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.google.common.collect.Sets;
import cn.com.glsx.process.modules.entity.SopInst; import cn.com.glsx.process.modules.entity.SopInst;
import cn.com.glsx.process.modules.entity.SopInstBatch; import cn.com.glsx.process.modules.entity.SopInstBatch;
import cn.com.glsx.process.modules.entity.SopInstNode; import cn.com.glsx.process.modules.entity.SopInstNode;
...@@ -38,4 +42,40 @@ public class SopStruct { ...@@ -38,4 +42,40 @@ public class SopStruct {
/** 实例当前批次 */ /** 实例当前批次 */
private SopInstBatch batch; private SopInstBatch batch;
/** 所有节点执行顺序 */
public List<Integer> getNodeSort() {
List<Integer> sort = new ArrayList<>();
Set<Integer> keys = Sets.newHashSet();
int i = 0;
while (keys.size() < instLinkMap.keySet().size()) {
Set<Integer> sets = instLinkMap.keySet();
for (Integer key : sets) {
i++;
Pair<Set<Integer>, Set<Integer>> pair = instLinkMap.get(key);
if (!keys.contains(key)) {
// 第一个节点:直接执行
if (pair.getKey().isEmpty()) {
keys.add(key);
sort.add(key);
continue;
}
// 非第一个节点:判断前置节点
if (keys.containsAll(pair.getKey())) {
keys.add(key);
sort.add(key);
continue;
}
} else {
// 进入后置节点
sets = pair.getValue();
continue;
}
}
if (i++ > 1000) {
throw new RuntimeException("Sop实例:【" + inst.getInstId() + "-" + inst.getInstName() + "】节点不闭环,请检查!");
}
}
return sort;
}
} }
...@@ -8,17 +8,16 @@ import java.util.TimeZone; ...@@ -8,17 +8,16 @@ import java.util.TimeZone;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.glsx.plat.context.EnableRestAdmin; import com.glsx.plat.context.EnableRestAdmin;
import cn.com.glsx.process.job.DynamicTaskService; import cn.com.glsx.process.job.xxljob.impl.SopInstLoadJobHandler;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/** /**
* @author payu * @author payu
...@@ -26,25 +25,22 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy; ...@@ -26,25 +25,22 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableDubbo @EnableDubbo
@EnableRestAdmin @EnableRestAdmin
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableFeignClients(basePackages = {"cn.com.glsx.*.api"}) @EnableFeignClients(basePackages = { "cn.com.glsx.*.api" })
@SpringBootApplication @SpringBootApplication
@EnableAspectJAutoProxy @EnableAspectJAutoProxy
public class Application implements CommandLineRunner { public class Application implements CommandLineRunner {
@Autowired public static void main(String[] args) {
private DynamicTaskService dynamicTaskAdd; SpringApplication.run(Application.class, args);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@PostConstruct @PostConstruct
void started() { void started() {
TimeZone.setDefault(getTimeZone(of("Asia/Shanghai"))); TimeZone.setDefault(getTimeZone(of("Asia/Shanghai")));
} }
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
/* dynamicTaskAdd.scanAddJob();*/ SopInstLoadJobHandler.initLoadJob();
} }
} }
package cn.com.glsx.process.job.xxljob.core;
import cn.com.glsx.process.modules.entity.SopInst;
import cn.com.glsx.process.modules.entity.SopInstNode;
import cn.com.glsx.process.modules.model.SopStruct;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SopXxlJobUtil {
public static final String SOP_LOAD_JOB_ID = "sopInstLoadJobHandler";
public static final String SOP_EXECUTE_JOB_ID = "sopInstExecuteJobHandler";
public static void pauseSop(SopStruct sop) {
String jobBusId = SopStruct.jobBusId(sop.getInst());
String pause = XxlJobUtil.pause(jobBusId);
log.info("【{}】Sop任务停止:{}", jobBusId, pause);
if (sop != null) {
// 遍历node,停止任务
for (SopInstNode node : sop.getInstNodeMap().values()) {
jobBusId = SopStruct.jobBusId(sop.getInst(), node);
pause = XxlJobUtil.pause(jobBusId);
log.info("【{}】Sop节点任务停止:{}", jobBusId, pause);
}
}
}
public static void initLoadJob() {
XxlJobInfo job = XxlJobUtil.findByBusKey(SOP_LOAD_JOB_ID);
if (job != null) {
if (job.getTriggerStatus() == 1) {
return;
} else {
XxlJobUtil.remove(SOP_LOAD_JOB_ID);
}
}
job = new XxlJobInfo();
job.setExecutorParam("");
job.setJobDesc("加载所有SOP实例任务");
job.setAuthor("admin");
job.setScheduleConf("10 */5 * * * ?");
job.setExecutorHandler(SOP_LOAD_JOB_ID);
job.setBusKey(SOP_LOAD_JOB_ID);
job.setTriggerStatus(1);
String add = XxlJobUtil.add(job);
log.info("【{}】任务启动:{}", SOP_LOAD_JOB_ID, add);
}
public static void initSopJob(SopInst inst) {
String jobBusId = SopStruct.jobBusId(inst);
XxlJobInfo job = XxlJobUtil.findByBusKey(jobBusId);
if (job != null) {
XxlJobUtil.remove(jobBusId);
}
// 重建job
job = new XxlJobInfo();
job.setExecutorParam(inst.getInstId());
job.setJobDesc(inst.getInstName());
job.setAuthor("admin");
job.setScheduleConf(inst.getInstCron());
job.setExecutorHandler(SopXxlJobUtil.SOP_EXECUTE_JOB_ID);
job.setBusKey(jobBusId);
job.setTriggerStatus(1);
String add = XxlJobUtil.add(job);
log.info("【{}】Sop任务启动:{}", inst.getInstId(), add);
}
}
package cn.com.glsx.process.job.xxljob.core; package cn.com.glsx.process.job.xxljob.core;
import java.io.IOException;
import java.net.HttpCookie;
import java.util.List;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -9,6 +13,8 @@ import com.alibaba.fastjson.JSONObject; ...@@ -9,6 +13,8 @@ import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.XxlJobRemotingUtil; import com.xxl.job.core.util.XxlJobRemotingUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@Component @Component
...@@ -19,6 +25,7 @@ public class XxlJobUtil { ...@@ -19,6 +25,7 @@ public class XxlJobUtil {
private static String appname; private static String appname;
private static Integer groupId; private static Integer groupId;
private static String accessToken; private static String accessToken;
private static String cookie = "";
@Value("${xxl.job.admin.addresses}") @Value("${xxl.job.admin.addresses}")
public void setAdminAddresses(String adminAddresses) { public void setAdminAddresses(String adminAddresses) {
...@@ -166,7 +173,7 @@ public class XxlJobUtil { ...@@ -166,7 +173,7 @@ public class XxlJobUtil {
} }
public static <T> T doPost(String url, Object json, Class<T> clazz) { public static <T> T doPost(String url, Object json, Class<T> clazz) {
log.info("请求url{},请求参数{} accessToken:{}", url, JSONObject.toJSONString(json), accessToken); log.info("请求url{},请求参数{} {}:{}", url, JSONObject.toJSONString(json), XxlJobRemotingUtil.XXL_JOB_ACCESS_TOKEN, accessToken);
ReturnT<T> returnT = XxlJobRemotingUtil.postBody(url, accessToken, 30, json, clazz); ReturnT<T> returnT = XxlJobRemotingUtil.postBody(url, accessToken, 30, json, clazz);
if (returnT.getCode() != SUCCESS_CODE) { if (returnT.getCode() != SUCCESS_CODE) {
log.info(String.format("【xxl-job】接口调用失败,错误码:%s,原因:%s", returnT.getCode(), returnT.getMsg())); log.info(String.format("【xxl-job】接口调用失败,错误码:%s,原因:%s", returnT.getCode(), returnT.getMsg()));
...@@ -174,4 +181,25 @@ public class XxlJobUtil { ...@@ -174,4 +181,25 @@ public class XxlJobUtil {
return returnT.getContent(); return returnT.getContent();
} }
/**
* 登录
*
* @param url
* @param userName
* @param password
* @return
* @throws HttpException
* @throws IOException
*/
public static String login(String userName, String password) {
HttpResponse rsp = HttpUtil.createGet(adminAddresses + "/api/jobinfo/login?userName=" + userName + "&password=" + password).execute();
List<HttpCookie> cookies = rsp.getCookies();
StringBuffer tmpcookies = new StringBuffer();
for (HttpCookie c : cookies) {
tmpcookies.append(c.toString() + ";");
}
cookie = tmpcookies.toString();
return cookie;
}
} }
package cn.com.glsx.process.job.xxljob.impl;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
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.model.SopStruct;
import cn.com.glsx.process.modules.service.SopService;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Component
public class SopInstExecuteJobHandler {
static final String JOB_ID = "sopInstExecuteJobHandler";
@Resource
private SopService sopService;
@XxlJob(SopXxlJobUtil.SOP_EXECUTE_JOB_ID)
public void handler(String param) throws Exception {
param = XxlJobHelper.getJobParam();
log.info("【执行Sop实例】{}", param);
String instId = param;
SopStruct sop = sopService.loadSop(instId);
// 创建所有节点任务
initSopNodeJob(sop);
}
}
package cn.com.glsx.process.job.xxljob.impl; package cn.com.glsx.process.job.xxljob.impl;
import java.util.Date;
import java.util.List; import java.util.List;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
import com.xxl.job.core.glue.GlueTypeEnum;
import com.xxl.job.core.handler.annotation.XxlJob; import com.xxl.job.core.handler.annotation.XxlJob;
import cn.com.glsx.process.job.xxljob.core.XxlJobInfo; import cn.com.glsx.process.job.xxljob.core.SopXxlJobUtil;
import cn.com.glsx.process.job.xxljob.core.XxlJobUtil;
import cn.com.glsx.process.modules.entity.SopInst; import cn.com.glsx.process.modules.entity.SopInst;
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;
...@@ -21,12 +18,10 @@ import lombok.extern.slf4j.Slf4j; ...@@ -21,12 +18,10 @@ import lombok.extern.slf4j.Slf4j;
@Component @Component
public class SopInstLoadJobHandler { public class SopInstLoadJobHandler {
static final String JOB_ID = "sopInstLoadJobHandler";
@Resource @Resource
private SopService sopService; private SopService sopService;
@XxlJob(JOB_ID) @XxlJob(SopXxlJobUtil.SOP_LOAD_JOB_ID)
public void handler(String param) throws Exception { public void handler(String param) throws Exception {
log.info("【加载Sop实例列表】{}", param); log.info("【加载Sop实例列表】{}", param);
...@@ -34,67 +29,16 @@ public class SopInstLoadJobHandler { ...@@ -34,67 +29,16 @@ public class SopInstLoadJobHandler {
for (SopInst inst : all) { for (SopInst inst : all) {
if (inst.getInstStatus() == 0) { if (inst.getInstStatus() == 0) {
String pause = XxlJobUtil.pause(inst.getInstId()); // 停止实例任务以及所有子任务
log.info("【{}】任务停止:{}", inst.getInstId(), pause);
SopStruct sop = sopService.loadSop(inst.getInstId());
SopXxlJobUtil.pauseSop(sop);
continue; continue;
} }
// 检查实例是否已加载到任务列表 // 启动sop任务
XxlJobInfo job = XxlJobUtil.findByBusKey(inst.getInstId()); SopXxlJobUtil.initSopJob(inst);
if (job == null || job.getTriggerStatus() == 0) {
job = new XxlJobInfo();
job.setAuthor("admin");
job.setAddTime(new Date());
job.setBusKey(inst.getInstId());
job.setExecutorBlockStrategy(param);
job.setExecutorFailRetryCount(1);
job.setExecutorHandler("");
job.setExecutorParam("");
job.setExecutorRouteStrategy(param);
job.setExecutorTimeout(0);
job.setGlueRemark(param);
job.setGlueSource(param);
job.setGlueType(GlueTypeEnum.BEAN.getCmd());
job.setGlueUpdatetime(new Date());
job.setJobDesc(param);
job.setJobGroup(0);
job.setMisfireStrategy(param);
job.setScheduleConf(param);
job.setScheduleType(param);
job.setTriggerStatus(0);
job.setUpdateTime(null);
String add = XxlJobUtil.add(job);
log.info("【{}】任务启动:{}", inst.getInstId(), add);
}
} }
} }
public static void initJob() {
XxlJobInfo job = new XxlJobInfo();
job.setAddTime(new Date());
job.setAuthor("admin");
job.setBusKey(JOB_ID);
job.setExecutorBlockStrategy(ExecutorBlockStrategyEnum.COVER_EARLY.name());
job.setExecutorFailRetryCount(1);
job.setExecutorHandler(JOB_ID);
job.setExecutorParam("");
job.setExecutorRouteStrategy("FIRST");
job.setExecutorTimeout(0);
job.setGlueRemark("java");
job.setGlueSource("");
job.setGlueType(GlueTypeEnum.BEAN.getCmd());
job.setGlueUpdatetime(new Date());
job.setJobDesc("加载所有SOP实例任务");
job.setJobGroup(1);
job.setMisfireStrategy("DO_NOTHING");
job.setScheduleConf("10 */5 * * * ?"); // 每5分钟加载一次
job.setScheduleType("CRON");
job.setTriggerStatus(1);
job.setUpdateTime(new Date());
String add = XxlJobUtil.add(job);
log.info("【{}】任务启动:{}", JOB_ID, add);
}
} }
...@@ -12,7 +12,6 @@ import com.glsx.plat.core.web.R; ...@@ -12,7 +12,6 @@ import com.glsx.plat.core.web.R;
import cn.com.glsx.process.job.xxljob.core.XxlJobInfo; import cn.com.glsx.process.job.xxljob.core.XxlJobInfo;
import cn.com.glsx.process.job.xxljob.core.XxlJobUtil; import cn.com.glsx.process.job.xxljob.core.XxlJobUtil;
import cn.com.glsx.process.job.xxljob.impl.SopInstLoadJobHandler;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!