Skip to content

Commit 9a74fa5

Browse files
authored
fix: Fix getExecutionsFailingLongerThan to not return executions that have previously failed but are now running ok (#685)
getExecutionsFailingLongerThan is not working as expected. Returns execution forever if it has failed once.. ## Fixes #563 ## Reminders - [ ] Added/ran automated tests - [ ] Update README and/or examples - [ ] Ran `mvn spotless:apply`
1 parent 8c1e4fa commit 9a74fa5

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/jdbc/JdbcTaskRepository.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,12 +673,12 @@ public List<Execution> getExecutionsFailingLongerThan(Duration interval) {
673673
"select * from "
674674
+ tableName
675675
+ " where "
676-
+ " ((last_success is null and last_failure is not null)"
677-
+ " or (last_failure is not null and last_success < ?)) "
676+
+ " consecutive_failures > 0 "
677+
+ " and (last_success is null or last_success < ?) "
678678
+ unresolvedFilter.andCondition(),
679679
(PreparedStatement p) -> {
680680
int index = 1;
681-
jdbcCustomization.setInstant(p, index++, Instant.now().minus(interval));
681+
jdbcCustomization.setInstant(p, index++, clock.now().minus(interval));
682682
unresolvedFilter.setParameters(p, index);
683683
},
684684
new ExecutionResultSetMapper(false, false));

db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/JdbcTaskRepositoryTest.java

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.github.kagkarlsson.scheduler.ScheduledExecutionsFilter.all;
44
import static com.github.kagkarlsson.scheduler.ScheduledExecutionsFilter.onlyResolved;
55
import static com.github.kagkarlsson.scheduler.jdbc.JdbcTaskRepository.DEFAULT_TABLE_NAME;
6+
import static java.time.Duration.ofDays;
7+
import static java.time.Duration.ofMinutes;
68
import static org.hamcrest.MatcherAssert.assertThat;
79
import static org.hamcrest.Matchers.contains;
810
import static org.hamcrest.Matchers.empty;
@@ -175,13 +177,13 @@ public void get_due_should_be_sorted_by_priority_when_enabled() {
175177
Instant now = TimeHelper.truncatedInstantNow();
176178
SchedulableTaskInstance<Void> id1 =
177179
new SchedulableTaskInstance<>(
178-
oneTimeTask.instanceBuilder("id1").priority(1).build(), now.minus(Duration.ofDays(1)));
180+
oneTimeTask.instanceBuilder("id1").priority(1).build(), now.minus(ofDays(1)));
179181
SchedulableTaskInstance<Void> id2 =
180182
new SchedulableTaskInstance<>(
181-
oneTimeTask.instanceBuilder("id2").priority(10).build(), now.minus(Duration.ofDays(2)));
183+
oneTimeTask.instanceBuilder("id2").priority(10).build(), now.minus(ofDays(2)));
182184
SchedulableTaskInstance<Void> id3 =
183185
new SchedulableTaskInstance<>(
184-
oneTimeTask.instanceBuilder("id3").priority(5).build(), now.minus(Duration.ofDays(3)));
186+
oneTimeTask.instanceBuilder("id3").priority(5).build(), now.minus(ofDays(3)));
185187

186188
Stream.of(id1, id2, id3).forEach(taskRespositoryWithPriority::createIfNotExists);
187189

