From 5b280137fb31ff4a8eb58faaa912414815bb47f8 Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Tue, 16 Jul 2019 21:25:12 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E5=BA=A6=E7=BB=84=E4=BB=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=9A=E9=92=88=E5=AF=B9=E6=B0=B8=E8=BF=9C=E4=B8=8D?= =?UTF-8?q?=E4=BC=9A=E8=A7=A6=E5=8F=91=E7=9A=84Cron=E7=A6=81=E6=AD=A2?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=92=8C=E5=90=AF=E5=8A=A8=EF=BC=9B=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1Cron=E6=9C=80=E5=90=8E=E4=B8=80=E6=AC=A1=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E5=90=8E=E5=86=8D=E4=B9=9F=E4=B8=8D=E4=BC=9A=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E6=97=B6=EF=BC=8C=E6=AF=94=E5=A6=82=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=80=A7=E4=BB=BB=E5=8A=A1=EF=BC=8C=E4=B8=BB=E5=8A=A8=E5=81=9C?= =?UTF-8?q?=E6=AD=A2=E7=9B=B8=E5=85=B3=E4=BB=BB=E5=8A=A1=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/XXL-JOB官方文档.md | 1 + .../admin/core/thread/JobScheduleHelper.java | 45 +++++++++++-------- .../admin/service/impl/XxlJobServiceImpl.java | 12 ++++- .../main/resources/i18n/message.properties | 1 + .../main/resources/i18n/message_en.properties | 1 + .../mybatis-mapper/XxlJobInfoMapper.xml | 3 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/doc/XXL-JOB官方文档.md b/doc/XXL-JOB官方文档.md index 74afa447..a0d54277 100644 --- a/doc/XXL-JOB官方文档.md +++ b/doc/XXL-JOB官方文档.md @@ -1519,6 +1519,7 @@ Tips: 历史版本(V1.3.x)目前已经Release至稳定版本, 进入维护阶段 - 8、[ING]任务、执行器数据全量本地缓存;新增消息表广播通知; - 9、[ING]任务触发组件优化,常规1s预加载一次,轮空时主动休眠4s; - 10、注册监控线程优化,降低死锁几率; +- 11、调度组件优化:针对永远不会触发的Cron禁止配置和启动;任务Cron最后一次触发后再也不会触发时,比如一次性任务,主动停止相关任务; ### TODO LIST diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobScheduleHelper.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobScheduleHelper.java index ae7983fe..89c5e6d0 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobScheduleHelper.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/thread/JobScheduleHelper.java @@ -78,12 +78,15 @@ public class JobScheduleHelper { // 过期超5s:本地忽略,当前时间开始计算下次触发时间 // fresh next - jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); - jobInfo.setTriggerNextTime( - new CronExpression(jobInfo.getJobCron()) - .getNextValidTimeAfter(new Date()) - .getTime() - ); + Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date()); + if (nextValidTime != null) { + jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); + jobInfo.setTriggerNextTime(nextValidTime.getTime()); + } else { + jobInfo.setTriggerStatus(0); + jobInfo.setTriggerLastTime(0); + jobInfo.setTriggerNextTime(0); + } } else if (nowTime > jobInfo.getTriggerNextTime()) { // 过期5s内 :立即触发一次,当前时间开始计算下次触发时间; @@ -110,12 +113,15 @@ public class JobScheduleHelper { pushTimeRing(ringSecond, jobInfo.getId()); // 3、fresh next - jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); - jobInfo.setTriggerNextTime( - new CronExpression(jobInfo.getJobCron()) - .getNextValidTimeAfter(new Date(jobInfo.getTriggerNextTime())) - .getTime() - ); + Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(jobInfo.getTriggerNextTime())); + if (nextValidTime != null) { + jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); + jobInfo.setTriggerNextTime(nextValidTime.getTime()); + } else { + jobInfo.setTriggerStatus(0); + jobInfo.setTriggerLastTime(0); + jobInfo.setTriggerNextTime(0); + } } @@ -129,12 +135,15 @@ public class JobScheduleHelper { pushTimeRing(ringSecond, jobInfo.getId()); // 3、fresh next - jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); - jobInfo.setTriggerNextTime( - new CronExpression(jobInfo.getJobCron()) - .getNextValidTimeAfter(new Date(jobInfo.getTriggerNextTime())) - .getTime() - ); + Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(jobInfo.getTriggerNextTime())); + if (nextValidTime != null) { + jobInfo.setTriggerLastTime(jobInfo.getTriggerNextTime()); + jobInfo.setTriggerNextTime(nextValidTime.getTime()); + } else { + jobInfo.setTriggerStatus(0); + jobInfo.setTriggerLastTime(0); + jobInfo.setTriggerNextTime(0); + } } diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java index 8e067742..a2d2e0b1 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/XxlJobServiceImpl.java @@ -194,7 +194,11 @@ public class XxlJobServiceImpl implements XxlJobService { long nextTriggerTime = exists_jobInfo.getTriggerNextTime(); if (exists_jobInfo.getTriggerStatus() == 1 && !jobInfo.getJobCron().equals(exists_jobInfo.getJobCron()) ) { try { - nextTriggerTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS)).getTime(); + Date nextValidTime = new CronExpression(jobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS)); + if (nextValidTime == null) { + return new ReturnT(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire")); + } + nextTriggerTime = nextValidTime.getTime(); } catch (ParseException e) { logger.error(e.getMessage(), e); return new ReturnT(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage()); @@ -240,7 +244,11 @@ public class XxlJobServiceImpl implements XxlJobService { // next trigger time (5s后生效,避开预读周期) long nextTriggerTime = 0; try { - nextTriggerTime = new CronExpression(xxlJobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS)).getTime(); + Date nextValidTime = new CronExpression(xxlJobInfo.getJobCron()).getNextValidTimeAfter(new Date(System.currentTimeMillis() + JobScheduleHelper.PRE_READ_MS)); + if (nextValidTime == null) { + return new ReturnT(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_never_fire")); + } + nextTriggerTime = nextValidTime.getTime(); } catch (ParseException e) { logger.error(e.getMessage(), e); return new ReturnT(ReturnT.FAIL_CODE, I18nUtil.getString("jobinfo_field_cron_unvalid")+" | "+ e.getMessage()); diff --git a/xxl-job-admin/src/main/resources/i18n/message.properties b/xxl-job-admin/src/main/resources/i18n/message.properties index f2436aea..2d2456a8 100644 --- a/xxl-job-admin/src/main/resources/i18n/message.properties +++ b/xxl-job-admin/src/main/resources/i18n/message.properties @@ -109,6 +109,7 @@ jobinfo_field_jobdesc=任务描述 jobinfo_field_gluetype=运行模式 jobinfo_field_executorparam=任务参数 jobinfo_field_cron_unvalid=Cron格式非法 +jobinfo_field_cron_never_fire=Cron非法,永远不会触发 jobinfo_field_author=负责人 jobinfo_field_timeout=任务超时时间 jobinfo_field_alarmemail=报警邮件 diff --git a/xxl-job-admin/src/main/resources/i18n/message_en.properties b/xxl-job-admin/src/main/resources/i18n/message_en.properties index 54955e07..e5140e89 100644 --- a/xxl-job-admin/src/main/resources/i18n/message_en.properties +++ b/xxl-job-admin/src/main/resources/i18n/message_en.properties @@ -110,6 +110,7 @@ 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_cron_never_fire=The Cron will never fire 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 diff --git a/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobInfoMapper.xml b/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobInfoMapper.xml index 58100366..46286fc5 100644 --- a/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobInfoMapper.xml +++ b/xxl-job-admin/src/main/resources/mybatis-mapper/XxlJobInfoMapper.xml @@ -219,7 +219,8 @@ UPDATE xxl_job_info SET trigger_last_time = #{triggerLastTime}, - trigger_next_time = #{triggerNextTime} + trigger_next_time = #{triggerNextTime}, + trigger_status = #{triggerStatus} WHERE id = #{id}