|
|
@@ -18,6 +18,7 @@ import org.slf4j.LoggerFactory;
|
|
18
|
18
|
|
|
19
|
19
|
import java.util.ArrayList;
|
|
20
|
20
|
import java.util.Date;
|
|
|
21
|
+import java.util.List;
|
|
21
|
22
|
|
|
22
|
23
|
/**
|
|
23
|
24
|
* xxl-job trigger
|
|
|
@@ -36,165 +37,116 @@ public class XxlJobTrigger {
|
|
36
|
37
|
*
|
|
37
|
38
|
*/
|
|
38
|
39
|
public static void trigger(int jobId, int failRetryCount, TriggerTypeEnum triggerType) {
|
|
39
|
|
-
|
|
40
|
40
|
// load data
|
|
41
|
41
|
XxlJobInfo jobInfo = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(jobId); // job info
|
|
42
|
42
|
if (jobInfo == null) {
|
|
43
|
43
|
logger.warn(">>>>>>>>>>>> trigger fail, jobId invalid,jobId={}", jobId);
|
|
44
|
44
|
return;
|
|
45
|
45
|
}
|
|
46
|
|
- int finalFailRetryCount = jobInfo.getExecutorFailRetryCount();
|
|
47
|
|
- if (failRetryCount >= 0) {
|
|
48
|
|
- finalFailRetryCount = failRetryCount;
|
|
|
46
|
+ int finalFailRetryCount = failRetryCount>=0?failRetryCount:jobInfo.getExecutorFailRetryCount();
|
|
|
47
|
+ XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup()); // group info
|
|
|
48
|
+
|
|
|
49
|
+ // process trigger
|
|
|
50
|
+ if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) && CollectionUtils.isNotEmpty(group.getRegistryList())) {
|
|
|
51
|
+ for (int i = 0; i < group.getRegistryList().size(); i++) {
|
|
|
52
|
+ processTrigger(group, jobInfo, finalFailRetryCount, triggerType, i);
|
|
|
53
|
+ }
|
|
|
54
|
+ } else {
|
|
|
55
|
+ processTrigger(group, jobInfo, finalFailRetryCount, triggerType, 0);
|
|
49
|
56
|
}
|
|
|
57
|
+ }
|
|
50
|
58
|
|
|
51
|
|
- XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(jobInfo.getJobGroup()); // group info
|
|
|
59
|
+ private static void processTrigger(XxlJobGroup group, XxlJobInfo jobInfo, int finalFailRetryCount, TriggerTypeEnum triggerType, int index){
|
|
52
|
60
|
|
|
|
61
|
+ // param
|
|
53
|
62
|
ExecutorBlockStrategyEnum blockStrategy = ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), ExecutorBlockStrategyEnum.SERIAL_EXECUTION); // block strategy
|
|
54
|
63
|
ExecutorRouteStrategyEnum executorRouteStrategyEnum = ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null); // route strategy
|
|
55
|
|
- ArrayList<String> addressList = (ArrayList<String>) group.getRegistryList();
|
|
56
|
|
-
|
|
57
|
|
- // broadcast
|
|
58
|
|
- if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == executorRouteStrategyEnum && CollectionUtils.isNotEmpty(addressList)) {
|
|
59
|
|
- for (int i = 0; i < addressList.size(); i++) {
|
|
60
|
|
- String address = addressList.get(i);
|
|
61
|
|
-
|
|
62
|
|
- // 1、save log-id
|
|
63
|
|
- XxlJobLog jobLog = new XxlJobLog();
|
|
64
|
|
- jobLog.setJobGroup(jobInfo.getJobGroup());
|
|
65
|
|
- jobLog.setJobId(jobInfo.getId());
|
|
66
|
|
- XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
|
|
67
|
|
- logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
|
|
68
|
|
-
|
|
69
|
|
- // 2、prepare trigger-info
|
|
70
|
|
- //jobLog.setExecutorAddress(executorAddress);
|
|
71
|
|
- jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
72
|
|
- jobLog.setExecutorParam(jobInfo.getExecutorParam());
|
|
73
|
|
- jobLog.setExecutorFailRetryCount(finalFailRetryCount);
|
|
74
|
|
- jobLog.setTriggerTime(new Date());
|
|
75
|
|
-
|
|
76
|
|
- ReturnT<String> triggerResult = new ReturnT<String>(null);
|
|
77
|
|
- StringBuffer triggerMsgSb = new StringBuffer();
|
|
78
|
|
- triggerMsgSb.append(I18nUtil.getString("jobconf_trigger_type")).append(":").append(triggerType.getTitle());
|
|
79
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_admin_adress")).append(":").append(IpUtil.getIp());
|
|
80
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regtype")).append(":")
|
|
81
|
|
- .append( (group.getAddressType() == 0)?I18nUtil.getString("jobgroup_field_addressType_0"):I18nUtil.getString("jobgroup_field_addressType_1") );
|
|
82
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regaddress")).append(":").append(group.getRegistryList());
|
|
83
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorRouteStrategy")).append(":").append(executorRouteStrategyEnum.getTitle()).append("("+i+"/"+addressList.size()+")"); // update01
|
|
84
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorBlockStrategy")).append(":").append(blockStrategy.getTitle());
|
|
85
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_timeout")).append(":").append(jobInfo.getExecutorTimeout());
|
|
86
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorFailRetryCount")).append(":").append(finalFailRetryCount);
|
|
87
|
|
-
|
|
88
|
|
-
|
|
89
|
|
- // 3.1、trigger-param
|
|
90
|
|
- TriggerParam triggerParam = new TriggerParam();
|
|
91
|
|
- triggerParam.setJobId(jobInfo.getId());
|
|
92
|
|
- triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
93
|
|
- triggerParam.setExecutorParams(jobInfo.getExecutorParam());
|
|
94
|
|
- triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
|
|
95
|
|
- triggerParam.setExecutorTimeout(jobInfo.getExecutorTimeout());
|
|
96
|
|
- triggerParam.setLogId(jobLog.getId());
|
|
97
|
|
- triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
|
|
98
|
|
- triggerParam.setGlueType(jobInfo.getGlueType());
|
|
99
|
|
- triggerParam.setGlueSource(jobInfo.getGlueSource());
|
|
100
|
|
- triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
|
|
101
|
|
- triggerParam.setBroadcastIndex(i);
|
|
102
|
|
- triggerParam.setBroadcastTotal(addressList.size()); // update02
|
|
103
|
|
-
|
|
104
|
|
- // 3.2、trigger-run (route run / trigger remote executor)
|
|
105
|
|
- triggerResult = runExecutor(triggerParam, address); // update03
|
|
106
|
|
- triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_run") +"<<<<<<<<<<< </span><br>").append(triggerResult.getMsg());
|
|
107
|
|
-
|
|
108
|
|
- // 4、save trigger-info
|
|
109
|
|
- jobLog.setExecutorAddress(triggerResult.getContent());
|
|
110
|
|
- jobLog.setTriggerCode(triggerResult.getCode());
|
|
111
|
|
- jobLog.setTriggerMsg(triggerMsgSb.toString());
|
|
112
|
|
- XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
|
|
113
|
|
-
|
|
114
|
|
- // 5、monitor trigger
|
|
115
|
|
- JobFailMonitorHelper.monitor(jobLog.getId());
|
|
116
|
|
- logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
|
|
117
|
|
-
|
|
118
|
|
- }
|
|
119
|
64
|
|
|
120
|
|
- } else {
|
|
121
|
|
- // 1、save log-id
|
|
122
|
|
- XxlJobLog jobLog = new XxlJobLog();
|
|
123
|
|
- jobLog.setJobGroup(jobInfo.getJobGroup());
|
|
124
|
|
- jobLog.setJobId(jobInfo.getId());
|
|
125
|
|
- XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
|
|
126
|
|
- logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
|
|
127
|
|
-
|
|
128
|
|
- // 2、prepare trigger-info
|
|
129
|
|
- //jobLog.setExecutorAddress(executorAddress);
|
|
130
|
|
- jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
131
|
|
- jobLog.setExecutorParam(jobInfo.getExecutorParam());
|
|
132
|
|
- jobLog.setExecutorFailRetryCount(finalFailRetryCount);
|
|
133
|
|
- jobLog.setTriggerTime(new Date());
|
|
134
|
|
-
|
|
135
|
|
- ReturnT<String> triggerResult = new ReturnT<String>(null);
|
|
136
|
|
- StringBuffer triggerMsgSb = new StringBuffer();
|
|
137
|
|
- triggerMsgSb.append(I18nUtil.getString("jobconf_trigger_type")).append(":").append(triggerType.getTitle());
|
|
138
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_admin_adress")).append(":").append(IpUtil.getIp());
|
|
139
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regtype")).append(":")
|
|
140
|
|
- .append( (group.getAddressType() == 0)?I18nUtil.getString("jobgroup_field_addressType_0"):I18nUtil.getString("jobgroup_field_addressType_1") );
|
|
141
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regaddress")).append(":").append(group.getRegistryList());
|
|
142
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorRouteStrategy")).append(":").append(executorRouteStrategyEnum.getTitle());
|
|
143
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorBlockStrategy")).append(":").append(blockStrategy.getTitle());
|
|
144
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_timeout")).append(":").append(jobInfo.getExecutorTimeout());
|
|
145
|
|
- triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorFailRetryCount")).append(":").append(finalFailRetryCount);
|
|
146
|
|
-
|
|
147
|
|
- // 3.0、trigger-valid
|
|
148
|
|
- String address = null;
|
|
149
|
|
- if (CollectionUtils.isEmpty(addressList)) {
|
|
150
|
|
- triggerResult.setCode(ReturnT.FAIL_CODE);
|
|
151
|
|
- triggerMsgSb.append("<br>----------------------<br>").append(I18nUtil.getString("jobconf_trigger_address_empty"));
|
|
|
65
|
+ // 1、save log-id
|
|
|
66
|
+ XxlJobLog jobLog = new XxlJobLog();
|
|
|
67
|
+ jobLog.setJobGroup(jobInfo.getJobGroup());
|
|
|
68
|
+ jobLog.setJobId(jobInfo.getId());
|
|
|
69
|
+ jobLog.setTriggerTime(new Date());
|
|
|
70
|
+ XxlJobDynamicScheduler.xxlJobLogDao.save(jobLog);
|
|
|
71
|
+ logger.debug(">>>>>>>>>>> xxl-job trigger start, jobId:{}", jobLog.getId());
|
|
|
72
|
+
|
|
|
73
|
+ // 2、init trigger-param
|
|
|
74
|
+ TriggerParam triggerParam = new TriggerParam();
|
|
|
75
|
+ triggerParam.setJobId(jobInfo.getId());
|
|
|
76
|
+ triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
|
77
|
+ triggerParam.setExecutorParams(jobInfo.getExecutorParam());
|
|
|
78
|
+ triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
|
|
|
79
|
+ triggerParam.setExecutorTimeout(jobInfo.getExecutorTimeout());
|
|
|
80
|
+ triggerParam.setLogId(jobLog.getId());
|
|
|
81
|
+ triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
|
|
|
82
|
+ triggerParam.setGlueType(jobInfo.getGlueType());
|
|
|
83
|
+ triggerParam.setGlueSource(jobInfo.getGlueSource());
|
|
|
84
|
+ triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
|
|
|
85
|
+ triggerParam.setBroadcastIndex(index);
|
|
|
86
|
+ triggerParam.setBroadcastTotal(group.getRegistryList()!=null?group.getRegistryList().size():0);
|
|
|
87
|
+
|
|
|
88
|
+ // 3、init address
|
|
|
89
|
+ String address = null;
|
|
|
90
|
+ ReturnT<String> routeAddressResult = null;
|
|
|
91
|
+ if (CollectionUtils.isNotEmpty(group.getRegistryList())) {
|
|
|
92
|
+ if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == executorRouteStrategyEnum) {
|
|
|
93
|
+ address = group.getRegistryList().get(index);
|
|
152
|
94
|
} else {
|
|
153
|
|
- // 3.1、trigger-param
|
|
154
|
|
- TriggerParam triggerParam = new TriggerParam();
|
|
155
|
|
- triggerParam.setJobId(jobInfo.getId());
|
|
156
|
|
- triggerParam.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
157
|
|
- triggerParam.setExecutorParams(jobInfo.getExecutorParam());
|
|
158
|
|
- triggerParam.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
|
|
159
|
|
- triggerParam.setExecutorTimeout(jobInfo.getExecutorTimeout());
|
|
160
|
|
- triggerParam.setLogId(jobLog.getId());
|
|
161
|
|
- triggerParam.setLogDateTim(jobLog.getTriggerTime().getTime());
|
|
162
|
|
- triggerParam.setGlueType(jobInfo.getGlueType());
|
|
163
|
|
- triggerParam.setGlueSource(jobInfo.getGlueSource());
|
|
164
|
|
- triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
|
|
165
|
|
- triggerParam.setBroadcastIndex(0);
|
|
166
|
|
- triggerParam.setBroadcastTotal(1);
|
|
167
|
|
-
|
|
168
|
|
- // 3.2、trigger-run (route run / trigger remote executor)
|
|
169
|
|
- //triggerResult = executorRouteStrategyEnum.getRouter().routeRun(triggerParam, addressList);
|
|
170
|
|
- ReturnT<String> routeAddressResult = executorRouteStrategyEnum.getRouter().route(triggerParam, addressList);
|
|
|
95
|
+ routeAddressResult = executorRouteStrategyEnum.getRouter().route(triggerParam, group.getRegistryList());
|
|
171
|
96
|
if (routeAddressResult.getCode() == ReturnT.SUCCESS_CODE) {
|
|
172
|
97
|
address = routeAddressResult.getContent();
|
|
173
|
|
- triggerResult = runExecutor(triggerParam, address);
|
|
174
|
98
|
}
|
|
175
|
|
- triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_run") +"<<<<<<<<<<< </span><br>")
|
|
176
|
|
- .append(routeAddressResult.getMsg()!=null?routeAddressResult.getMsg()+"<br><br>":"").append(triggerResult.getMsg()!=null?triggerResult.getMsg():"");
|
|
177
|
|
-
|
|
178
|
99
|
}
|
|
|
100
|
+ } else {
|
|
|
101
|
+ routeAddressResult = new ReturnT<String>(ReturnT.FAIL_CODE, "<br>----------------------<br>" + I18nUtil.getString("jobconf_trigger_address_empty"));
|
|
|
102
|
+ }
|
|
179
|
103
|
|
|
180
|
|
- // 4、save trigger-info
|
|
181
|
|
- jobLog.setExecutorAddress(address);
|
|
182
|
|
- jobLog.setTriggerCode(triggerResult.getCode());
|
|
183
|
|
- jobLog.setTriggerMsg(triggerMsgSb.toString());
|
|
184
|
|
- XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
|
|
185
|
|
-
|
|
186
|
|
- // 5、monitor trigger
|
|
187
|
|
- JobFailMonitorHelper.monitor(jobLog.getId());
|
|
188
|
|
- logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
|
|
|
104
|
+ // 4、trigger remote executor
|
|
|
105
|
+ ReturnT<String> triggerResult = null;
|
|
|
106
|
+ if (address != null) {
|
|
|
107
|
+ triggerResult = runExecutor(triggerParam, address);
|
|
|
108
|
+ } else {
|
|
|
109
|
+ triggerResult = new ReturnT<String>(ReturnT.FAIL_CODE, null);
|
|
189
|
110
|
}
|
|
190
|
111
|
|
|
|
112
|
+ // 5、collection trigger info
|
|
|
113
|
+ StringBuffer triggerMsgSb = new StringBuffer();
|
|
|
114
|
+ triggerMsgSb.append(I18nUtil.getString("jobconf_trigger_type")).append(":").append(triggerType.getTitle());
|
|
|
115
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_admin_adress")).append(":").append(IpUtil.getIp());
|
|
|
116
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regtype")).append(":")
|
|
|
117
|
+ .append( (group.getAddressType() == 0)?I18nUtil.getString("jobgroup_field_addressType_0"):I18nUtil.getString("jobgroup_field_addressType_1") );
|
|
|
118
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobconf_trigger_exe_regaddress")).append(":").append(group.getRegistryList());
|
|
|
119
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorRouteStrategy")).append(":").append(executorRouteStrategyEnum.getTitle());
|
|
|
120
|
+ if (ExecutorRouteStrategyEnum.SHARDING_BROADCAST == ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null)) {
|
|
|
121
|
+ triggerMsgSb.append("("+index+"/"+(group.getRegistryList()!=null?group.getRegistryList().size():0)+")");
|
|
|
122
|
+ }
|
|
|
123
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorBlockStrategy")).append(":").append(blockStrategy.getTitle());
|
|
|
124
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_timeout")).append(":").append(jobInfo.getExecutorTimeout());
|
|
|
125
|
+ triggerMsgSb.append("<br>").append(I18nUtil.getString("jobinfo_field_executorFailRetryCount")).append(":").append(finalFailRetryCount);
|
|
|
126
|
+
|
|
|
127
|
+ triggerMsgSb.append("<br><br><span style=\"color:#00c0ef;\" > >>>>>>>>>>>"+ I18nUtil.getString("jobconf_trigger_run") +"<<<<<<<<<<< </span><br>")
|
|
|
128
|
+ .append((routeAddressResult!=null&&routeAddressResult.getMsg()!=null)?routeAddressResult.getMsg()+"<br><br>":"").append(triggerResult.getMsg()!=null?triggerResult.getMsg():"");
|
|
|
129
|
+
|
|
|
130
|
+ // 6、save log trigger-info
|
|
|
131
|
+ jobLog.setExecutorAddress(address);
|
|
|
132
|
+ jobLog.setExecutorHandler(jobInfo.getExecutorHandler());
|
|
|
133
|
+ jobLog.setExecutorParam(jobInfo.getExecutorParam());
|
|
|
134
|
+ jobLog.setExecutorFailRetryCount(finalFailRetryCount);
|
|
|
135
|
+ //jobLog.setTriggerTime();
|
|
|
136
|
+ jobLog.setTriggerCode(triggerResult.getCode());
|
|
|
137
|
+ jobLog.setTriggerMsg(triggerMsgSb.toString());
|
|
|
138
|
+ XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
|
|
|
139
|
+
|
|
|
140
|
+ // 7、monitor trigger
|
|
|
141
|
+ JobFailMonitorHelper.monitor(jobLog.getId());
|
|
|
142
|
+ logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
|
|
191
|
143
|
}
|
|
192
|
144
|
|
|
193
|
145
|
/**
|
|
194
|
146
|
* run executor
|
|
195
|
147
|
* @param triggerParam
|
|
196
|
148
|
* @param address
|
|
197
|
|
- * @return ReturnT.content: final address
|
|
|
149
|
+ * @return
|
|
198
|
150
|
*/
|
|
199
|
151
|
public static ReturnT<String> runExecutor(TriggerParam triggerParam, String address){
|
|
200
|
152
|
ReturnT<String> runResult = null;
|
|
|
@@ -212,7 +164,6 @@ public class XxlJobTrigger {
|
|
212
|
164
|
runResultSB.append("<br>msg:").append(runResult.getMsg());
|
|
213
|
165
|
|
|
214
|
166
|
runResult.setMsg(runResultSB.toString());
|
|
215
|
|
- runResult.setContent(address);
|
|
216
|
167
|
return runResult;
|
|
217
|
168
|
}
|
|
218
|
169
|
|