Skip to content

Commit

Permalink
解决repeat job两个bug:job多执行一次,不按照job的第一次初始时间执行。
Browse files Browse the repository at this point in the history
 repeatedCount已经重复的次数,从1开始,最大值repeatCount;repeatCount可重复次数,从0开始。
 假如可执行job列表里的repeatedCount假如为2,那么此时RepeatJobQueue里面的repeatedCount值应该为1。当这个可执行job执行完后,RepeatJobQueue里面的repeatedCount更新为2,再次生成的可执行repeat
job里的repeatedCount应该为3.
  • Loading branch information
lusong1986 committed Jul 27, 2017
1 parent b8f66b9 commit 2b10aa9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,13 @@ public static void addRepeatJobForInterval(
}
// 计算出应该重复的次数
int repeatedCount = Long.valueOf((lastGenerateTime.getTime() - firstTriggerTime) / jobPo.getRepeatInterval()).intValue();
if (repeatedCount <= 0) {
repeatedCount = 1; //repeatedCount从1开始
}

boolean stop = false;
while (!stop) {
Long nextTriggerTime = firstTriggerTime + repeatedCount * repeatInterval;
Long nextTriggerTime = firstTriggerTime + (repeatedCount - 1) * repeatInterval; //第一次执行时间点应该是firstTriggerTime

if (nextTriggerTime <= endTime &&
(repeatCount == -1 || repeatedCount <= repeatCount)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private void finishNoReplyPrevRepeatJob(JobMeta jobMeta, boolean isRetryForThisT
// 可能任务队列中改条记录被删除了
return;
}
if (jobPo.getRepeatCount() != -1 && jobPo.getRepeatedCount() >= jobPo.getRepeatCount()) {
if (jobPo.getRepeatCount() != -1 && (jobPo.getRepeatedCount() + 1) >= jobPo.getRepeatCount()) { //最后一次执行时 repeatedCount+1=repeatCount
// 已经重试完成, 那么删除, 这里可以不用check可执行队列是否还有,因为这里依赖的是计数
appContext.getRepeatJobQueue().remove(jobPo.getJobId());
jobRemoveLog(jobPo, "Repeat");
Expand All @@ -137,7 +137,7 @@ private void finishRepeatJob(String jobId, boolean isRetryForThisTime) {
// 可能任务队列中改条记录被删除了
return;
}
if (jobPo.getRepeatCount() != -1 && jobPo.getRepeatedCount() >= jobPo.getRepeatCount()) {
if (jobPo.getRepeatCount() != -1 && (jobPo.getRepeatedCount() + 1) >= jobPo.getRepeatCount()) { //最后一次执行时 repeatedCount+1=repeatCount
// 已经重试完成, 那么删除
appContext.getRepeatJobQueue().remove(jobId);
jobRemoveLog(jobPo, "Repeat");
Expand All @@ -156,7 +156,7 @@ private void finishRepeatJob(String jobId, boolean isRetryForThisTime) {
}
long nexTriggerTime = JobUtils.getRepeatNextTriggerTime(jobPo);
try {
jobPo.setRepeatedCount(repeatedCount);
jobPo.setRepeatedCount(repeatedCount + 1); //再生成可执行job时,从RepeatJobQueue更新后的repeatedCount再加1
jobPo.setTaskTrackerIdentity(null);
jobPo.setIsRunning(false);
jobPo.setTriggerTime(nexTriggerTime);
Expand Down

0 comments on commit 2b10aa9

Please sign in to comment.