JobLogController.java 9.3 KB

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