Skip to content

Commit

Permalink
任务下次运行时间优化:支持Cron、固定间隔等多种时间计算。
Browse files Browse the repository at this point in the history
  • Loading branch information
xueli.xue committed Nov 5, 2020
1 parent 51682bc commit 25a331b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
import com.xxl.job.admin.core.scheduler.MisfireStrategyEnum;
import com.xxl.job.admin.core.scheduler.ScheduleTypeEnum;
import com.xxl.job.admin.core.thread.JobScheduleHelper;
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
import com.xxl.job.admin.core.trigger.TriggerTypeEnum;
import com.xxl.job.admin.core.util.I18nUtil;
Expand All @@ -18,6 +19,8 @@
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
import com.xxl.job.core.glue.GlueTypeEnum;
import com.xxl.job.core.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -36,6 +39,7 @@
@Controller
@RequestMapping("/jobinfo")
public class JobInfoController {
private static Logger logger = LoggerFactory.getLogger(JobInfoController.class);

@Resource
private XxlJobGroupDao xxlJobGroupDao;
Expand Down Expand Up @@ -148,23 +152,29 @@ public ReturnT<String> triggerJob(int id, String executorParam, String addressLi

@RequestMapping("/nextTriggerTime")
@ResponseBody
public ReturnT<List<String>> nextTriggerTime(String cron) {
public ReturnT<List<String>> nextTriggerTime(String scheduleType, String scheduleConf) {

XxlJobInfo paramXxlJobInfo = new XxlJobInfo();
paramXxlJobInfo.setScheduleType(scheduleType);
paramXxlJobInfo.setScheduleConf(scheduleConf);

List<String> result = new ArrayList<>();
try {
CronExpression cronExpression = new CronExpression(cron);
Date lastTime = new Date();
for (int i = 0; i < 5; i++) {
lastTime = cronExpression.getNextValidTimeAfter(lastTime);
lastTime = JobScheduleHelper.generateNextValidTime(paramXxlJobInfo, lastTime);
if (lastTime != null) {
result.add(DateUtil.formatDateTime(lastTime));
} else {
break;
}
}
} catch (ParseException e) {
return new ReturnT<List<String>>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid"));
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new ReturnT<List<String>>(ReturnT.FAIL_CODE, (I18nUtil.getString("schedule_type")+I18nUtil.getString("system_unvalid")) + e.getMessage());
}
return new ReturnT<List<String>>(result);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public ReturnT<String> add(XxlJobInfo jobInfo) {
}
if (scheduleTypeEnum == ScheduleTypeEnum.CRON) {
if (jobInfo.getScheduleConf()==null || !CronExpression.isValidExpression(jobInfo.getScheduleConf())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
return new ReturnT<String>(ReturnT.FAIL_CODE, "Cron"+I18nUtil.getString("system_unvalid"));
}
} else if (scheduleTypeEnum == ScheduleTypeEnum.FIX_RATE/* || scheduleTypeEnum == ScheduleTypeEnum.FIX_DELAY*/) {
if (jobInfo.getScheduleConf() == null) {
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("schedule_type")+I18nUtil.getString("system_unvalid")) );
return new ReturnT<String>(ReturnT.FAIL_CODE, (I18nUtil.getString("schedule_type")) );
}
try {
int fixSecond = Integer.valueOf(jobInfo.getScheduleConf());
Expand Down Expand Up @@ -183,7 +183,7 @@ public ReturnT<String> update(XxlJobInfo jobInfo) {
}
if (scheduleTypeEnum == ScheduleTypeEnum.CRON) {
if (jobInfo.getScheduleConf()==null || !CronExpression.isValidExpression(jobInfo.getScheduleConf())) {
return new ReturnT<String>(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid") );
return new ReturnT<String>(ReturnT.FAIL_CODE, "Cron"+I18nUtil.getString("system_unvalid") );
}
} else if (scheduleTypeEnum == ScheduleTypeEnum.FIX_RATE /*|| scheduleTypeEnum == ScheduleTypeEnum.FIX_DELAY*/) {
if (jobInfo.getScheduleConf() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ jobinfo_field_jobdesc=Job description
jobinfo_field_timeout=Job timeout period
jobinfo_field_gluetype=GLUE Type
jobinfo_field_executorparam=Param
jobinfo_field_cron_unvalid=The Cron is illegal
jobinfo_field_author=Author
jobinfo_field_alarmemail=Alarm email
jobinfo_field_alarmemail_placeholder=Please enter alarm mail, if there are more than one comma separated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ jobinfo_field_jobgroup=执行器
jobinfo_field_jobdesc=任务描述
jobinfo_field_gluetype=运行模式
jobinfo_field_executorparam=任务参数
jobinfo_field_cron_unvalid=Cron格式非法
jobinfo_field_author=负责人
jobinfo_field_timeout=任务超时时间
jobinfo_field_alarmemail=报警邮件
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ jobinfo_field_jobgroup=執行器
jobinfo_field_jobdesc=任務描述
jobinfo_field_gluetype=運行模式
jobinfo_field_executorparam=任務參數
jobinfo_field_cron_unvalid=Cron 格式非法
jobinfo_field_author=負責人
jobinfo_field_timeout=任務超時秒數
jobinfo_field_alarmemail=告警郵件
Expand Down
23 changes: 11 additions & 12 deletions xxl-job-admin/src/main/resources/static/js/jobinfo.index.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,16 @@ $(function() {
start_stop_div = '<li><a href="javascript:void(0);" class="job_operate" _type="job_resume" >'+ I18n.jobinfo_opt_start +'</a></li>\n';
}

// job_next_time_html
var job_next_time_html = '';
if (row.scheduleType == 'CRON' || row.scheduleType == 'FIX_RATE') {
job_next_time_html = '<li><a href="javascript:void(0);" class="job_next_time" >' + I18n.jobinfo_opt_next_time + '</a></li>\n';
}

// log url
var logHref = base_url +'/joblog?jobId='+ row.id;

// log url
// code url
var codeBtn = "";
if ('BEAN' != row.glueType) {
var codeUrl = base_url +'/jobcode?jobId='+ row.id;
Expand All @@ -143,7 +149,7 @@ $(function() {
' <li><a href="javascript:void(0);" class="job_trigger" >'+ I18n.jobinfo_opt_run +'</a></li>\n' +
' <li><a href="'+ logHref +'">'+ I18n.jobinfo_opt_log +'</a></li>\n' +
' <li><a href="javascript:void(0);" class="job_registryinfo" >' + I18n.jobinfo_opt_registryinfo + '</a></li>\n' +
' <li><a href="javascript:void(0);" class="job_next_time" >' + I18n.jobinfo_opt_next_time + '</a></li>\n' +
job_next_time_html +
' <li class="divider"></li>\n' +
codeBtn +
start_stop_div +
Expand Down Expand Up @@ -329,17 +335,16 @@ $(function() {
var id = $(this).parents('ul').attr("_id");
var row = tableData['key'+id];

var jobCron = row.jobCron;

$.ajax({
type : 'POST',
url : base_url + "/jobinfo/nextTriggerTime",
data : {
"cron" : jobCron
"scheduleType" : row.scheduleType,
"scheduleConf" : row.scheduleConf
},
dataType : "json",
success : function(data){

if (data.code != 200) {
layer.open({
title: I18n.jobinfo_opt_next_time ,
Expand Down Expand Up @@ -585,9 +590,6 @@ $(function() {
required : true,
maxlength: 50
},
jobCron : {
required : true
},
author : {
required : true
}
Expand All @@ -596,9 +598,6 @@ $(function() {
jobDesc : {
required : I18n.system_please_input + I18n.jobinfo_field_jobdesc
},
jobCron : {
required : I18n.system_please_input + "Cron"
},
author : {
required : I18n.system_please_input + I18n.jobinfo_field_author
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@
type : 'GET',
url : base_url + "/jobinfo/nextTriggerTime",
data : {
"cron" : inputElement.val(),
"scheduleType" : 'CRON',
"scheduleConf" : inputElement.val()
},
dataType : "json",
success : function(data){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@
type : 'GET',
url : base_url + "/jobinfo/nextTriggerTime",
data : {
"cron" : inputElement.val(),
"scheduleType" : 'CRON',
"scheduleConf" : inputElement.val()
},
dataType : "json",
success : function(data){
Expand Down

0 comments on commit 25a331b

Please sign in to comment.