|
|
@@ -31,87 +31,79 @@ public class XxlJobTrigger {
|
|
31
|
31
|
*/
|
|
32
|
32
|
public static void trigger(int jobId) {
|
|
33
|
33
|
|
|
34
|
|
- // load job
|
|
35
|
|
- XxlJobInfo jobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(jobId);
|
|
|
34
|
+ // load data
|
|
|
35
|
+ XxlJobInfo jobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(jobId); // job info
|
|
|
36
|
+ XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup()); // group info
|
|
36
|
37
|
|
|
37
|
|
- // log part-1
|
|
|
38
|
+ ExecutorBlockStrategyEnum blockStrategy = ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorBlockStrategyEnum.SERIAL_EXECUTION); // block strategy
|
|
|
39
|
+ ExecutorFailStrategyEnum failStrategy = ExecutorFailStrategyEnum.match(jobInfo.getExecutorFailStrategy(), ExecutorFailStrategyEnum.FAIL_ALARM); // fail strategy
|
|
|
40
|
+ ExecutorRouteStrategyEnum executorRouteStrategyEnum = ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null); // route strategy
|
|
|
41
|
+ ArrayList<String> addressList = (ArrayList<String>) group.getRegistryList();
|
|
|
42
|
+
|
|
|
43
|
+ // 1、save log-id
|
|
38
|
44
|
XxlJobLog jobLog = new XxlJobLog();
|
|
39
|
45
|
jobLog.setJobGroup(jobInfo.getJobGroup());
|
|
40
|
46
|
jobLog.setJobId(jobInfo.getId());
|
|
41
|
47
|
XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
|
|
42
|
48
|
logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
|
|
43
|
49
|
|
|
44
|
|
- // log part-2 param
|
|
|
50
|
+ // 2、prepare trigger-info
|
|
45
|
51
|
//jobLog.setExecutorAddress(executorAddress);
|
|
46
|
52
|
jobLog.setGlueType(jobInfo.getGlueType());
|
|
47
|
53
|
jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
48
|
54
|
jobLog.setExecutorParam(jobInfo.getExecutorParam());
|
|
49
|
55
|
jobLog.setTriggerTime(new Date());
|
|
50
|
56
|
|
|
51
|
|
- // trigger request
|
|
52
|
|
- TriggerParam triggerParam = new TriggerParam();
|
|
53
|
|
- triggerParam.setJobId(jobInfo.getId());
|
|
54
|
|
- triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
55
|
|
- triggerParam.setExecutorParams(jobInfo.getExecutorParam());
|
|
56
|
|
- triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
|
|
57
|
|
- triggerParam.setGlueType(jobInfo.getGlueType());
|
|
58
|
|
- triggerParam.setGlueSource(jobInfo.getGlueSource());
|
|
59
|
|
- triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
|
|
60
|
|
- triggerParam.setLogId(jobLog.getId());
|
|
61
|
|
- triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
|
|
62
|
|
-
|
|
63
|
|
- // do trigger
|
|
64
|
|
- ReturnT<String> triggerResult = doTrigger(triggerParam, jobInfo, jobLog);
|
|
65
|
|
-
|
|
66
|
|
- // fail retry
|
|
67
|
|
- if (triggerResult.getCode()==ReturnT.FAIL_CODE &&
|
|
68
|
|
- ExecutorFailStrategyEnum.match(jobInfo.getExecutorFailStrategy(), null) == ExecutorFailStrategyEnum.FAIL_RETRY) {
|
|
69
|
|
- ReturnT<String> retryTriggerResult = doTrigger(triggerParam, jobInfo, jobLog);
|
|
70
|
|
-
|
|
71
|
|
- triggerResult.setCode(retryTriggerResult.getCode());
|
|
72
|
|
- triggerResult.setMsg(triggerResult.getMsg() + "<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>失败重试<<<<<<<<<<< </span><br><br>" +retryTriggerResult.getMsg());
|
|
|
57
|
+ ReturnT<String> triggerResult = new ReturnT<String>(null);
|
|
|
58
|
+ StringBuffer triggerMsgSb = new StringBuffer();
|
|
|
59
|
+ triggerMsgSb.append("注册方式:").append( (group.getAddressType() == 0)?"自动注册":"手动录入" );
|
|
|
60
|
+ triggerMsgSb.append("<br>阻塞处理策略:").append(blockStrategy.getTitle());
|
|
|
61
|
+ triggerMsgSb.append("<br>失败处理策略:").append(failStrategy.getTitle());
|
|
|
62
|
+ triggerMsgSb.append("<br>地址列表:").append(group.getRegistryList());
|
|
|
63
|
+ triggerMsgSb.append("<br>路由策略:").append(executorRouteStrategyEnum.getTitle());
|
|
|
64
|
+
|
|
|
65
|
+ // 3、trigger-valid
|
|
|
66
|
+ if (triggerResult.getCode()==ReturnT.SUCCESS_CODE && CollectionUtils.isEmpty(addressList)) {
|
|
|
67
|
+ triggerResult.setCode(ReturnT.FAIL_CODE);
|
|
|
68
|
+ triggerMsgSb.append("<br>----------------------<br>").append("调度失败:").append("执行器地址为空");
|
|
|
69
|
+ }
|
|
|
70
|
+ if (triggerResult.getCode() == ReturnT.SUCCESS_CODE && executorRouteStrategyEnum == null) {
|
|
|
71
|
+ triggerResult.setCode(ReturnT.FAIL_CODE);
|
|
|
72
|
+ triggerMsgSb.append("<br>----------------------<br>").append("调度失败:").append("执行器路由策略为空");
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ if (triggerResult.getCode() == ReturnT.SUCCESS_CODE) {
|
|
|
76
|
+ // 4.1、trigger-param
|
|
|
77
|
+ TriggerParam triggerParam = new TriggerParam();
|
|
|
78
|
+ triggerParam.setJobId(jobInfo.getId());
|
|
|
79
|
+ triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
|
80
|
+ triggerParam.setExecutorParams(jobInfo.getExecutorParam());
|
|
|
81
|
+ triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
|
|
|
82
|
+ triggerParam.setGlueType(jobInfo.getGlueType());
|
|
|
83
|
+ triggerParam.setGlueSource(jobInfo.getGlueSource());
|
|
|
84
|
+ triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
|
|
|
85
|
+ triggerParam.setLogId(jobLog.getId());
|
|
|
86
|
+ triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
|
|
|
87
|
+
|
|
|
88
|
+ // 4.2、trigger-run (route run / trigger remote executor)
|
|
|
89
|
+ triggerResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList, jobLog);
|
|
|
90
|
+ triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>触发调度<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
|
|
|
91
|
+
|
|
|
92
|
+ // 4.3、trigger (fail retry)
|
|
|
93
|
+ if (triggerResult.getCode()!=ReturnT.SUCCESS_CODE && failStrategy == ExecutorFailStrategyEnum.FAIL_RETRY) {
|
|
|
94
|
+ triggerResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList, jobLog);
|
|
|
95
|
+ triggerMsgSb.append("<br><br><span style=\"color:#F39C12;\" > >>>>>>>>>>>失败重试<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
|
|
|
96
|
+ }
|
|
73
|
97
|
}
|
|
74
|
98
|
|
|
75
|
|
- // log part-2
|
|
|
99
|
+ // 5、save trigger-info
|
|
76
|
100
|
jobLog.setTriggerCode(triggerResult.getCode());
|
|
77
|
|
- jobLog.setTriggerMsg(triggerResult.getMsg());
|
|
|
101
|
+ jobLog.setTriggerMsg(triggerMsgSb.toString());
|
|
78
|
102
|
XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
|
|
79
|
103
|
|
|
80
|
|
- // monitor triger
|
|
|
104
|
+ // 6、monitor triger
|
|
81
|
105
|
JobFailMonitorHelper.monitor(jobLog.getId());
|
|
82
|
106
|
logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
|
|
83
|
107
|
}
|
|
84
|
108
|
|
|
85
|
|
- private static ReturnT<String> doTrigger(TriggerParam triggerParam, XxlJobInfo jobInfo, XxlJobLog jobLog){
|
|
86
|
|
- StringBuffer triggerSb = new StringBuffer();
|
|
87
|
|
-
|
|
88
|
|
- // exerutor address list
|
|
89
|
|
- XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup());
|
|
90
|
|
- triggerSb.append( (group.getAddressType() == 0)?"注册方式:自动注册":"注册方式:手动录入" );
|
|
91
|
|
- ArrayList<String> addressList = (ArrayList<String>) group.getRegistryList();
|
|
92
|
|
-
|
|
93
|
|
- triggerSb.append("<br>阻塞处理策略:").append(ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorBlockStrategyEnum.SERIAL_EXECUTION).getTitle());
|
|
94
|
|
- triggerSb.append("<br>失败处理策略:").append(ExecutorFailStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorFailStrategyEnum.FAIL_ALARM).getTitle());
|
|
95
|
|
- triggerSb.append("<br>地址列表:").append(addressList!=null?addressList.toString():"");
|
|
96
|
|
- if (CollectionUtils.isEmpty(addressList)) {
|
|
97
|
|
- triggerSb.append("<br>----------------------<br>").append("调度失败:").append("执行器地址为空");
|
|
98
|
|
- return new ReturnT<String>(ReturnT.FAIL_CODE, triggerSb.toString());
|
|
99
|
|
- }
|
|
100
|
|
-
|
|
101
|
|
- // executor route strategy
|
|
102
|
|
- ExecutorRouteStrategyEnum executorRouteStrategyEnum = ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null);
|
|
103
|
|
- if (executorRouteStrategyEnum == null) {
|
|
104
|
|
- triggerSb.append("<br>----------------------<br>").append("调度失败:").append("执行器路由策略为空");
|
|
105
|
|
- return new ReturnT<String>(ReturnT.FAIL_CODE, triggerSb.toString());
|
|
106
|
|
- }
|
|
107
|
|
- triggerSb.append("<br>路由策略:").append(executorRouteStrategyEnum.name() + "-" + executorRouteStrategyEnum.getTitle());
|
|
108
|
|
-
|
|
109
|
|
-
|
|
110
|
|
- // route run / trigger remote executor
|
|
111
|
|
- ReturnT<String> routeRunResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList, jobLog);
|
|
112
|
|
- triggerSb.append("<br>----------------------<br>").append(routeRunResult.getMsg());
|
|
113
|
|
- return new ReturnT<String>(routeRunResult.getCode(), triggerSb.toString());
|
|
114
|
|
-
|
|
115
|
|
- }
|
|
116
|
|
-
|
|
117
|
109
|
}
|