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

Add ExecutionOpernation.rescheduleAndResetFailures #119

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ target/
.project
.settings/
dependency-reduced-pom.xml
.terraform
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (C) Gustav Karlsson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler.task;

import com.github.kagkarlsson.scheduler.TaskRepository;

import java.time.Instant;

public class CompletionOperations<T> extends ExecutionOperations<T> {

public CompletionOperations(TaskRepository taskRepository, Execution execution) {
super(taskRepository, execution);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import java.time.Instant;

public class ExecutionOperations<T> {
public abstract class ExecutionOperations<T> {

private final TaskRepository taskRepository;
private final Execution execution;
protected final TaskRepository taskRepository;
protected final Execution execution;

public ExecutionOperations(TaskRepository taskRepository, Execution execution) {
this.taskRepository = taskRepository;
Expand Down Expand Up @@ -50,4 +50,16 @@ public void reschedule(ExecutionComplete completed, Instant nextExecutionTime, T
}
}

// public void rescheduleAndResetFailures(ExecutionComplete completed, Instant nextExecutionTime) {
// if (completed.getResult() == ExecutionComplete.Result.OK) {
// taskRepository.reschedule(execution, nextExecutionTime, completed.getTimeDone(), execution.lastFailure, 0);
// } else {
// taskRepository.reschedule(execution, nextExecutionTime, execution.lastSuccess, completed.getTimeDone(), 0);
// }
// }
//
// public void rescheduleAndResetFailures(Instant nextExecutionTime, T newData) {
// taskRepository.reschedule(execution, nextExecutionTime, newData, execution.lastSuccess, execution.lastFailure, 0);
// }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) Gustav Karlsson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.kagkarlsson.scheduler.task;

import com.github.kagkarlsson.scheduler.TaskRepository;

import java.time.Instant;

public abstract class FailureOperations<T> extends ExecutionOperations<T> {

public FailureOperations(TaskRepository taskRepository, Execution execution) {
super(taskRepository, execution);
}

public void rescheduleAndResetFailures(ExecutionComplete completed, Instant nextExecutionTime) {
taskRepository.reschedule(execution, nextExecutionTime, execution.lastSuccess, completed.getTimeDone(), 0);
}

public void rescheduleAndResetFailures(ExecutionComplete completed, Instant nextExecutionTime, T newData) {
taskRepository.reschedule(execution, nextExecutionTime, newData, execution.lastSuccess, completed.getTimeDone(), 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.kagkarlsson.scheduler.example;

import com.github.kagkarlsson.scheduler.HsqlTestDatabaseExtension;
import com.github.kagkarlsson.scheduler.Scheduler;
import com.github.kagkarlsson.scheduler.task.ExecutionComplete;
import com.github.kagkarlsson.scheduler.task.ExecutionContext;
import com.github.kagkarlsson.scheduler.task.ExecutionOperations;
import com.github.kagkarlsson.scheduler.task.FailureHandler;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.VoidExecutionHandler;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import com.github.kagkarlsson.scheduler.task.helper.RecurringTask;
import com.github.kagkarlsson.scheduler.task.helper.Tasks;
import com.github.kagkarlsson.scheduler.task.schedule.Schedule;
import com.github.kagkarlsson.scheduler.task.schedule.Schedules;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;

public class MaxRetriesRecurringMain {
private static final Logger LOG = LoggerFactory.getLogger(MaxRetriesRecurringMain.class);

private static void example(DataSource dataSource) {

final Schedule mainSchedule = Schedules.fixedDelay(Duration.ofSeconds(10));

final RecurringTask<Void> failingRecurringTask = Tasks.recurring("max_retries_recurring_task", mainSchedule)
.onFailure((executionComplete, executionOperations) -> {
if (executionComplete.getExecution().consecutiveFailures >= 3) {
System.out.println("Execution has failed " + executionComplete.getExecution().consecutiveFailures
+ " times. Resetting to next execution-time according to main schedule.");
executionOperations.rescheduleAndResetFailures(mainSchedule.getNextExecutionTime(executionComplete));
} else {
// try again in 1 second (max 3 times)
System.out.println("Execution has failed " + executionComplete.getExecution().consecutiveFailures + " times. Trying again in a bit...");

executionOperations.reschedule(executionComplete, executionComplete.getTimeDone().plusSeconds(1));
}
})
.execute((taskInstance, executionContext) -> {
throw new RuntimeException("simulated task exception");
});

final Scheduler scheduler = Scheduler
.create(dataSource)
.startTasks(failingRecurringTask)
.pollingInterval(Duration.ofSeconds(1))
.build();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
LOG.info("Received shutdown signal.");
scheduler.stop();
}));

scheduler.start();
}

public static void main(String[] args) throws Throwable {
try {
final HsqlTestDatabaseExtension hsqlRule = new HsqlTestDatabaseExtension();
hsqlRule.beforeEach(null);

final DataSource dataSource = hsqlRule.getDataSource();

example(dataSource);
} catch (Exception e) {
LOG.error("Error", e);
}

}

}