@@ -295,7 +297,7 @@ public void reschedule_should_move_execution_in_time() {
295297

296298
Execution execution = due.get(0);
297299
final Optional<Execution> pickedExecution = taskRepository.pick(execution, now);
298-
final Instant nextExecutionTime = now.plus(Duration.ofMinutes(1));
300+
final Instant nextExecutionTime = now.plus(ofMinutes(1));
299301
taskRepository.reschedule(pickedExecution.get(), nextExecutionTime, now, null, 0);
300302

301303
assertThat(taskRepository.getDue(now, POLLING_LIMIT), hasSize(0));
@@ -318,7 +320,7 @@ public void reschedule_should_persist_consecutive_failures() {
318320

319321
Execution execution = due.get(0);
320322
final Optional<Execution> pickedExecution = taskRepository.pick(execution, now);
321-
final Instant nextExecutionTime = now.plus(Duration.ofMinutes(1));
323+
final Instant nextExecutionTime = now.plus(ofMinutes(1));
322324
taskRepository.reschedule(
323325
pickedExecution.get(), nextExecutionTime, now.minusSeconds(1), now, 1);
324326

@@ -336,7 +338,7 @@ public void reschedule_should_update_data_if_specified() {
336338
Execution created = taskRepository.getExecution(instance).get();
337339
assertEquals(created.taskInstance.getData(), 1);
338340

339-
final Instant nextExecutionTime = now.plus(Duration.ofMinutes(1));
341+
final Instant nextExecutionTime = now.plus(ofMinutes(1));
340342
taskRepository.reschedule(created, nextExecutionTime, 2, now, null, 0);
341343

342344
final Execution rescheduled = taskRepository.getExecution(instance).get();
@@ -359,16 +361,43 @@ public void test_get_failing_executions() {
359361

360362
taskRepository.reschedule(getSingleDueExecution(), now, null, now, 1);
361363
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(1));
362-
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ofMinutes(1)), hasSize(1));
363-
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ofDays(1)), hasSize(1));
364+
assertThat(taskRepository.getExecutionsFailingLongerThan(ofMinutes(1)), hasSize(1));
365+
assertThat(taskRepository.getExecutionsFailingLongerThan(ofDays(1)), hasSize(1));
364366

365-
taskRepository.reschedule(
366-
getSingleDueExecution(), now, now.minus(Duration.ofMinutes(1)), now, 1);
367+
taskRepository.reschedule(getSingleDueExecution(), now, now.minus(ofMinutes(1)), now, 1);
367368
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(1));
368369
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ofSeconds(1)), hasSize(1));
369370
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ofHours(1)), hasSize(0));
370371
}
371372

373+
@Test
374+
public void
375+
get_failing_executions_should_not_return_previously_failed_but_currently_successful() {
376+
Instant third = TimeHelper.truncatedInstantNow();
377+
Instant second = third.minus(ofMinutes(1));
378+
Instant first = second.minus(ofMinutes(1));
379+
380+
Instant timeToRun = first;
381+
382+
final TaskInstance<Void> instance = oneTimeTask.instance("id1");
383+
taskRepository.createIfNotExists(new SchedulableTaskInstance<>(instance, timeToRun));
384+
385+
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(0));
386+
387+
// simulate success
388+
taskRepository.reschedule(getSingleDueExecution(), timeToRun, first, null, 0);
389+
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(0));
390+
391+
// simulate failure
392+
taskRepository.reschedule(getSingleDueExecution(), timeToRun, first, second, 1);
393+
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(1));
394+
assertThat(taskRepository.getExecutionsFailingLongerThan(ofMinutes(5)), hasSize(0));
395+
396+
// simulate success
397+
taskRepository.reschedule(getSingleDueExecution(), timeToRun, third, second, 0);
398+
assertThat(taskRepository.getExecutionsFailingLongerThan(Duration.ZERO), hasSize(0));
399+
}
400+
372401
@Test
373402
public void get_scheduled_executions() {
374403
Instant now = TimeHelper.truncatedInstantNow();
@@ -426,7 +455,7 @@ public void get_dead_executions_should_not_include_previously_unresolved() {
426455
Instant now = TimeHelper.truncatedInstantNow();
427456

428457
// 1
429-
final Instant timeDied = now.minus(Duration.ofDays(5));
458+
final Instant timeDied = now.minus(ofDays(5));
430459
createDeadExecution(oneTimeTask.instance("id1"), timeDied);
431460

432461
TaskResolver taskResolverMissingTask =

db-scheduler/src/test/java/com/github/kagkarlsson/scheduler/helper/TimeHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ private TimeHelper() {}
1313
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8068730">JDK-8068730</a>
1414
*/
1515
public static Instant truncatedInstantNow() {
16-
return Instant.now().truncatedTo(MILLIS);
16+
return truncated(Instant.now());
17+
}
18+
19+
private static Instant truncated(Instant instant) {
20+
return instant.truncatedTo(MILLIS);
1721
}
1822
}

0 commit comments

Comments
 (0)