Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve flexibility of the MaxRetriesFailureHandler #554

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,9 @@ There are a number of users that are using db-scheduler for high throughput use-

See [releases](https://github.com/kagkarlsson/db-scheduler/releases) for release-notes.

**Upgrading to 16.x**
* The behavior of tasks with a defined `maxRetriesExceededHandler` for the `MaxRetriesFailureHandler` has been updated for greater flexibility. Tasks will no longer be automatically removed. To maintain the previous behavior of task removal, please invoke `executionOperations.stop()` within your `maxRetriesExceededHandler`. If you have not defined a `maxRetriesExceededHandler`, the behavior remains unchanged, and no further action is required.

**Upgrading to 15.x**
* Priority is a new opt-in feature. To be able to use it, column `priority` and index `priority_execution_time_idx`
must be added to the database schema. See table definitions for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class MaxRetriesFailureHandler<T> implements FailureHandler<T> {
private final BiConsumer<ExecutionComplete, ExecutionOperations<T>> maxRetriesExceededHandler;

public MaxRetriesFailureHandler(int maxRetries, FailureHandler<T> failureHandler) {
this(maxRetries, failureHandler, (executionComplete, executionOperations) -> {});
this(
maxRetries,
failureHandler,
(executionComplete, executionOperations) -> executionOperations.stop());
}

public MaxRetriesFailureHandler(
Expand All @@ -90,10 +93,9 @@ public void onFailure(
int totalNumberOfFailures = consecutiveFailures + 1;
if (totalNumberOfFailures > maxRetries) {
LOG.error(
"Execution has failed {} times for task instance {}. Cancelling execution.",
"Execution has failed {} times for task instance {}. Invoking maxRetriesExceededHandler",
totalNumberOfFailures,
executionComplete.getExecution().taskInstance);
executionOperations.stop();
maxRetriesExceededHandler.accept(executionComplete, executionOperations);
Comment on lines -96 to 99
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure about the original intent of the maxRetriesExceededHandler. The handler should not have taken executionOperations as a parameter since the execution had already been stopped.

} else {
this.failureHandler.onFailure(executionComplete, executionOperations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,37 @@ void should_run_handler_when_max_retries_exceeded() {

assertThat(failureHandlerCalled.get(), is(false));
assertThat(maxRetriesExceededHandlerCalled.get(), is(true));
}

@Test
void should_remove_task_when_maxRetriesExceededHandler_is_undefined_and_max_retries_exceeded() {
MaxRetriesFailureHandler<String> maxRetriesFailureHandler =
new MaxRetriesFailureHandler<>(3, failureHandler);

Execution execution = getExecutionWithFails(3);
when(executionComplete.getExecution()).thenReturn(execution);

maxRetriesFailureHandler.onFailure(executionComplete, executionOperations);

assertThat(failureHandlerCalled.get(), is(false));
verify(executionOperations).stop();
}

@Test
void should_keep_task_when_maxRetriesExceededHandler_is_defined_and_max_retries_exceeded() {
MaxRetriesFailureHandler<String> maxRetriesFailureHandler =
new MaxRetriesFailureHandler<>(3, failureHandler, maxRetriesExceededHandler);

Execution execution = getExecutionWithFails(3);
when(executionComplete.getExecution()).thenReturn(execution);

maxRetriesFailureHandler.onFailure(executionComplete, executionOperations);

assertThat(failureHandlerCalled.get(), is(false));
assertThat(maxRetriesExceededHandlerCalled.get(), is(true));
verifyNoInteractions(executionOperations);
}

@Test
void should_do_not_run_max_retries_handler_when_retries_are_not_exceeded() {
MaxRetriesFailureHandler<String> maxRetriesFailureHandler =
Expand Down
Loading