a5580f1169d5e4ecc40121675fc1c980c265e826.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.xxl.job.admin.service.impl;
  2. import com.xxl.job.admin.core.model.XxlJobGroup;
  3. import com.xxl.job.admin.core.model.XxlJobInfo;
  4. import com.xxl.job.admin.core.cron.CronExpression;
  5. import com.xxl.job.admin.core.model.XxlJobLogReport;
  6. import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
  7. import com.xxl.job.admin.core.thread.JobScheduleHelper;
  8. import com.xxl.job.admin.core.util.I18nUtil;
  9. import com.xxl.job.admin.dao.*;
  10. import com.xxl.job.admin.service.XxlJobService;
  11. import com.xxl.job.core.biz.model.ReturnT;
  12. import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
  13. import com.xxl.job.core.glue.GlueTypeEnum;
  14. import com.xxl.job.core.util.DateUtil;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.text.MessageFormat;
  20. import java.text.ParseException;
  21. import java.util.*;
  22. /**
  23. * core job action for xxl-job
  24. * @author xuxueli 2016-5-28 15:30:33
  25. */
  26. @Service
  27. public class XxlJobServiceImpl implements XxlJobService {
  28. private static Logger logger = LoggerFactory.getLogger(XxlJobServiceImpl.class);
  29. @Resource
  30. private XxlJobGroupDao xxlJobGroupDao;
  31. @Resource
  32. private XxlJobInfoDao xxlJobInfoDao;
  33. @Resource
  34. public XxlJobLogDao xxlJobLogDao;
  35. @Resource
  36. private XxlJobLogGlueDao xxlJobLogGlueDao;
  37. @Resource
  38. private XxlJobLogReportDao xxlJobLogReportDao;
  39. @Override
  40. public Map<String, Object> pageList(int start, int length, int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) {
  41. // page list
  42. List<XxlJobInfo> list = xxlJobInfoDao.pageList(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);
  43. int list_count = xxlJobInfoDao.pageListCount(start, length, jobGroup, triggerStatus, jobDesc, executorHandler, author);
  44. // package result
  45. Map<String, Object> maps = new HashMap<String, Object>();
  46. maps.put("recordsTotal", list_count); // 总记录数
  47. maps.put("recordsFiltered", list_count); // 过滤后的总记录数
  48. maps.put("data", list); // 分页列表
  49. return maps;
  50. }
  51. @Override
  52. public ReturnT<String> add(XxlJobInfo jobInfo) {
  53. // valid
  54. XxlJobGroup group = xxlJobGroupDao.load(jobInfo.getJobGroup());
  55. if (group == null) {
  56. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_choose")+I18nUtil.getString("jobinfo_field_jobgroup")) );
  57. }
  58. if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
  59. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
  60. }
  61. if (jobInfo.getJobDesc()==null || jobInfo.getJobDesc().trim().length()==0) {
  62. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
  63. }
  64. if (jobInfo.getAuthor()==null || jobInfo.getAuthor().trim().length()==0) {
  65. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_author")) );
  66. }
  67. if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
  68. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy")+I18nUtil.getString("system_unvalid")) );
  69. }
  70. if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
  71. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy")+I18nUtil.getString("system_unvalid")) );
  72. }
  73. if (GlueTypeEnum.match(jobInfo.getGlueType()) == null) {
  74. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_gluetype")+I18nUtil.getString("system_unvalid")) );
  75. }
  76. if (GlueTypeEnum.BEAN==GlueTypeEnum.match(jobInfo.getGlueType()) && (jobInfo.getExecutorHandler()==null || jobInfo.getExecutorHandler().trim().length()==0) ) {
  77. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+"JobHandler") );
  78. }
  79. // fix "\r" in shell
  80. if (GlueTypeEnum.GLUE_SHELL==GlueTypeEnum.match(jobInfo.getGlueType()) && jobInfo.getGlueSource()!=null) {
  81. jobInfo.setGlueSource(jobInfo.getGlueSource().replaceAll("\r", ""));
  82. }
  83. // ChildJobId valid
  84. if (jobInfo.getChildJobId()!=null && jobInfo.getChildJobId().trim().length()>0) {
  85. String[] childJobIds = jobInfo.getChildJobId().split(",");
  86. for (String childJobIdItem: childJobIds) {
  87. if (childJobIdItem!=null && childJobIdItem.trim().length()>0 && isNumeric(childJobIdItem)) {
  88. XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.parseInt(childJobIdItem));
  89. if (childJobInfo==null) {
  90. return new ReturnT<String>(ReturnT.FAIL_CODE,
  91. MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_not_found")), childJobIdItem));
  92. }
  93. } else {
  94. return new ReturnT<String>(ReturnT.FAIL_CODE,
  95. MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_unvalid")), childJobIdItem));
  96. }
  97. }
  98. // join , avoid "xxx,,"
  99. String temp = "";
  100. for (String item:childJobIds) {
  101. temp += item + ",";
  102. }
  103. temp = temp.substring(0, temp.length()-1);
  104. jobInfo.setChildJobId(temp);
  105. }
  106. // add in db
  107. jobInfo.setAddTime(new Date());
  108. jobInfo.setUpdateTime(new Date());
  109. jobInfo.setGlueUpdatetime(new Date());
  110. xxlJobInfoDao.save(jobInfo);
  111. if (jobInfo.getId() < 1) {
  112. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_add")+I18nUtil.getString("system_fail")) );
  113. }
  114. return new ReturnT<String>(String.valueOf(jobInfo.getId()));
  115. }
  116. private boolean isNumeric(String str){
  117. try {
  118. int result = Integer.valueOf(str);
  119. return true;
  120. } catch (NumberFormatException e) {
  121. return false;
  122. }
  123. }
  124. @Override
  125. public ReturnT<String> update(XxlJobInfo jobInfo) {
  126. // valid
  127. if (!CronExpression.isValidExpression(jobInfo.getJobCron())) {
  128. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
  129. }
  130. if (jobInfo.getJobDesc()==null || jobInfo.getJobDesc().trim().length()==0) {
  131. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_jobdesc")) );
  132. }
  133. if (jobInfo.getAuthor()==null || jobInfo.getAuthor().trim().length()==0) {
  134. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("system_please_input")+I18nUtil.getString("jobinfo_field_author")) );
  135. }
  136. if (ExecutorRouteStrategyEnum.match(jobInfo.getExecutorRouteStrategy(), null) == null) {
  137. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorRouteStrategy")+I18nUtil.getString("system_unvalid")) );
  138. }
  139. if (ExecutorBlockStrategyEnum.match(jobInfo.getExecutorBlockStrategy(), null) == null) {
  140. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_executorBlockStrategy")+I18nUtil.getString("system_unvalid")) );
  141. }
  142. // ChildJobId valid
  143. if (jobInfo.getChildJobId()!=null && jobInfo.getChildJobId().trim().length()>0) {
  144. String[] childJobIds = jobInfo.getChildJobId().split(",");
  145. for (String childJobIdItem: childJobIds) {
  146. if (childJobIdItem!=null && childJobIdItem.trim().length()>0 && isNumeric(childJobIdItem)) {
  147. XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.parseInt(childJobIdItem));
  148. if (childJobInfo==null) {
  149. return new ReturnT<String>(ReturnT.FAIL_CODE,
  150. MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_not_found")), childJobIdItem));
  151. }
  152. } else {
  153. return new ReturnT<String>(ReturnT.FAIL_CODE,
  154. MessageFormat.format((I18nUtil.getString("jobinfo_field_childJobId")+"({0})"+I18nUtil.getString("system_unvalid")), childJobIdItem));
  155. }
  156. }
  157. // join , avoid "xxx,,"
  158. String temp = "";
  159. for (String item:childJobIds) {
  160. temp += item + ",";
  161. }
  162. temp = temp.substring(0, temp.length()-1);
  163. jobInfo.setChildJobId(temp);
  164. }
  165. // group valid
  166. XxlJobGroup jobGroup = xxlJobGroupDao.load(jobInfo.getJobGroup());
  167. if (jobGroup == null) {
  168. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_jobgroup")+I18nUtil.getString("system_unvalid")) );
  169. }
  170. // stage job info
  171. XxlJobInfo exists_jobInfo = xxlJobInfoDao.loadById(jobInfo.getId());
  172. if (exists_jobInfo == null) {
  173. return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("jobinfo_field_id")+I18nUtil.getString("system_not_found")) );
  174. }
  175. // next trigger time (5s后生效,避开预读周期)
  176. long nextTriggerTime = exists_jobInfo.getTriggerNextTime();
  177. if (exists_jobInfo.getTriggerStatus() == 1 && !jobInfo.getJobCron().equals(exists_jobInfo.getJobCron()) ) {
  178. try {
  179. Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS));
  180. if (nextValidTime == null) {
  181. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire"));
  182. }
  183. nextTriggerTime = nextValidTime.getTime();
  184. } catch (ParseException e) {
  185. logger.error(e.getMessage(), e);
  186. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage());
  187. }
  188. }
  189. exists_jobInfo.setJobGroup(jobInfo.getJobGroup());
  190. exists_jobInfo.setJobCron(jobInfo.getJobCron());
  191. exists_jobInfo.setJobDesc(jobInfo.getJobDesc());
  192. exists_jobInfo.setAuthor(jobInfo.getAuthor());
  193. exists_jobInfo.setAlarmEmail(jobInfo.getAlarmEmail());
  194. exists_jobInfo.setExecutorRouteStrategy(jobInfo.getExecutorRouteStrategy());
  195. exists_jobInfo.setExecutorHandler(jobInfo.getExecutorHandler());
  196. exists_jobInfo.setExecutorParam(jobInfo.getExecutorParam());
  197. exists_jobInfo.setExecutorBlockStrategy(jobInfo.getExecutorBlockStrategy());
  198. exists_jobInfo.setExecutorTimeout(jobInfo.getExecutorTimeout());
  199. exists_jobInfo.setExecutorFailRetryCount(jobInfo.getExecutorFailRetryCount());
  200. exists_jobInfo.setChildJobId(jobInfo.getChildJobId());
  201. exists_jobInfo.setTriggerNextTime(nextTriggerTime);
  202. exists_jobInfo.setUpdateTime(new Date());
  203. xxlJobInfoDao.update(exists_jobInfo);
  204. return ReturnT.SUCCESS;
  205. }
  206. @Override
  207. public ReturnT<String> remove(int id) {
  208. XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
  209. if (xxlJobInfo == null) {
  210. return ReturnT.SUCCESS;
  211. }
  212. xxlJobInfoDao.delete(id);
  213. xxlJobLogDao.delete(id);
  214. xxlJobLogGlueDao.deleteByJobId(id);
  215. return ReturnT.SUCCESS;
  216. }
  217. @Override
  218. public ReturnT<String> start(int id) {
  219. XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
  220. // next trigger time (5s后生效,避开预读周期)
  221. long nextTriggerTime = 0;
  222. try {
  223. Date nextValidTime = new CronExpression(xxlJobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS));
  224. if (nextValidTime == null) {
  225. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire"));
  226. }
  227. nextTriggerTime = nextValidTime.getTime();
  228. } catch (ParseException e) {
  229. logger.error(e.getMessage(), e);
  230. return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage());
  231. }
  232. xxlJobInfo.setTriggerStatus(1);
  233. xxlJobInfo.setTriggerLastTime(0);
  234. xxlJobInfo.setTriggerNextTime(nextTriggerTime);
  235. xxlJobInfo.setUpdateTime(new Date());
  236. xxlJobInfoDao.update(xxlJobInfo);
  237. return ReturnT.SUCCESS;
  238. }
  239. @Override
  240. public ReturnT<String> stop(int id) {
  241. XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
  242. xxlJobInfo.setTriggerStatus(0);
  243. xxlJobInfo.setTriggerLastTime(0);
  244. xxlJobInfo.setTriggerNextTime(0);
  245. xxlJobInfo.setUpdateTime(new Date());
  246. xxlJobInfoDao.update(xxlJobInfo);
  247. return ReturnT.SUCCESS;
  248. }
  249. @Override
  250. public Map<String, Object> dashboardInfo() {
  251. int jobInfoCount = xxlJobInfoDao.findAllCount();
  252. int jobLogCount = 0;
  253. int jobLogSuccessCount = 0;
  254. XxlJobLogReport xxlJobLogReport = xxlJobLogReportDao.queryLogReportTotal();
  255. if (xxlJobLogReport != null) {
  256. jobLogCount = xxlJobLogReport.getRunningCount() + xxlJobLogReport.getSucCount() + xxlJobLogReport.getFailCount();
  257. jobLogSuccessCount = xxlJobLogReport.getSucCount();
  258. }
  259. // executor count
  260. Set<String> executorAddressSet = new HashSet<String>();
  261. List<XxlJobGroup> groupList = xxlJobGroupDao.findAll();
  262. if (groupList!=null && !groupList.isEmpty()) {
  263. for (XxlJobGroup group: groupList) {
  264. if (group.getRegistryList()!=null && !group.getRegistryList().isEmpty()) {
  265. executorAddressSet.addAll(group.getRegistryList());
  266. }
  267. }
  268. }
  269. int executorCount = executorAddressSet.size();
  270. Map<String, Object> dashboardMap = new HashMap<String, Object>();
  271. dashboardMap.put("jobInfoCount", jobInfoCount);
  272. dashboardMap.put("jobLogCount", jobLogCount);
  273. dashboardMap.put("jobLogSuccessCount", jobLogSuccessCount);
  274. dashboardMap.put("executorCount", executorCount);
  275. return dashboardMap;
  276. }
  277. @Override
  278. public ReturnT<Map<String, Object>> chartInfo(Date startDate, Date endDate) {
  279. // process
  280. List<String> triggerDayList = new ArrayList<String>();
  281. List<Integer> triggerDayCountRunningList = new ArrayList<Integer>();
  282. List<Integer> triggerDayCountSucList = new ArrayList<Integer>();
  283. List<Integer> triggerDayCountFailList = new ArrayList<Integer>();
  284. int triggerCountRunningTotal = 0;
  285. int triggerCountSucTotal = 0;
  286. int triggerCountFailTotal = 0;
  287. List<XxlJobLogReport> logReportList = xxlJobLogReportDao.queryLogReport(startDate, endDate);
  288. if (logReportList!=null && logReportList.size()>0) {
  289. for (XxlJobLogReport item: logReportList) {
  290. String day = DateUtil.formatDate(item.getTriggerDay());
  291. int triggerDayCountRunning = item.getRunningCount();
  292. int triggerDayCountSuc = item.getSucCount();
  293. int triggerDayCountFail = item.getFailCount();
  294. triggerDayList.add(day);
  295. triggerDayCountRunningList.add(triggerDayCountRunning);
  296. triggerDayCountSucList.add(triggerDayCountSuc);
  297. triggerDayCountFailList.add(triggerDayCountFail);
  298. triggerCountRunningTotal += triggerDayCountRunning;
  299. triggerCountSucTotal += triggerDayCountSuc;
  300. triggerCountFailTotal += triggerDayCountFail;
  301. }
  302. } else {
  303. for (int i = -6; i <= 0; i++) {
  304. triggerDayList.add(DateUtil.formatDate(DateUtil.addDays(new Date(), i)));
  305. triggerDayCountRunningList.add(0);
  306. triggerDayCountSucList.add(0);
  307. triggerDayCountFailList.add(0);
  308. }
  309. }
  310. Map<String, Object> result = new HashMap<String, Object>();
  311. result.put("triggerDayList", triggerDayList);
  312. result.put("triggerDayCountRunningList", triggerDayCountRunningList);
  313. result.put("triggerDayCountSucList", triggerDayCountSucList);
  314. result.put("triggerDayCountFailList", triggerDayCountFailList);
  315. result.put("triggerCountRunningTotal", triggerCountRunningTotal);
  316. result.put("triggerCountSucTotal", triggerCountSucTotal);
  317. result.put("triggerCountFailTotal", triggerCountFailTotal);
  318. return new ReturnT<Map<String, Object>>(result);
  319. }
  320. }