提交 | 用户 | 时间
|
e7c126
|
1 |
package com.xxl.job.admin.controller; |
H |
2 |
|
|
3 |
import com.xxl.job.admin.core.complete.XxlJobCompleter; |
|
4 |
import com.xxl.job.admin.core.exception.XxlJobException; |
|
5 |
import com.xxl.job.admin.core.model.XxlJobGroup; |
|
6 |
import com.xxl.job.admin.core.model.XxlJobInfo; |
|
7 |
import com.xxl.job.admin.core.model.XxlJobLog; |
|
8 |
import com.xxl.job.admin.core.scheduler.XxlJobScheduler; |
|
9 |
import com.xxl.job.admin.core.util.I18nUtil; |
|
10 |
import com.xxl.job.admin.dao.XxlJobGroupDao; |
|
11 |
import com.xxl.job.admin.dao.XxlJobInfoDao; |
|
12 |
import com.xxl.job.admin.dao.XxlJobLogDao; |
|
13 |
import com.xxl.job.core.biz.ExecutorBiz; |
|
14 |
import com.xxl.job.core.biz.model.KillParam; |
|
15 |
import com.xxl.job.core.biz.model.LogParam; |
|
16 |
import com.xxl.job.core.biz.model.LogResult; |
|
17 |
import com.xxl.job.core.biz.model.ReturnT; |
|
18 |
import com.xxl.job.core.util.DateUtil; |
|
19 |
import org.slf4j.Logger; |
|
20 |
import org.slf4j.LoggerFactory; |
|
21 |
import org.springframework.stereotype.Controller; |
|
22 |
import org.springframework.ui.Model; |
|
23 |
import org.springframework.util.StringUtils; |
|
24 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
25 |
import org.springframework.web.bind.annotation.RequestParam; |
|
26 |
import org.springframework.web.bind.annotation.ResponseBody; |
|
27 |
import org.springframework.web.util.HtmlUtils; |
|
28 |
|
|
29 |
import javax.annotation.Resource; |
|
30 |
import javax.servlet.http.HttpServletRequest; |
|
31 |
import java.util.Date; |
|
32 |
import java.util.HashMap; |
|
33 |
import java.util.List; |
|
34 |
import java.util.Map; |
|
35 |
|
|
36 |
/** |
|
37 |
* index controller |
|
38 |
* @author xuxueli 2015-12-19 16:13:16 |
|
39 |
*/ |
|
40 |
@Controller |
|
41 |
@RequestMapping("/joblog") |
|
42 |
public class JobLogController { |
|
43 |
private static Logger logger = LoggerFactory.getLogger(JobLogController.class); |
|
44 |
|
|
45 |
@Resource |
|
46 |
private XxlJobGroupDao xxlJobGroupDao; |
|
47 |
@Resource |
|
48 |
public XxlJobInfoDao xxlJobInfoDao; |
|
49 |
@Resource |
|
50 |
public XxlJobLogDao xxlJobLogDao; |
|
51 |
|
|
52 |
@RequestMapping |
|
53 |
public String index(HttpServletRequest request, Model model, @RequestParam(required = false, defaultValue = "0") Integer jobId) { |
|
54 |
|
|
55 |
// 执行器列表 |
|
56 |
List<XxlJobGroup> jobGroupList_all = xxlJobGroupDao.findAll(); |
|
57 |
|
|
58 |
// filter group |
|
59 |
List<XxlJobGroup> jobGroupList = JobInfoController.filterJobGroupByRole(request, jobGroupList_all); |
|
60 |
if (jobGroupList==null || jobGroupList.size()==0) { |
|
61 |
throw new XxlJobException(I18nUtil.getString("jobgroup_empty")); |
|
62 |
} |
|
63 |
|
|
64 |
model.addAttribute("JobGroupList", jobGroupList); |
|
65 |
|
|
66 |
// 任务 |
|
67 |
if (jobId > 0) { |
|
68 |
XxlJobInfo jobInfo = xxlJobInfoDao.loadById(jobId); |
|
69 |
if (jobInfo == null) { |
|
70 |
throw new RuntimeException(I18nUtil.getString("jobinfo_field_id") + I18nUtil.getString("system_unvalid")); |
|
71 |
} |
|
72 |
|
|
73 |
model.addAttribute("jobInfo", jobInfo); |
|
74 |
|
|
75 |
// valid permission |
|
76 |
JobInfoController.validPermission(request, jobInfo.getJobGroup()); |
|
77 |
} |
|
78 |
|
|
79 |
return "joblog/joblog.index"; |
|
80 |
} |
|
81 |
|
|
82 |
@RequestMapping("/getJobsByGroup") |
|
83 |
@ResponseBody |
|
84 |
public ReturnT<List<XxlJobInfo>> getJobsByGroup(int jobGroup){ |
|
85 |
List<XxlJobInfo> list = xxlJobInfoDao.getJobsByGroup(jobGroup); |
|
86 |
return new ReturnT<List<XxlJobInfo>>(list); |
|
87 |
} |
|
88 |
|
|
89 |
@RequestMapping("/pageList") |
|
90 |
@ResponseBody |
|
91 |
public Map<String, Object> pageList(HttpServletRequest request, |
|
92 |
@RequestParam(required = false, defaultValue = "0") int start, |
|
93 |
@RequestParam(required = false, defaultValue = "10") int length, |
|
94 |
int jobGroup, int jobId, int logStatus, String filterTime) { |
|
95 |
|
|
96 |
// valid permission |
|
97 |
JobInfoController.validPermission(request, jobGroup); // 仅管理员支持查询全部;普通用户仅支持查询有权限的 jobGroup |
|
98 |
|
|
99 |
// parse param |
|
100 |
Date triggerTimeStart = null; |
|
101 |
Date triggerTimeEnd = null; |
|
102 |
if (filterTime!=null && filterTime.trim().length()>0) { |
|
103 |
String[] temp = filterTime.split(" - "); |
|
104 |
if (temp.length == 2) { |
|
105 |
triggerTimeStart = DateUtil.parseDateTime(temp[0]); |
|
106 |
triggerTimeEnd = DateUtil.parseDateTime(temp[1]); |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
// page query |
|
111 |
List<XxlJobLog> list = xxlJobLogDao.pageList(start, length, jobGroup, jobId, triggerTimeStart, triggerTimeEnd, logStatus); |
|
112 |
int list_count = xxlJobLogDao.pageListCount(start, length, jobGroup, jobId, triggerTimeStart, triggerTimeEnd, logStatus); |
|
113 |
|
|
114 |
// package result |
|
115 |
Map<String, Object> maps = new HashMap<String, Object>(); |
|
116 |
maps.put("recordsTotal", list_count); // 总记录数 |
|
117 |
maps.put("recordsFiltered", list_count); // 过滤后的总记录数 |
|
118 |
maps.put("data", list); // 分页列表 |
|
119 |
return maps; |
|
120 |
} |
|
121 |
|
|
122 |
@RequestMapping("/logDetailPage") |
|
123 |
public String logDetailPage(int id, Model model){ |
|
124 |
|
|
125 |
// base check |
|
126 |
ReturnT<String> logStatue = ReturnT.SUCCESS; |
|
127 |
XxlJobLog jobLog = xxlJobLogDao.load(id); |
|
128 |
if (jobLog == null) { |
|
129 |
throw new RuntimeException(I18nUtil.getString("joblog_logid_unvalid")); |
|
130 |
} |
|
131 |
|
|
132 |
model.addAttribute("triggerCode", jobLog.getTriggerCode()); |
|
133 |
model.addAttribute("handleCode", jobLog.getHandleCode()); |
|
134 |
model.addAttribute("logId", jobLog.getId()); |
|
135 |
return "joblog/joblog.detail"; |
|
136 |
} |
|
137 |
|
|
138 |
@RequestMapping("/logDetailCat") |
|
139 |
@ResponseBody |
|
140 |
public ReturnT<LogResult> logDetailCat(long logId, int fromLineNum){ |
|
141 |
try { |
|
142 |
// valid |
|
143 |
XxlJobLog jobLog = xxlJobLogDao.load(logId); // todo, need to improve performance |
|
144 |
if (jobLog == null) { |
|
145 |
return new ReturnT<LogResult>(ReturnT.FAIL_CODE, I18nUtil.getString("joblog_logid_unvalid")); |
|
146 |
} |
|
147 |
|
|
148 |
// log cat |
|
149 |
ExecutorBiz executorBiz = XxlJobScheduler.getExecutorBiz(jobLog.getExecutorAddress()); |
|
150 |
ReturnT<LogResult> logResult = executorBiz.log(new LogParam(jobLog.getTriggerTime().getTime(), logId, fromLineNum)); |
|
151 |
|
|
152 |
// is end |
|
153 |
if (logResult.getContent()!=null && logResult.getContent().getFromLineNum() > logResult.getContent().getToLineNum()) { |
|
154 |
if (jobLog.getHandleCode() > 0) { |
|
155 |
logResult.getContent().setEnd(true); |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
// fix xss |
|
160 |
if (logResult.getContent()!=null && StringUtils.hasText(logResult.getContent().getLogContent())) { |
|
161 |
String newLogContent = logResult.getContent().getLogContent(); |
|
162 |
newLogContent = HtmlUtils.htmlEscape(newLogContent, "UTF-8"); |
|
163 |
logResult.getContent().setLogContent(newLogContent); |
|
164 |
} |
|
165 |
|
|
166 |
return logResult; |
|
167 |
} catch (Exception e) { |
|
168 |
logger.error(e.getMessage(), e); |
|
169 |
return new ReturnT<LogResult>(ReturnT.FAIL_CODE, e.getMessage()); |
|
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
@RequestMapping("/logKill") |
|
174 |
@ResponseBody |
|
175 |
public ReturnT<String> logKill(int id){ |
|
176 |
// base check |
|
177 |
XxlJobLog log = xxlJobLogDao.load(id); |
|
178 |
XxlJobInfo jobInfo = xxlJobInfoDao.loadById(log.getJobId()); |
|
179 |
if (jobInfo==null) { |
|
180 |
return new ReturnT<String>(500, I18nUtil.getString("jobinfo_glue_jobid_unvalid")); |
|
181 |
} |
|
182 |
if (ReturnT.SUCCESS_CODE != log.getTriggerCode()) { |
|
183 |
return new ReturnT<String>(500, I18nUtil.getString("joblog_kill_log_limit")); |
|
184 |
} |
|
185 |
|
|
186 |
// request of kill |
|
187 |
ReturnT<String> runResult = null; |
|
188 |
try { |
|
189 |
ExecutorBiz executorBiz = XxlJobScheduler.getExecutorBiz(log.getExecutorAddress()); |
|
190 |
runResult = executorBiz.kill(new KillParam(jobInfo.getId())); |
|
191 |
} catch (Exception e) { |
|
192 |
logger.error(e.getMessage(), e); |
|
193 |
runResult = new ReturnT<String>(500, e.getMessage()); |
|
194 |
} |
|
195 |
|
|
196 |
if (ReturnT.SUCCESS_CODE == runResult.getCode()) { |
|
197 |
log.setHandleCode(ReturnT.FAIL_CODE); |
|
198 |
log.setHandleMsg( I18nUtil.getString("joblog_kill_log_byman")+":" + (runResult.getMsg()!=null?runResult.getMsg():"")); |
|
199 |
log.setHandleTime(new Date()); |
|
200 |
XxlJobCompleter.updateHandleInfoAndFinish(log); |
|
201 |
return new ReturnT<String>(runResult.getMsg()); |
|
202 |
} else { |
|
203 |
return new ReturnT<String>(500, runResult.getMsg()); |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
207 |
@RequestMapping("/clearLog") |
|
208 |
@ResponseBody |
|
209 |
public ReturnT<String> clearLog(int jobGroup, int jobId, int type){ |
|
210 |
|
|
211 |
Date clearBeforeTime = null; |
|
212 |
int clearBeforeNum = 0; |
|
213 |
if (type == 1) { |
|
214 |
clearBeforeTime = DateUtil.addMonths(new Date(), -1); // 清理一个月之前日志数据 |
|
215 |
} else if (type == 2) { |
|
216 |
clearBeforeTime = DateUtil.addMonths(new Date(), -3); // 清理三个月之前日志数据 |
|
217 |
} else if (type == 3) { |
|
218 |
clearBeforeTime = DateUtil.addMonths(new Date(), -6); // 清理六个月之前日志数据 |
|
219 |
} else if (type == 4) { |
|
220 |
clearBeforeTime = DateUtil.addYears(new Date(), -1); // 清理一年之前日志数据 |
|
221 |
} else if (type == 5) { |
|
222 |
clearBeforeNum = 1000; // 清理一千条以前日志数据 |
|
223 |
} else if (type == 6) { |
|
224 |
clearBeforeNum = 10000; // 清理一万条以前日志数据 |
|
225 |
} else if (type == 7) { |
|
226 |
clearBeforeNum = 30000; // 清理三万条以前日志数据 |
|
227 |
} else if (type == 8) { |
|
228 |
clearBeforeNum = 100000; // 清理十万条以前日志数据 |
|
229 |
} else if (type == 9) { |
|
230 |
clearBeforeNum = 0; // 清理所有日志数据 |
|
231 |
} else { |
|
232 |
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("joblog_clean_type_unvalid")); |
|
233 |
} |
|
234 |
|
|
235 |
List<Long> logIds = null; |
|
236 |
do { |
|
237 |
logIds = xxlJobLogDao.findClearLogIds(jobGroup, jobId, clearBeforeTime, clearBeforeNum, 1000); |
|
238 |
if (logIds!=null && logIds.size()>0) { |
|
239 |
xxlJobLogDao.clearLog(logIds); |
|
240 |
} |
|
241 |
} while (logIds!=null && logIds.size()>0); |
|
242 |
|
|
243 |
return ReturnT.SUCCESS; |
|
244 |
} |
|
245 |
|
|
246 |
} |